Mailer   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 246
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 0
dl 0
loc 246
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A addTo() 0 4 1
A addCc() 0 4 1
A addBcc() 0 4 1
A setFrom() 0 4 1
A setReplyTo() 0 4 1
A setSubject() 0 4 1
A setContent() 0 4 1
A addAttachment() 0 10 1
A send() 0 5 1
A sendMail() 0 7 2
A getBoundary() 0 7 2
B getHeaders() 0 27 6
B getMessage() 0 46 5
1
<?php
2
3
namespace Faulancer\Mail;
4
5
/**
6
 * Class Mailer | Mailer.php
7
 * @package Faulancer\Mail
8
 * @author  Florian Knapp <[email protected]>
9
 *
10
 * +--Example usage:
11
 * |
12
 * | $mail = new Mailer();
13
 * | $mail->addTo('[email protected]');
14
 * | $mail->addCc('[email protected]');
15
 * | $mail->addBcc('[email protected]');
16
 * | $mail->setFrom('Test User <[email protected]>');
17
 * | $mail->setSubject('Testsubject');
18
 * | $mail->addAttachment('shownFileName.png', '/absolute/path/to/file.png');
19
 * | $mail->addAttachment('logo.png', '/absolute/path/to/logo.png', true); // We want to show it inline
20
 * | $mail->setContent('<img src="cid:logo.png"><br /><h3>Welcome to our newsletter</h3>... ... ...');
21
 * | $mail->send(); // Html is on per default; give this method 'false' to send in plain text
22
 * |
23
 */
