2008-04-26 06:32:31 +00:00
|
|
|
<?php
|
2016-08-19 10:51:35 +12:00
|
|
|
|
|
|
|
namespace SilverStripe\Dev;
|
|
|
|
|
|
|
|
use SilverStripe\Control\Email\Mailer;
|
|
|
|
|
2016-11-29 12:31:16 +13:00
|
|
|
class TestMailer extends Mailer
|
|
|
|
{
|
|
|
|
protected $emailsSent = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a plain-text email.
|
|
|
|
* TestMailer will merely record that the email was asked to be sent, without sending anything.
|
|
|
|
*
|
|
|
|
* @param string $to
|
|
|
|
* @param string $from
|
|
|
|
* @param string $subject
|
|
|
|
* @param string $plainContent
|
|
|
|
* @param bool $attachedFiles
|
|
|
|
* @param bool $customHeaders
|
|
|
|
* @return bool|mixed
|
|
|
|
*/
|
|
|
|
public function sendPlain($to, $from, $subject, $plainContent, $attachedFiles = false, $customHeaders = false)
|
|
|
|
{
|
2017-01-12 09:55:10 +13:00
|
|
|
$this->saveEmail([
|
|
|
|
'Type' => 'plain',
|
|
|
|
'To' => $to,
|
|
|
|
'From' => $from,
|
|
|
|
'Subject' => $subject,
|
2016-11-29 12:31:16 +13:00
|
|
|
|
2017-01-12 09:55:10 +13:00
|
|
|
'Content' => $plainContent,
|
|
|
|
'PlainContent' => $plainContent,
|
2016-11-29 12:31:16 +13:00
|
|
|
|
2017-01-12 09:55:10 +13:00
|
|
|
'AttachedFiles' => $attachedFiles,
|
|
|
|
'CustomHeaders' => $customHeaders,
|
|
|
|
]);
|
2016-11-29 12:31:16 +13:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a multi-part HTML email
|
|
|
|
* TestMailer will merely record that the email was asked to be sent, without sending anything.
|
|
|
|
*
|
|
|
|
* @param string $to
|
|
|
|
* @param string $from
|
|
|
|
* @param string $subject
|
|
|
|
* @param string $htmlContent
|
|
|
|
* @param bool $attachedFiles
|
|
|
|
* @param bool $customHeaders
|
|
|
|
* @param bool $plainContent
|
|
|
|
* @param bool $inlineImages
|
|
|
|
* @return bool|mixed
|
|
|
|
*/
|
|
|
|
public function sendHTML(
|
|
|
|
$to,
|
|
|
|
$from,
|
|
|
|
$subject,
|
|
|
|
$htmlContent,
|
|
|
|
$attachedFiles = false,
|
|
|
|
$customHeaders = false,
|
|
|
|
$plainContent = false,
|
|
|
|
$inlineImages = false
|
|
|
|
) {
|
|
|
|
|
2017-01-12 09:55:10 +13:00
|
|
|
$this->saveEmail([
|
|
|
|
'Type' => 'html',
|
|
|
|
'To' => $to,
|
|
|
|
'From' => $from,
|
|
|
|
'Subject' => $subject,
|
2016-11-29 12:31:16 +13:00
|
|
|
|
2017-01-12 09:55:10 +13:00
|
|
|
'Content' => $htmlContent,
|
|
|
|
'PlainContent' => $plainContent,
|
|
|
|
'HtmlContent' => $htmlContent,
|
2016-11-29 12:31:16 +13:00
|
|
|
|
2017-01-12 09:55:10 +13:00
|
|
|
'AttachedFiles' => $attachedFiles,
|
|
|
|
'CustomHeaders' => $customHeaders,
|
|
|
|
'InlineImages' => $inlineImages,
|
|
|
|
]);
|
2016-11-29 12:31:16 +13:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-12 09:55:10 +13:00
|
|
|
/**
|
|
|
|
* Save a single email to the log
|
|
|
|
* @param $data A map of information about the email
|
|
|
|
*/
|
|
|
|
protected function saveEmail($data)
|
|
|
|
{
|
|
|
|
$this->emailsSent[] = $data;
|
|
|
|
}
|
|
|
|
|
2016-11-29 12:31:16 +13:00
|
|
|
/**
|
|
|
|
* Clear the log of emails sent
|
|
|
|
*/
|
|
|
|
public function clearEmails()
|
|
|
|
{
|
|
|
|
$this->emailsSent = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @return array Contains the keys: 'type', 'to', 'from', 'subject', 'content', 'plainContent', 'attachedFiles',
|
|
|
|
* 'customHeaders', 'htmlContent', 'inlineImages'
|
|
|
|
*/
|
|
|
|
public function findEmail($to, $from = null, $subject = null, $content = null)
|
|
|
|
{
|
2017-01-12 09:55:10 +13:00
|
|
|
$compare = [
|
|
|
|
'To' => $to,
|
|
|
|
'From' => $from,
|
|
|
|
'Subject' => $subject,
|
|
|
|
'Content' => $content,
|
|
|
|
];
|
|
|
|
|
2016-11-29 12:31:16 +13:00
|
|
|
foreach ($this->emailsSent as $email) {
|
|
|
|
$matched = true;
|
|
|
|
|
2017-01-12 09:55:10 +13:00
|
|
|
foreach (array('To','From','Subject','Content') as $field) {
|
|
|
|
if ($value = $compare[$field]) {
|
2016-11-29 12:31:16 +13:00
|
|
|
if ($value[0] == '/') {
|
|
|
|
$matched = preg_match($value, $email[$field]);
|
|
|
|
} else {
|
|
|
|
$matched = ($value == $email[$field]);
|
|
|
|
}
|
|
|
|
if (!$matched) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($matched) {
|
|
|
|
return $email;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-24 16:04:52 +13:00
|
|
|
}
|