Completed
Pull Request — master (#6483)
by Sam
09:46
created

TestMailer::saveEmail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace SilverStripe\Dev;
4
5
use SilverStripe\Control\Email\Mailer;
6
7
class TestMailer extends Mailer
8
{
9
    protected $emailsSent = array();
10
11
    /**
12
     * Send a plain-text email.
13
     * TestMailer will merely record that the email was asked to be sent, without sending anything.
14
     *
15
     * @param string $to
16
     * @param string $from
17
     * @param string $subject
18
     * @param string $plainContent
19
     * @param bool $attachedFiles
20
     * @param bool $customHeaders
21
     * @return bool|mixed
22
     */
23
    public function sendPlain($to, $from, $subject, $plainContent, $attachedFiles = false, $customHeaders = false)
24
    {
25
        $this->saveEmail([
26
            'Type' => 'plain',
27
            'To' => $to,
28
            'From' => $from,
29
            'Subject' => $subject,
30
31
            'Content' => $plainContent,
32
            'PlainContent' => $plainContent,
33
34
            'AttachedFiles' => $attachedFiles,
35
            'CustomHeaders' => $customHeaders,
36
        ]);
37
38
        return true;
39
    }
40
41
    /**
42
     * Send a multi-part HTML email
43
     * TestMailer will merely record that the email was asked to be sent, without sending anything.
44
     *
45
     * @param string $to
46
     * @param string $from
47
     * @param string $subject
48
     * @param string $htmlContent
49
     * @param bool $attachedFiles
50
     * @param bool $customHeaders
51
     * @param bool $plainContent
52
     * @param bool $inlineImages
53
     * @return bool|mixed
54
     */
55
    public function sendHTML(
56
        $to,
57
        $from,
58
        $subject,
59
        $htmlContent,
60
        $attachedFiles = false,
61
        $customHeaders = false,
62
        $plainContent = false,
63
        $inlineImages = false
64
    ) {
65
66
        $this->saveEmail([
67
            'Type' => 'html',
68
            'To' => $to,
69
            'From' => $from,
70
            'Subject' => $subject,
71
72
            'Content' => $htmlContent,
73
            'PlainContent' => $plainContent,
74
            'HtmlContent' => $htmlContent,
75
76
            'AttachedFiles' => $attachedFiles,
77
            'CustomHeaders' => $customHeaders,
78
            'InlineImages' => $inlineImages,
79
        ]);
80
81
        return true;
82
    }
83
84
    /**
85
     * Save a single email to the log
86
     * @param $data A map of information about the email
87
     */
88
    protected function saveEmail($data)
89
    {
90
        $this->emailsSent[] = $data;
91
    }
92
93
    /**
94
     * Clear the log of emails sent
95
     */
96
    public function clearEmails()
97
    {
98
        $this->emailsSent = array();
99
    }
100
101
    /**
102
     * Search for an email that was sent.
103
     * All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression.
104
     *
105
     * @param string $to
106
     * @param string $from
107
     * @param string $subject
108
     * @param string $content
109
     * @return array Contains the keys: 'type', 'to', 'from', 'subject', 'content', 'plainContent', 'attachedFiles',
110
     *               'customHeaders', 'htmlContent', 'inlineImages'
111
     */
112
    public function findEmail($to, $from = null, $subject = null, $content = null)
113
    {
114
        $compare = [
115
            'To' => $to,
116
            'From' => $from,
117
            'Subject' => $subject,
118
            'Content' => $content,
119
        ];
120
121
        foreach ($this->emailsSent as $email) {
122
            $matched = true;
123
124
            foreach (array('To','From','Subject','Content') as $field) {
125
                if ($value = $compare[$field]) {
126
                    if ($value[0] == '/') {
127
                        $matched = preg_match($value, $email[$field]);
128
                    } else {
129
                        $matched = ($value == $email[$field]);
130
                    }
131
                    if (!$matched) {
132
                        break;
133
                    }
134
                }
135
            }
136
137
            if ($matched) {
138
                return $email;
139
            }
140
        }
141
    }
142
}
143