24
class Mailer
25
{
26
27
    /**
28
     * @var array
29
     */
30
    protected $recipients = [];
31
32
    /**
33
     * @var array
34
     */
35
    protected $carbonCopies = [];
36
37
    /**
38
     * @var array
39
     */
40
    protected $blindCarbonCopies = [];
41
42
    /**
43
     * @var string
44
     */
45
    protected $from = '';
46
47
    /**
48
     * @var string
49
     */
50
    protected $replyTo = '';
51
52
    /**
53
     * @var string
54
     */
55
    protected $subject = '';
56
57
    /**
58
     * @var string
59
     */
60
    protected $content = '';
61
62
    /**
63
     * @var bool
64
     */
65
    protected $isHtml = false;
66
67
    /**
68
     * @var array
69
     */
70
    protected $attachment = [];
71
72
    /**
73
     * @var string
74
     */
75
    protected $boundary = '';
76
77
    /**
78
     * @param string $name
79
     */
80
    public function addTo(string $name)
81
    {
82
        $this->recipients[] = $name;
83
    }
84
85
    /**
86
     * @param string $name
87
     */
88
    public function addCc(string $name)
89
    {
90
        $this->carbonCopies[] = $name;
91
    }
92
93
    /**
94
     * @param string $name
95
     */
96
    public function addBcc(string $name)
97
    {
98
        $this->blindCarbonCopies[] = $name;
99
    }
100
101
    /**
102
     * @param string $name
103
     */
104
    public function setFrom(string $name)
105
    {
106
        $this->from = $name;
107
    }
108
109
    public function setReplyTo(string $name)
110
    {
111
        $this->replyTo = $name;
112
    }
113
114
    /**
115
     * @param string $subject
116
     */
117
    public function setSubject(string $subject)
118
    {
119
        $this->subject = $subject;
120
    }
121
122
    public function setContent(string $content)
123
    {
124
        $this->content = $content;
125
    }
126
127
    /**
128
     * @param string $name
129
     * @param string $path
130
     * @param bool   $inline
131
     */
132
    public function addAttachment(string $name, string $path, bool $inline = false)
133
    {
134
        $this->attachment[] = [
135
            'name'     => $name,
136
            'path'     => $path,
137
            'encoded'  => chunk_split(base64_encode(file_get_contents($path))),
138
            'mimetype' => mime_content_type($path),
139
            'inline'   => $inline
140
        ];
141
    }
142
143
    /**
144
     * Send email
145
     *
146
     * @param bool $html Whether to send as html or plain text
147
     * @return bool
148
     */
149
    public function send($html = true)
150
    {
151
        $this->isHtml = $html;
152
        return $this->sendMail($this->getHeaders(), $this->getMessage());
153
    }
154
155
    /**
156
     * Executes mail command
157
     *
158
     * @param string $headers
159
     * @param string $message
160
     *
161
     * @return bool
162
     */
163
    protected function sendMail($headers, $message)
164
    {
165
        if (mail(implode(',', $this->recipients), mb_encode_mimeheader($this->subject, 'UTF-8'), $message, $headers)) {
166
            return true;
167
        }
168
        return false;
169
    }
170
171
    /**
172
     * Get boundary for email headers
173
     *
174
     * @return string
175
     */
176
    protected function getBoundary()
177
    {
178
        if (empty($this->boundary)) {
179
            $this->boundary = md5('faulancer');
180
        }
181
        return $this->boundary;
182
    }
183
184
    /**
185
     * Get headers
186
     *
187
     * @return string
188
     */
189
    protected function getHeaders()
190
    {
191
        $eol      = "\r\n";
192
        $boundary = $this->getBoundary();
193
        $headers = 'MIME-Version: 1.0' . $eol;
194
        $headers .= 'Content-Type: multipart/mixed; charset=UTF-8; boundary=' . $boundary . $eol;
195
196
        if (!empty($this->from)) {
197
            $headers .= 'From: ' .$this->from . $eol;
198
        }
199
200
        if (!empty($this->replyTo)) {
201
            $headers .= 'Reply-To: ' . $this->replyTo . $eol;
202
        } else if (!empty($this->from)) {
203
            $headers .= 'Reply-To: ' . $this->from . $eol;
204
        }
205
206
        if (!empty($this->carbonCopies)) {
207
            $headers .= 'CC: ' . implode(',', $this->carbonCopies) . $eol;
208
        }
209
210
        if (!empty($this->blindCarbonCopies)) {
211
            $headers .= 'BCC: ' . implode(',', $this->blindCarbonCopies) . $eol;
212
        }
213
214
        return $headers;
215
    }
216
217
    /**
218
     * Get message body
219
     *
220
     * @return string
221
     */
222
    protected function getMessage()
223
    {
224
        $eol      = "\r\n";
225
        $boundary = $this->getBoundary();
226
227
        $body = '--' . $boundary . $eol;
228
229
        if ($this->isHtml) {
230
            $body .= 'Content-Type: text/html; charset="utf-8"' . $eol;
231
        } else {
232
            $body .= 'Content-Type: text/plain; charset="utf-8"' . $eol;
233
        }
234
235
        $body .= 'Content-Transfer-Encoding: 8bit' . $eol . $eol;
236
        $body .= $this->content . $eol;
237
238
        if (!empty($this->attachment)) {
239
240
            foreach ($this->attachment as $attachment) {
241
242
                $encodedContent = $attachment['encoded'];
243
                $fileType       = $attachment['mimetype'];
244
                $fileName       = $attachment['name'];
245
246
                $body .= '--' . $boundary . $eol;
247
                $body .= 'Content-Type: ' . $fileType . '; name=' . $fileName . $eol;
248
249
                if ($attachment['inline']) {
250
                    $body .= 'Content-ID: <' . $fileName . '>' . $eol;
251
                    $body .= 'Content-Disposition: inline; filename=' . $fileName . $eol;
252
                } else {
253
                    $body .= 'Content-Disposition: attachment; filename=' . $fileName . $eol;
254
                }
255
256
                $body .= 'Content-Transfer-Encoding: base64' . $eol;
257
                $body .= 'X-Attachment-Id: ' . rand(1000,99999) . $eol . $eol;
258
                $body .= $encodedContent . $eol;
259
260
            }
261
262
        }
263
264
        $body .= '--' . $boundary . '--' . $eol;
265
266
        return $body;
267
    }
268
269
}