1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jacobemerick\Archangel; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerAwareInterface; |
6
|
|
|
use Psr\Log\LoggerInterface; |
7
|
|
|
use Psr\Log\NullLogger; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This is the main class for Archangel mailer |
11
|
|
|
* For licensing and examples: |
12
|
|
|
* @see https://github.com/jacobemerick/archangel |
13
|
|
|
* |
14
|
|
|
* @author jacobemerick (http://home.jacobemerick.com/) |
15
|
|
|
*/ |
16
|
|
|
class Archangel implements LoggerAwareInterface |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** @var string $subject */ |
20
|
|
|
protected $subject; |
21
|
|
|
|
22
|
|
|
/** @var array $toAddresses */ |
23
|
|
|
protected $toAddresses = array(); |
24
|
|
|
|
25
|
|
|
/** @var array $headers */ |
26
|
|
|
protected $headers = array(); |
27
|
|
|
|
28
|
|
|
/** @var string $plainMessage */ |
29
|
|
|
protected $plainMessage; |
30
|
|
|
|
31
|
|
|
/** @var string $htmlMessage */ |
32
|
|
|
protected $htmlMessage; |
33
|
|
|
|
34
|
|
|
/** @var array $attachments */ |
35
|
|
|
protected $attachments = array(); |
36
|
|
|
|
37
|
|
|
/** @var string $boundaryMixed */ |
38
|
|
|
protected $boundaryMixed; |
39
|
|
|
|
40
|
|
|
/** @var string $boundaryAlternative */ |
41
|
|
|
protected $boundaryAlternative; |
42
|
|
|
|
43
|
|
|
/** @var LoggerInterface */ |
44
|
|
|
protected $logger; |
45
|
|
|
|
46
|
|
|
/** @var string LINE_BREAK */ |
47
|
|
|
const LINE_BREAK = "\r\n"; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $mailer |
51
|
|
|
*/ |
52
|
|
|
public function __construct($mailer = null) |
53
|
|
|
{ |
54
|
|
|
if (is_null($mailer)) { |
55
|
|
|
$mailer = sprintf('PHP/%s', phpversion()); |
56
|
|
|
} |
57
|
|
|
$this->headers['X-Mailer'] = $mailer; |
58
|
|
|
|
59
|
|
|
$this->logger = new NullLogger(); |
60
|
|
|
$this->boundaryMixed = sprintf('PHP-mixed-%s', uniqid()); |
61
|
|
|
$this->boundaryAlternative = sprintf('PHP-alternative-%s', uniqid()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param LoggerInterface $logger |
66
|
|
|
* |
67
|
|
|
* @return $this; |
|
|
|
|
68
|
|
|
*/ |
69
|
|
|
public function setLogger(LoggerInterface $logger) |
70
|
|
|
{ |
71
|
|
|
$this->logger = $logger; |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Setter method for adding recipients |
78
|
|
|
* |
79
|
|
|
* @param string $address email address for the recipient |
80
|
|
|
* @param string $title name of the recipient (optional) |
81
|
|
|
|
82
|
|
|
* @return object instantiated $this |
83
|
|
|
*/ |
84
|
|
|
public function addTo($address, $title = '') |
85
|
|
|
{ |
86
|
|
|
array_push( |
87
|
|
|
$this->toAddresses, |
88
|
|
|
$this->formatEmailAddress($address, $title) |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Setter method for adding cc recipients |
96
|
|
|
* |
97
|
|
|
* @param string $address email address for the cc recipient |
98
|
|
|
* @param string $title name of the cc recipient (optional) |
99
|
|
|
* |
100
|
|
|
* @return object instantiated $this |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
public function addCC($address, $title = '') |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
if (!isset($this->headers['CC'])) { |
105
|
|
|
$this->headers['CC'] = array(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
array_push( |
109
|
|
|
$this->headers['CC'], |
110
|
|
|
$this->formatEmailAddress($address, $title) |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Setter method for adding bcc recipients |
118
|
|
|
* |
119
|
|
|
* @param string $address email address for the bcc recipient |
120
|
|
|
* @param string $title name of the bcc recipient (optional) |
121
|
|
|
* |
122
|
|
|
* @return object instantiated $this |
123
|
|
|
*/ |
124
|
|
View Code Duplication |
public function addBCC($address, $title = '') |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
if (!isset($this->headers['BCC'])) { |
127
|
|
|
$this->headers['BCC'] = array(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
array_push( |
131
|
|
|
$this->headers['BCC'], |
132
|
|
|
$this->formatEmailAddress($address, $title) |
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Setter method for setting the single 'from' field |
140
|
|
|
* |
141
|
|
|
* @param string $address email address for the sender |
142
|
|
|
* @param string $title name of the sender (optional) |
143
|
|
|
* |
144
|
|
|
* @return object instantiated $this |
145
|
|
|
*/ |
146
|
|
|
public function setFrom($address, $title = '') |
147
|
|
|
{ |
148
|
|
|
$this->headers['From'] = $this->formatEmailAddress($address, $title); |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Setter method for setting the single 'reply-to' field |
155
|
|
|
* |
156
|
|
|
* @param string $address email address for the reply-to |
157
|
|
|
* @param string $title name of the reply-to (optional) |
158
|
|
|
* |
159
|
|
|
* @return object instantiated $this |
160
|
|
|
*/ |
161
|
|
|
public function setReplyTo($address, $title = '') |
162
|
|
|
{ |
163
|
|
|
$this->headers['Reply-To'] = $this->formatEmailAddress($address, $title); |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param string $address |
170
|
|
|
* @param string $title |
171
|
|
|
* |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
protected function formatEmailAddress($address, $title) |
175
|
|
|
{ |
176
|
|
|
if (!empty($title)) { |
177
|
|
|
$address = sprintf('"%s" <%s>', $title, $address); |
178
|
|
|
} |
179
|
|
|
return $address; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Setter method for setting a subject |
184
|
|
|
* |
185
|
|
|
* @param string $subject subject for the email |
186
|
|
|
* |
187
|
|
|
* @return object instantiated $this |
188
|
|
|
*/ |
189
|
|
|
public function setSubject($subject) |
190
|
|
|
{ |
191
|
|
|
$this->subject = $subject; |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Setter method for the plain text message |
198
|
|
|
* |
199
|
|
|
* @param string $message the plain-text message |
200
|
|
|
* |
201
|
|
|
* @return object instantiated $this |
202
|
|
|
*/ |
203
|
|
|
public function setPlainMessage($message) |
204
|
|
|
{ |
205
|
|
|
$this->plainMessage = $message; |
206
|
|
|
|
207
|
|
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Setter method for the html message |
212
|
|
|
* |
213
|
|
|
* @param string $message the html message |
214
|
|
|
* |
215
|
|
|
* @return object instantiated $this |
216
|
|
|
*/ |
217
|
|
|
public function setHTMLMessage($message) |
218
|
|
|
{ |
219
|
|
|
$this->htmlMessage = $message; |
220
|
|
|
|
221
|
|
|
return $this; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Setter method for adding attachments |
226
|
|
|
* |
227
|
|
|
* @param string $path the full path of the attachment |
228
|
|
|
* @param string $type mime type of the file |
229
|
|
|
* @param string $title the title of the attachment (optional) |
230
|
|
|
* |
231
|
|
|
* @return object instantiated $this |
232
|
|
|
*/ |
233
|
|
|
public function addAttachment($path, $type, $title = '') |
234
|
|
|
{ |
235
|
|
|
array_push($this->attachments, array( |
236
|
|
|
'path' => $path, |
237
|
|
|
'type' => $type, |
238
|
|
|
'title' => $title, |
239
|
|
|
)); |
240
|
|
|
|
241
|
|
|
return $this; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* The executing step, the actual sending of the email |
246
|
|
|
* First checks to make sure the minimum fields are set (returns false if they are not) |
247
|
|
|
* Second it attempts to send the mail with php's mail() (returns false if it fails) |
248
|
|
|
* |
249
|
|
|
* return boolean whether or not the email was valid & sent |
250
|
|
|
*/ |
251
|
|
|
public function send() |
252
|
|
|
{ |
253
|
|
|
if (!$this->checkRequiredFields()) { |
254
|
|
|
return false; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
$recipients = $this->buildTo(); |
258
|
|
|
$subject = $this->subject; |
259
|
|
|
$message = $this->buildMessage(); |
260
|
|
|
$headers = $this->buildHeaders(); |
261
|
|
|
|
262
|
|
|
return mail($recipients, $subject, $message, $headers); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Call to check the minimum required fields |
267
|
|
|
* |
268
|
|
|
* @return boolean whether or not the email meets the minimum required fields |
269
|
|
|
*/ |
270
|
|
|
protected function checkRequiredFields() |
271
|
|
|
{ |
272
|
|
|
if (empty($this->toAddresses)) { |
273
|
|
|
return false; |
274
|
|
|
} |
275
|
|
|
if (empty($this->subject)) { |
276
|
|
|
return false; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
if (empty($this->plainMessage) && empty($this->htmlMessage) && empty($this->attachments)) { |
280
|
|
|
return false; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
return true; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Build the recipients from 'to' |
288
|
|
|
* |
289
|
|
|
* @return string comma-separated lit of recipients |
290
|
|
|
*/ |
291
|
|
|
protected function buildTo() |
292
|
|
|
{ |
293
|
|
|
return implode(', ', $this->toAddresses); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Long, nasty creater of the actual message, with all the multipart logic you'd never want to see |
298
|
|
|
* |
299
|
|
|
* @return string email message |
300
|
|
|
*/ |
301
|
|
|
protected function buildMessage() |
302
|
|
|
{ |
303
|
|
|
$messageString = ''; |
304
|
|
|
|
305
|
|
|
if (!empty($this->attachments)) { |
306
|
|
|
$messageString .= "--{$this->boundaryMixed}" . self::LINE_BREAK; |
307
|
|
|
} |
308
|
|
|
if (!empty($this->plainMessage) && !empty($this->htmlMessage)) { |
309
|
|
View Code Duplication |
if (!empty($this->attachments)) { |
|
|
|
|
310
|
|
|
$messageString .= "Content-Type: multipart/alternative; boundary={$this->boundaryAlternative}" . self::LINE_BREAK; |
311
|
|
|
$messageString .= self::LINE_BREAK; |
312
|
|
|
} |
313
|
|
|
$messageString .= "--{$this->boundaryAlternative}" . self::LINE_BREAK; |
314
|
|
|
$messageString .= 'Content-Type: text/plain; charset="iso-8859"' . self::LINE_BREAK; |
315
|
|
|
$messageString .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK; |
316
|
|
|
$messageString .= self::LINE_BREAK; |
317
|
|
|
$messageString .= $this->plainMessage; |
318
|
|
|
$messageString .= self::LINE_BREAK; |
319
|
|
|
$messageString .= "--{$this->boundaryAlternative}" . self::LINE_BREAK; |
320
|
|
|
$messageString .= 'Content-Type: text/html; charset="iso-8859-1"' . self::LINE_BREAK; |
321
|
|
|
$messageString .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK; |
322
|
|
|
$messageString .= self::LINE_BREAK; |
323
|
|
|
$messageString .= $this->htmlMessage; |
324
|
|
|
$messageString .= self::LINE_BREAK; |
325
|
|
|
$messageString .= "--{$this->boundaryAlternative}--" . self::LINE_BREAK; |
326
|
|
|
$messageString .= self::LINE_BREAK; |
327
|
|
|
} elseif (!empty($this->plainMessage)) { |
328
|
|
View Code Duplication |
if (!empty($this->attachments)) { |
|
|
|
|
329
|
|
|
$messageString .= 'Content-Type: text/plain; charset="iso-8859"' . self::LINE_BREAK; |
330
|
|
|
$messageString .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK; |
331
|
|
|
$messageString .= self::LINE_BREAK; |
332
|
|
|
} |
333
|
|
|
$messageString .= $this->plainMessage; |
334
|
|
|
$messageString .= self::LINE_BREAK; |
335
|
|
|
} elseif (!empty($this->htmlMessage)) { |
336
|
|
View Code Duplication |
if (!empty($this->attachments)) { |
|
|
|
|
337
|
|
|
$messageString .= 'Content-Type: text/html; charset="iso-8859-1"' . self::LINE_BREAK; |
338
|
|
|
$messageString .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK; |
339
|
|
|
$messageString .= self::LINE_BREAK; |
340
|
|
|
} |
341
|
|
|
$messageString .= $this->htmlMessage; |
342
|
|
|
$messageString .= self::LINE_BREAK; |
343
|
|
|
} |
344
|
|
|
if (!empty($this->attachments)) { |
345
|
|
|
foreach ($this->attachments as $attachment) { |
346
|
|
|
$messageString .= "--{$this->boundaryMixed}" . self::LINE_BREAK; |
347
|
|
|
$messageString .= "Content-Type: {$attachment['type']}; name=\"{$attachment['title']}\"" . self::LINE_BREAK; |
348
|
|
|
$messageString .= 'Content-Transfer-Encoding: base64' . self::LINE_BREAK; |
349
|
|
|
$messageString .= 'Content-Disposition: attachment' . self::LINE_BREAK; |
350
|
|
|
$messageString .= self::LINE_BREAK; |
351
|
|
|
$messageString .= $this->buildAttachmentContent($attachment); |
352
|
|
|
$messageString .= self::LINE_BREAK; |
353
|
|
|
} |
354
|
|
|
$messageString .= "--{$this->boundaryMixed}--" . self::LINE_BREAK; |
355
|
|
|
} |
356
|
|
|
return $messageString; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Builder for the additional headers needed for multipart emails |
362
|
|
|
* |
363
|
|
|
* @return string headers needed for multipart |
364
|
|
|
*/ |
365
|
|
|
protected function buildHeaders() |
366
|
|
|
{ |
367
|
|
|
$headers = array(); |
368
|
|
|
foreach ($this->headers as $key => $value) { |
369
|
|
|
if ($key == 'CC' || $key == 'BCC') { |
370
|
|
|
$value = implode(', ', $value); |
371
|
|
|
} |
372
|
|
|
array_push($headers, sprintf('%s: %s', $key, $value)); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
if (!empty($this->attachments)) { |
376
|
|
|
array_push( |
377
|
|
|
$headers, |
378
|
|
|
"Content-Type: multipart/mixed; boundary=\"{$this->boundaryMixed}\"" |
379
|
|
|
); |
380
|
|
|
} elseif (!empty($this->plainMessage) && !empty($this->htmlMessage)) { |
381
|
|
|
array_push( |
382
|
|
|
$headers, |
383
|
|
|
"Content-Type: multipart/alternative; boundary=\"{$this->boundaryAlternative}\"" |
384
|
|
|
); |
385
|
|
|
} elseif (!empty($this->htmlMessage)) { |
386
|
|
|
array_push( |
387
|
|
|
$headers, |
388
|
|
|
'Content-type: text/html; charset="iso-8859-1"' |
389
|
|
|
); |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
return implode(self::LINE_BREAK, $headers); |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* File reader for attachments |
397
|
|
|
* |
398
|
|
|
* @return string binary representation of file, base64'd |
399
|
|
|
*/ |
400
|
|
|
protected function buildAttachmentContent($attachment) |
401
|
|
|
{ |
402
|
|
|
if (!file_exists($attachment['path'])) { |
403
|
|
|
return ''; // todo log error |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
$handle = fopen($attachment['path'], 'r'); |
407
|
|
|
$contents = fread($handle, filesize($attachment['path'])); |
408
|
|
|
fclose($handle); |
409
|
|
|
|
410
|
|
|
$contents = base64_encode($contents); |
411
|
|
|
$contents = chunk_split($contents); |
412
|
|
|
return $contents; |
413
|
|
|
} |
414
|
|
|
} |
415
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.