Version: current

     1 <?php
     2 /**
     3  * @package sapphire
     4  * @subpackage email
     5  */
     6 class TestMailer extends Mailer {
     7     protected $emailsSent = array();
     8
     9     /**
    10      * Send a plain-text email.
    11      * TestMailer will merely record that the email was asked to be sent, without sending anything.
    12      */
    13     function sendPlain($to, $from, $subject, $plainContent, $attachedFiles = false, $customHeaders = false) {
    14         $this->emailsSent[] = array(
    15             'type' => 'plain',
    16             'to' => $to,
    17             'from' => $from,
    18             'subject' => $subject,
    19
    20             'content' => $plainContent,
    21             'plainContent' => $plainContent,
    22
    23             'attachedFiles' => $attachedFiles,
    24             'customHeaders' => $customHeaders,
    25         );
    26
    27         return true;
    28     }
    29
    30     /**
    31      * Send a multi-part HTML email
    32      * TestMailer will merely record that the email was asked to be sent, without sending anything.
    33      */
    34     function sendHTML($to, $from, $subject, $htmlContent, $attachedFiles = false, $customHeaders = false, $plainContent = false, $inlineImages = false) {
    35         $this->emailsSent[] = array(
    36             'type' => 'html',
    37             'to' => $to,
    38             'from' => $from,
    39             'subject' => $subject,
    40
    41             'content' => $htmlContent,
    42             'plainContent' => $plainContent,
    43             'htmlContent' => $htmlContent,
    44
    45             'attachedFiles' => $attachedFiles,
    46             'customHeaders' => $customHeaders,
    47             'inlineImages' => $inlineImages,
    48         );
    49
    50         return true;
    51     }
    52
    53     /**
    54      * Clear the log of emails sent
    55      */
    56     function clearEmails() {
    57         $this->emailsSent = array();
    58     }
    59
    60     /**
    61      * Search for an email that was sent.
    62      * All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression.
    63      * @param $to
    64      * @param $from
    65      * @param $subject
    66      * @param $content
    67      * @return An array containing the keys: 'type','to','from','subject','content', 'plainContent','attachedFiles','customHeaders','htmlContent',inlineImages'
    68      */
    69     function findEmail($to, $from = null, $subject = null, $content = null) {
    70         foreach($this->emailsSent as $email) {
    71             $matched = true;
    72
    73             foreach(array('to','from','subject','content') as $field) {
    74                 if($value = $$field) {
    75                     if($value[0] == '/') $matched = preg_match($value, $email[$field]);
    76                     else $matched = ($value == $email[$field]);
    77                     if(!$matched) break;
    78                 }
    79             }
    80
    81             if($matched) return $email;
    82         }
    83     }
    84
    85
    86 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -