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
|
|
|
$headers = $email->getSwiftMessage()->getHeaders(); |
45
|
|
|
$cc = $headers->get('CC') ? $headers->get('CC')->getFieldBody() : ''; |
46
|
|
|
$bcc = $headers->get('BCC') ? $headers->get('BCC')->getFieldBody() : ''; |
47
|
|
|
|
48
|
|
|
// Serialise email |
49
|
|
|
$serialised = [ |
50
|
|
|
'Type' => $type, |
51
|
|
|
'To' => implode(';', array_keys($email->getTo() ?: [])), |
52
|
|
|
'From' => implode(';', array_keys($email->getFrom() ?: [])), |
53
|
|
|
'Subject' => $email->getSubject(), |
54
|
|
|
'Content' => $email->getBody(), |
55
|
|
|
'AttachedFiles' => $attachedFiles, |
56
|
|
|
'Headers' => [ |
57
|
|
|
'Cc' => $cc, |
58
|
|
|
'Bcc' => $bcc, |
59
|
|
|
], |
60
|
|
|
]; |
61
|
|
|
if ($plainContent) { |
62
|
|
|
$serialised['PlainContent'] = $plainContent; |
63
|
|
|
} |
64
|
|
|
if ($htmlContent) { |
65
|
|
|
$serialised['HtmlContent'] = $htmlContent; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->saveEmail($serialised); |
69
|
|
|
|
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Save a single email to the log |
75
|
|
|
* |
76
|
|
|
* @param array $data A map of information about the email |
77
|
|
|
*/ |
78
|
|
|
protected function saveEmail($data) |
79
|
|
|
{ |
80
|
|
|
$this->emailsSent[] = $data; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Clear the log of emails sent |
85
|
|
|
*/ |
86
|
|
|
public function clearEmails() |
87
|
|
|
{ |
88
|
|
|
$this->emailsSent = []; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Search for an email that was sent. |
93
|
|
|
* All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression. |
94
|
|
|
* |
95
|
|
|
* @param string $to |
96
|
|
|
* @param string $from |
97
|
|
|
* @param string $subject |
98
|
|
|
* @param string $content |
99
|
|
|
* @return array|null Contains keys: 'Type', 'To', 'From', 'Subject', 'Content', 'PlainContent', 'AttachedFiles', |
100
|
|
|
* 'HtmlContent' |
101
|
|
|
*/ |
102
|
|
|
public function findEmail($to, $from = null, $subject = null, $content = null) |
103
|
|
|
{ |
104
|
|
|
$compare = [ |
105
|
|
|
'To' => $to, |
106
|
|
|
'From' => $from, |
107
|
|
|
'Subject' => $subject, |
108
|
|
|
'Content' => $content, |
109
|
|
|
]; |
110
|
|
|
|
111
|
|
|
foreach ($this->emailsSent as $email) { |
112
|
|
|
$matched = true; |
113
|
|
|
|
114
|
|
|
// Loop all our Email fields |
115
|
|
|
foreach ($compare as $field => $value) { |
116
|
|
|
$emailValue = $email[$field]; |
117
|
|
|
if ($value) { |
118
|
|
|
if (in_array($field, ['To', 'From'])) { |
119
|
|
|
$emailValue = $this->normaliseSpaces($emailValue); |
120
|
|
|
$value = $this->normaliseSpaces($value); |
121
|
|
|
} |
122
|
|
|
if ($value[0] === '/') { |
123
|
|
|
$matched = preg_match($value, $emailValue); |
124
|
|
|
} else { |
125
|
|
|
$matched = ($value === $emailValue); |
126
|
|
|
} |
127
|
|
|
if (!$matched) { |
128
|
|
|
break; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if ($matched) { |
134
|
|
|
return $email; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
return null; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $value |
142
|
|
|
*/ |
143
|
|
|
private function normaliseSpaces(string $value) |
144
|
|
|
{ |
145
|
|
|
return str_replace([', ', '; '], [',', ';'], $value); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|