2008-04-26 08:32:31 +02:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\Dev;
|
|
|
|
|
|
|
|
use SilverStripe\Control\Email\Mailer;
|
2017-01-13 02:48:46 +01:00
|
|
|
use Swift_Attachment;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2017-01-13 02:48:46 +01:00
|
|
|
class TestMailer implements Mailer
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
|
|
|
/**
|
2017-01-13 02:48:46 +01:00
|
|
|
* @var array
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-01-13 02:48:46 +01:00
|
|
|
protected $emailsSent = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-01-13 02:48:46 +01:00
|
|
|
public function send($email)
|
|
|
|
{
|
|
|
|
// Detect body type
|
|
|
|
$htmlContent = null;
|
|
|
|
$plainContent = null;
|
|
|
|
if ($email->getSwiftMessage()->getContentType() === 'text/plain') {
|
|
|
|
$type = 'plain';
|
|
|
|
$plainContent = $email->getBody();
|
|
|
|
} else {
|
|
|
|
$type = 'html';
|
|
|
|
$htmlContent = $email->getBody();
|
|
|
|
$plainPart = $email->findPlainPart();
|
|
|
|
if ($plainPart) {
|
|
|
|
$plainContent = $plainPart->getBody();
|
|
|
|
}
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-01-13 02:48:46 +01:00
|
|
|
// Get attachments
|
|
|
|
$attachedFiles = [];
|
|
|
|
foreach ($email->getSwiftMessage()->getChildren() as $child) {
|
|
|
|
if ($child instanceof Swift_Attachment) {
|
|
|
|
$attachedFiles[] = [
|
|
|
|
'contents' => $child->getBody(),
|
|
|
|
'filename' => $child->getFilename(),
|
|
|
|
'mimetype' => $child->getContentType(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-01-13 02:48:46 +01:00
|
|
|
// Serialise email
|
|
|
|
$serialised = [
|
|
|
|
'Type' => $type,
|
|
|
|
'To' => implode(';', array_keys($email->getTo() ?: [])),
|
|
|
|
'From' => implode(';', array_keys($email->getFrom() ?: [])),
|
|
|
|
'Subject' => $email->getSubject(),
|
|
|
|
'Content' => $email->getBody(),
|
|
|
|
'AttachedFiles' => $attachedFiles
|
|
|
|
];
|
|
|
|
if ($plainContent) {
|
|
|
|
$serialised['PlainContent'] = $plainContent;
|
|
|
|
}
|
|
|
|
if ($htmlContent) {
|
|
|
|
$serialised['HtmlContent'] = $htmlContent;
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-01-13 02:48:46 +01:00
|
|
|
$this->saveEmail($serialised);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-11 21:55:10 +01:00
|
|
|
/**
|
|
|
|
* Save a single email to the log
|
2017-01-13 02:48:46 +01:00
|
|
|
*
|
|
|
|
* @param array $data A map of information about the email
|
2017-01-11 21:55:10 +01:00
|
|
|
*/
|
|
|
|
protected function saveEmail($data)
|
|
|
|
{
|
|
|
|
$this->emailsSent[] = $data;
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Clear the log of emails sent
|
|
|
|
*/
|
|
|
|
public function clearEmails()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->emailsSent = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for an email that was sent.
|
|
|
|
* All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression.
|
|
|
|
*
|
|
|
|
* @param string $to
|
|
|
|
* @param string $from
|
|
|
|
* @param string $subject
|
|
|
|
* @param string $content
|
2017-09-06 06:10:55 +02:00
|
|
|
* @return array|null Contains keys: 'Type', 'To', 'From', 'Subject', 'Content', 'PlainContent', 'AttachedFiles',
|
|
|
|
* 'HtmlContent'
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public function findEmail($to, $from = null, $subject = null, $content = null)
|
|
|
|
{
|
2017-01-11 21:55:10 +01:00
|
|
|
$compare = [
|
|
|
|
'To' => $to,
|
|
|
|
'From' => $from,
|
|
|
|
'Subject' => $subject,
|
|
|
|
'Content' => $content,
|
|
|
|
];
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
foreach ($this->emailsSent as $email) {
|
|
|
|
$matched = true;
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
foreach (['To', 'From', 'Subject', 'Content'] as $field) {
|
2021-08-24 00:06:15 +02:00
|
|
|
$emailValue = $email[$field];
|
2017-01-11 21:55:10 +01:00
|
|
|
if ($value = $compare[$field]) {
|
2021-08-24 00:06:15 +02:00
|
|
|
if ($field == 'To') {
|
|
|
|
$emailValue = $this->normaliseSpaces($emailValue);
|
|
|
|
$value = $this->normaliseSpaces($value);
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
if ($value[0] == '/') {
|
2021-08-24 00:06:15 +02:00
|
|
|
$matched = preg_match($value, $emailValue);
|
2016-11-29 00:31:16 +01:00
|
|
|
} else {
|
2021-08-24 00:06:15 +02:00
|
|
|
$matched = ($value == $emailValue);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
if (!$matched) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($matched) {
|
|
|
|
return $email;
|
|
|
|
}
|
|
|
|
}
|
2017-01-13 02:48:46 +01:00
|
|
|
return null;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2021-08-24 00:06:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $value
|
|
|
|
*/
|
|
|
|
private function normaliseSpaces(string $value)
|
|
|
|
{
|
|
|
|
return str_replace([', ', '; '], [',', ';'], $value);
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|