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;
|
2023-02-06 23:56:04 +01:00
|
|
|
use SilverStripe\Dev\Deprecation;
|
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
|
2023-02-06 23:56:04 +01:00
|
|
|
* @deprecated 4.13.0 Will be removed without equivalent functionality to replace it
|
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(),
|
2021-12-10 01:11:48 +01:00
|
|
|
'AttachedFiles' => $attachedFiles,
|
|
|
|
'Headers' => $email->getSwiftMessage()->getHeaders(),
|
2017-01-13 02:48:46 +01:00
|
|
|
];
|
|
|
|
if ($plainContent) {
|
|
|
|
$serialised['PlainContent'] = $plainContent;
|
|
|
|
}
|
|
|
|
if ($htmlContent) {
|
|
|
|
$serialised['HtmlContent'] = $htmlContent;
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2023-02-06 23:56:04 +01:00
|
|
|
Deprecation::withNoReplacement(function () use ($serialised) {
|
|
|
|
$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
|
2023-02-06 23:56:04 +01:00
|
|
|
* @deprecated 4.13.0 Will be removed without equivalent functionality to replace it
|
2017-01-11 21:55:10 +01:00
|
|
|
*/
|
|
|
|
protected function saveEmail($data)
|
|
|
|
{
|
2023-02-06 23:56:04 +01:00
|
|
|
Deprecation::notice('4.13.0', 'Will be removed without equivalent functionality to replace it');
|
2017-01-11 21:55:10 +01:00
|
|
|
$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;
|
|
|
|
|
2021-09-09 09:23:37 +02:00
|
|
|
// Loop all our Email fields
|
|
|
|
foreach ($compare as $field => $value) {
|
2021-08-24 00:06:15 +02:00
|
|
|
$emailValue = $email[$field];
|
2021-09-09 09:23:37 +02:00
|
|
|
if ($value) {
|
|
|
|
if (in_array($field, ['To', 'From'])) {
|
2021-08-24 00:06:15 +02:00
|
|
|
$emailValue = $this->normaliseSpaces($emailValue);
|
|
|
|
$value = $this->normaliseSpaces($value);
|
|
|
|
}
|
2021-09-09 09:23:37 +02:00
|
|
|
if ($value[0] === '/') {
|
2022-04-14 03:12:59 +02:00
|
|
|
$matched = preg_match($value ?? '', $emailValue ?? '');
|
2016-11-29 00:31:16 +01:00
|
|
|
} else {
|
2021-09-09 09:23:37 +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)
|
|
|
|
{
|
2022-04-14 03:12:59 +02:00
|
|
|
return str_replace([', ', '; '], [',', ';'], $value ?? '');
|
2021-08-24 00:06:15 +02:00
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|