|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Dev; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Control\Email\Mailer; |
|
6
|
|
|
use Swift_Attachment; |
|
7
|
|
|
|
|
8
|
|
|
class TestMailer implements Mailer |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var array |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $emailsSent = []; |
|
14
|
|
|
|
|
15
|
|
|
public function send($email) |
|
16
|
|
|
{ |
|
17
|
|
|
// Detect body type |
|
18
|
|
|
$htmlContent = null; |
|
19
|
|
|
$plainContent = null; |
|
20
|
|
|
if ($email->getSwiftMessage()->getContentType() === 'text/plain') { |
|
21
|
|
|
$type = 'plain'; |
|
22
|
|
|
$plainContent = $email->getBody(); |
|
23
|
|
|
} else { |
|
24
|
|
|
$type = 'html'; |
|
25
|
|
|
$htmlContent = $email->getBody(); |
|
26
|
|
|
$plainPart = $email->findPlainPart(); |
|
27
|
|
|
if ($plainPart) { |
|
28
|
|
|
$plainContent = $plainPart->getBody(); |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
// Get attachments |
|
33
|
|
|
$attachedFiles = []; |
|
34
|
|
|
foreach ($email->getSwiftMessage()->getChildren() as $child) { |
|
35
|
|
|
if ($child instanceof Swift_Attachment) { |
|
36
|
|
|
$attachedFiles[] = [ |
|
37
|
|
|
'contents' => $child->getBody(), |
|
38
|
|
|
'filename' => $child->getFilename(), |
|
39
|
|
|
'mimetype' => $child->getContentType(), |
|
40
|
|
|
]; |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// Serialise email |
|
45
|
|
|
$serialised = [ |
|
46
|
|
|
'Type' => $type, |
|
47
|
|
|
'To' => implode(';', array_keys($email->getTo() ?: [])), |
|
48
|
|
|
'From' => implode(';', array_keys($email->getFrom() ?: [])), |
|
49
|
|
|
'Subject' => $email->getSubject(), |
|
50
|
|
|
'Content' => $email->getBody(), |
|
51
|
|
|
'AttachedFiles' => $attachedFiles |
|
52
|
|
|
]; |
|
53
|
|
|
if ($plainContent) { |
|
|
|
|
|
|
54
|
|
|
$serialised['PlainContent'] = $plainContent; |
|
55
|
|
|
} |
|
56
|
|
|
if ($htmlContent) { |
|
|
|
|
|
|
57
|
|
|
$serialised['HtmlContent'] = $htmlContent; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$this->saveEmail($serialised); |
|
61
|
|
|
|
|
62
|
|
|
return true; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Save a single email to the log |
|
67
|
|
|
* |
|
68
|
|
|
* @param array $data A map of information about the email |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function saveEmail($data) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->emailsSent[] = $data; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Clear the log of emails sent |
|
77
|
|
|
*/ |
|
78
|
|
|
public function clearEmails() |
|
79
|
|
|
{ |
|
80
|
|
|
$this->emailsSent = array(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Search for an email that was sent. |
|
85
|
|
|
* All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $to |
|
88
|
|
|
* @param string $from |
|
89
|
|
|
* @param string $subject |
|
90
|
|
|
* @param string $content |
|
91
|
|
|
* @return array Contains the keys: 'type', 'to', 'from', 'subject', 'content', 'plainContent', 'attachedFiles', |
|
92
|
|
|
* 'customHeaders', 'htmlContent', 'inlineImages' |
|
93
|
|
|
*/ |
|
94
|
|
|
public function findEmail($to, $from = null, $subject = null, $content = null) |
|
95
|
|
|
{ |
|
96
|
|
|
$compare = [ |
|
97
|
|
|
'To' => $to, |
|
98
|
|
|
'From' => $from, |
|
99
|
|
|
'Subject' => $subject, |
|
100
|
|
|
'Content' => $content, |
|
101
|
|
|
]; |
|
102
|
|
|
|
|
103
|
|
|
foreach ($this->emailsSent as $email) { |
|
104
|
|
|
$matched = true; |
|
105
|
|
|
|
|
106
|
|
|
foreach (array('To','From','Subject','Content') as $field) { |
|
107
|
|
|
if ($value = $compare[$field]) { |
|
108
|
|
|
if ($value[0] == '/') { |
|
109
|
|
|
$matched = preg_match($value, $email[$field]); |
|
110
|
|
|
} else { |
|
111
|
|
|
$matched = ($value == $email[$field]); |
|
112
|
|
|
} |
|
113
|
|
|
if (!$matched) { |
|
114
|
|
|
break; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
if ($matched) { |
|
120
|
|
|
return $email; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
return null; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: