|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Message.php |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.6+ |
|
6
|
|
|
* |
|
7
|
|
|
* @author Philippe Gaultier <[email protected]> |
|
8
|
|
|
* @copyright 2010-2016 Philippe Gaultier |
|
9
|
|
|
* @license http://www.sweelix.net/license license |
|
10
|
|
|
* @version XXX |
|
11
|
|
|
* @link http://www.sweelix.net |
|
12
|
|
|
* @package sweelix\postmark |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace sweelix\postmark; |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
use yii\base\InvalidConfigException; |
|
19
|
|
|
use yii\base\InvalidParamException; |
|
20
|
|
|
use yii\base\NotSupportedException; |
|
21
|
|
|
use yii\helpers\ArrayHelper; |
|
22
|
|
|
use yii\mail\BaseMessage; |
|
23
|
|
|
use Yii; |
|
24
|
|
|
use yii\mail\MailerInterface; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* This component allow user to send an email |
|
28
|
|
|
* |
|
29
|
|
|
* @author Philippe Gaultier <[email protected]> |
|
30
|
|
|
* @copyright 2010-2016 Philippe Gaultier |
|
31
|
|
|
* @license http://www.sweelix.net/license license |
|
32
|
|
|
* @version XXX |
|
33
|
|
|
* @link http://www.sweelix.net |
|
34
|
|
|
* @package sweelix\postmark |
|
35
|
|
|
* @since XXX |
|
36
|
|
|
*/ |
|
37
|
|
|
class Message extends BaseMessage |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* @var string|array from |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $from; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $to = []; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var string|array reply to |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $replyTo; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var array |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $cc = []; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var array |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $bcc = []; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var string |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $subject; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var string |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $textBody; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @var string |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $htmlBody; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @var array |
|
81
|
|
|
*/ |
|
82
|
|
|
protected $attachments = []; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @var string |
|
86
|
|
|
*/ |
|
87
|
|
|
protected $tag; |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @var bool |
|
91
|
|
|
*/ |
|
92
|
|
|
protected $trackOpens = true; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @var array |
|
96
|
|
|
*/ |
|
97
|
|
|
protected $headers = []; |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @var integer |
|
101
|
|
|
*/ |
|
102
|
|
|
protected $templateId; |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @var array |
|
106
|
|
|
*/ |
|
107
|
|
|
protected $templateModel; |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @var bool |
|
111
|
|
|
*/ |
|
112
|
|
|
protected $inlineCss = true; |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @inheritdoc |
|
116
|
|
|
*/ |
|
117
|
1 |
|
public function getCharset() |
|
118
|
|
|
{ |
|
119
|
1 |
|
throw new NotSupportedException(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @inheritdoc |
|
124
|
|
|
*/ |
|
125
|
1 |
|
public function setCharset($charset) |
|
126
|
|
|
{ |
|
127
|
1 |
|
throw new NotSupportedException(); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @inheritdoc |
|
132
|
|
|
*/ |
|
133
|
1 |
|
public function getFrom() |
|
134
|
|
|
{ |
|
135
|
1 |
|
return self::stringifyEmails($this->from); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @inheritdoc |
|
140
|
|
|
*/ |
|
141
|
3 |
|
public function setFrom($from) |
|
142
|
|
|
{ |
|
143
|
3 |
|
$this->from = $from; |
|
144
|
3 |
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @inheritdoc |
|
149
|
|
|
*/ |
|
150
|
3 |
|
public function getTo() |
|
151
|
|
|
{ |
|
152
|
3 |
|
return $this->to; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @inheritdoc |
|
157
|
|
|
*/ |
|
158
|
3 |
|
public function setTo($to) |
|
159
|
|
|
{ |
|
160
|
3 |
|
if (is_string($to) === true) { |
|
161
|
3 |
|
$to = [$to]; |
|
162
|
3 |
|
} |
|
163
|
3 |
|
$this->to = $to; |
|
164
|
3 |
|
return $this; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @inheritdoc |
|
169
|
|
|
*/ |
|
170
|
1 |
|
public function getReplyTo() |
|
171
|
|
|
{ |
|
172
|
1 |
|
return self::stringifyEmails($this->replyTo); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @inheritdoc |
|
177
|
|
|
*/ |
|
178
|
1 |
|
public function setReplyTo($replyTo) |
|
179
|
|
|
{ |
|
180
|
1 |
|
$this->replyTo = $replyTo; |
|
181
|
1 |
|
return $this; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @inheritdoc |
|
186
|
|
|
*/ |
|
187
|
1 |
|
public function getCc() |
|
188
|
|
|
{ |
|
189
|
1 |
|
return $this->cc; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @inheritdoc |
|
194
|
|
|
*/ |
|
195
|
1 |
|
public function setCc($cc) |
|
196
|
|
|
{ |
|
197
|
1 |
|
if (is_string($cc) === true) { |
|
198
|
1 |
|
$cc = [$cc]; |
|
199
|
1 |
|
} |
|
200
|
1 |
|
$this->cc = $cc; |
|
201
|
1 |
|
return $this; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @inheritdoc |
|
206
|
|
|
*/ |
|
207
|
1 |
|
public function getBcc() |
|
208
|
|
|
{ |
|
209
|
1 |
|
return $this->bcc; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @inheritdoc |
|
214
|
|
|
*/ |
|
215
|
1 |
|
public function setBcc($bcc) |
|
216
|
|
|
{ |
|
217
|
1 |
|
if (is_string($bcc) === true) { |
|
218
|
1 |
|
$bcc = [$bcc]; |
|
219
|
1 |
|
} |
|
220
|
1 |
|
$this->bcc = $bcc; |
|
221
|
1 |
|
return $this; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @inheritdoc |
|
226
|
|
|
*/ |
|
227
|
3 |
|
public function getSubject() |
|
228
|
|
|
{ |
|
229
|
3 |
|
return $this->subject; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* @inheritdoc |
|
234
|
|
|
*/ |
|
235
|
2 |
|
public function setSubject($subject) |
|
236
|
|
|
{ |
|
237
|
2 |
|
$this->subject = $subject; |
|
238
|
2 |
|
return $this; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @return string|null text body of the message |
|
243
|
|
|
* @since XXX |
|
244
|
|
|
*/ |
|
245
|
1 |
|
public function getTextBody() |
|
246
|
|
|
{ |
|
247
|
1 |
|
return $this->textBody; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* @inheritdoc |
|
252
|
|
|
*/ |
|
253
|
2 |
|
public function setTextBody($text) |
|
254
|
|
|
{ |
|
255
|
2 |
|
$this->textBody = $text; |
|
256
|
2 |
|
return $this; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* @return string|null html body of the message |
|
261
|
|
|
* @since XXX |
|
262
|
|
|
*/ |
|
263
|
1 |
|
public function getHtmlBody() |
|
264
|
|
|
{ |
|
265
|
1 |
|
return $this->htmlBody; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* @inheritdoc |
|
270
|
|
|
*/ |
|
271
|
1 |
|
public function setHtmlBody($html) |
|
272
|
|
|
{ |
|
273
|
1 |
|
$this->htmlBody = $html; |
|
274
|
1 |
|
return $this; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* @return string tag associated to the email |
|
279
|
|
|
* @since XXX |
|
280
|
|
|
*/ |
|
281
|
1 |
|
public function getTag() |
|
282
|
|
|
{ |
|
283
|
1 |
|
return $this->tag; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* @param string $tag tag which should be associated to the email |
|
288
|
|
|
* @return $this |
|
289
|
|
|
* @since XXX |
|
290
|
|
|
*/ |
|
291
|
1 |
|
public function setTag($tag) |
|
292
|
|
|
{ |
|
293
|
1 |
|
$this->tag = $tag; |
|
294
|
1 |
|
return $this; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* @param bool $trackOpens define if mail should be tracked |
|
299
|
|
|
* @return $this |
|
300
|
|
|
* @since XXX |
|
301
|
|
|
*/ |
|
302
|
1 |
|
public function setTrackOpens($trackOpens) |
|
303
|
|
|
{ |
|
304
|
1 |
|
$this->trackOpens = $trackOpens; |
|
305
|
1 |
|
return $this; |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* @return bool tracking status |
|
310
|
|
|
* @since XXX |
|
311
|
|
|
*/ |
|
312
|
1 |
|
public function getTrackOpens() |
|
313
|
|
|
{ |
|
314
|
1 |
|
return $this->trackOpens; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
/** |
|
318
|
|
|
* @param integer $templateId template Id used. in this case, Subject / HtmlBody / TextBody are discarded |
|
319
|
|
|
* @return $this |
|
320
|
|
|
* @since XXX |
|
321
|
|
|
*/ |
|
322
|
2 |
|
public function setTemplateId($templateId) |
|
323
|
|
|
{ |
|
324
|
2 |
|
$this->templateId = $templateId; |
|
325
|
2 |
|
return $this; |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
/** |
|
329
|
|
|
* @return integer|null current templateId |
|
330
|
|
|
* @since XXX |
|
331
|
|
|
*/ |
|
332
|
1 |
|
public function getTemplateId() |
|
333
|
|
|
{ |
|
334
|
1 |
|
return $this->templateId; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* @param array $templateModel model associated with the template |
|
339
|
|
|
* @return $this |
|
340
|
|
|
* @since XXX |
|
341
|
|
|
*/ |
|
342
|
2 |
|
public function setTemplateModel($templateModel) |
|
343
|
|
|
{ |
|
344
|
2 |
|
$this->templateModel = $templateModel; |
|
345
|
2 |
|
return $this; |
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
|
|
/** |
|
349
|
|
|
* @return array current template model |
|
350
|
|
|
* @since XXX |
|
351
|
|
|
*/ |
|
352
|
1 |
|
public function getTemplateModel() |
|
353
|
|
|
{ |
|
354
|
1 |
|
return $this->templateModel; |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
/** |
|
358
|
|
|
* @param bool $inlineCss define if css should be inlined |
|
359
|
|
|
* @return $this |
|
360
|
|
|
* @since XXX |
|
361
|
|
|
*/ |
|
362
|
1 |
|
public function setInlineCss($inlineCss) |
|
363
|
|
|
{ |
|
364
|
1 |
|
$this->inlineCss = $inlineCss; |
|
365
|
1 |
|
return $this; |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
/** |
|
369
|
|
|
* @return bool define if css should be inlined |
|
370
|
|
|
* @since XXX |
|
371
|
|
|
*/ |
|
372
|
1 |
|
public function getInlineCss() |
|
373
|
|
|
{ |
|
374
|
1 |
|
return $this->inlineCss; |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
/** |
|
378
|
|
|
* @param array $header add custom header to the mail |
|
379
|
|
|
* @since XXX |
|
380
|
|
|
*/ |
|
381
|
1 |
|
public function addHeader($header) |
|
382
|
|
|
{ |
|
383
|
1 |
|
$this->headers[] = $header; |
|
384
|
1 |
|
} |
|
385
|
|
|
|
|
386
|
|
|
/** |
|
387
|
|
|
* @return array|null headers which should be added to the mail |
|
388
|
|
|
* @since XXX |
|
389
|
|
|
*/ |
|
390
|
1 |
|
public function getHeaders() |
|
391
|
|
|
{ |
|
392
|
1 |
|
return empty($this->headers) ? null : $this->headers; |
|
393
|
|
|
} |
|
394
|
|
|
|
|
395
|
|
|
/** |
|
396
|
|
|
* @return array|null list of attachments |
|
397
|
|
|
* @since XXX |
|
398
|
|
|
*/ |
|
399
|
1 |
|
public function getAttachments() |
|
400
|
|
|
{ |
|
401
|
1 |
|
return empty($this->attachments) ? null : $this->attachments; |
|
402
|
|
|
} |
|
403
|
|
|
|
|
404
|
|
|
/** |
|
405
|
|
|
* @inheritdoc |
|
406
|
|
|
*/ |
|
407
|
1 |
|
public function attach($fileName, array $options = []) |
|
408
|
|
|
{ |
|
409
|
|
|
$attachment = [ |
|
410
|
1 |
|
'Content' => base64_encode(file_get_contents($fileName)) |
|
411
|
1 |
|
]; |
|
412
|
1 |
|
if (!empty($options['fileName'])) { |
|
413
|
1 |
|
$attachment['Name'] = $options['fileName']; |
|
414
|
1 |
|
} else { |
|
415
|
1 |
|
$attachment['Name'] = pathinfo($fileName, PATHINFO_BASENAME); |
|
416
|
|
|
} |
|
417
|
1 |
|
if (!empty($options['contentType'])) { |
|
418
|
1 |
|
$attachment['ContentType'] = $options['contentType']; |
|
419
|
1 |
|
} else { |
|
420
|
1 |
|
$attachment['ContentType'] = 'application/octet-stream'; |
|
421
|
|
|
} |
|
422
|
1 |
|
$this->attachments[] = $attachment; |
|
423
|
1 |
|
return $this; |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
|
|
/** |
|
427
|
|
|
* @inheritdoc |
|
428
|
|
|
*/ |
|
429
|
2 |
|
public function attachContent($content, array $options = []) |
|
430
|
|
|
{ |
|
431
|
|
|
$attachment = [ |
|
432
|
2 |
|
'Content' => base64_encode($content) |
|
433
|
2 |
|
]; |
|
434
|
2 |
|
if (!empty($options['fileName'])) { |
|
435
|
1 |
|
$attachment['Name'] = $options['fileName']; |
|
436
|
1 |
|
} else { |
|
437
|
1 |
|
throw new InvalidParamException('Filename is missing'); |
|
438
|
|
|
} |
|
439
|
1 |
|
if (!empty($options['contentType'])) { |
|
440
|
1 |
|
$attachment['ContentType'] = $options['contentType']; |
|
441
|
1 |
|
} else { |
|
442
|
1 |
|
$attachment['ContentType'] = 'application/octet-stream'; |
|
443
|
|
|
} |
|
444
|
1 |
|
$this->attachments[] = $attachment; |
|
445
|
1 |
|
return $this; |
|
446
|
|
|
} |
|
447
|
|
|
|
|
448
|
|
|
/** |
|
449
|
|
|
* @inheritdoc |
|
450
|
|
|
*/ |
|
451
|
1 |
|
public function embed($fileName, array $options = []) |
|
452
|
|
|
{ |
|
453
|
|
|
$embed = [ |
|
454
|
1 |
|
'Content' => base64_encode(file_get_contents($fileName)) |
|
455
|
1 |
|
]; |
|
456
|
1 |
|
if (!empty($options['fileName'])) { |
|
457
|
1 |
|
$embed['Name'] = $options['fileName']; |
|
458
|
1 |
|
} else { |
|
459
|
1 |
|
$embed['Name'] = pathinfo($fileName, PATHINFO_BASENAME); |
|
460
|
|
|
} |
|
461
|
1 |
|
if (!empty($options['contentType'])) { |
|
462
|
1 |
|
$embed['ContentType'] = $options['contentType']; |
|
463
|
1 |
|
} else { |
|
464
|
1 |
|
$embed['ContentType'] = 'application/octet-stream'; |
|
465
|
|
|
} |
|
466
|
1 |
|
$embed['ContentID'] = 'cid:' . uniqid(); |
|
467
|
1 |
|
$this->attachments[] = $embed; |
|
468
|
1 |
|
return $embed['ContentID']; |
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
/** |
|
472
|
|
|
* @inheritdoc |
|
473
|
|
|
*/ |
|
474
|
2 |
|
public function embedContent($content, array $options = []) |
|
475
|
|
|
{ |
|
476
|
|
|
$embed = [ |
|
477
|
2 |
|
'Content' => base64_encode($content) |
|
478
|
2 |
|
]; |
|
479
|
2 |
|
if (!empty($options['fileName'])) { |
|
480
|
1 |
|
$embed['Name'] = $options['fileName']; |
|
481
|
1 |
|
} else { |
|
482
|
1 |
|
throw new InvalidParamException('Filename is missing'); |
|
483
|
|
|
} |
|
484
|
1 |
|
if (!empty($options['contentType'])) { |
|
485
|
1 |
|
$embed['ContentType'] = $options['contentType']; |
|
486
|
1 |
|
} else { |
|
487
|
1 |
|
$embed['ContentType'] = 'application/octet-stream'; |
|
488
|
|
|
} |
|
489
|
1 |
|
$embed['ContentID'] = 'cid:' . uniqid(); |
|
490
|
1 |
|
$this->attachments[] = $embed; |
|
491
|
1 |
|
return $embed['ContentID']; |
|
492
|
|
|
} |
|
493
|
|
|
|
|
494
|
|
|
/** |
|
495
|
|
|
* @inheritdoc |
|
496
|
|
|
* @todo make real serialization to make message compliant with PostmarkAPI |
|
497
|
|
|
*/ |
|
498
|
|
|
public function toString() |
|
499
|
|
|
{ |
|
500
|
|
|
return serialize($this); |
|
501
|
|
|
} |
|
502
|
|
|
|
|
503
|
|
|
|
|
504
|
|
|
/** |
|
505
|
|
|
* @param array|string $emailsData email can be defined as string. In this case no transformation is done |
|
506
|
|
|
* or as an array ['[email protected]', '[email protected]' => 'Email 2'] |
|
507
|
|
|
* @return string|null |
|
508
|
|
|
* @since XXX |
|
509
|
|
|
*/ |
|
510
|
1 |
|
public static function stringifyEmails($emailsData) |
|
511
|
|
|
{ |
|
512
|
1 |
|
$emails = null; |
|
513
|
1 |
|
if (empty($emailsData) === false) { |
|
514
|
1 |
|
if (is_array($emailsData) === true) { |
|
515
|
1 |
|
foreach ($emailsData as $key => $email) { |
|
516
|
1 |
|
if (is_int($key) === true) { |
|
517
|
1 |
|
$emails[] = $email; |
|
518
|
1 |
|
} else { |
|
519
|
1 |
|
if (preg_match('/[.,:]/', $email) > 0) { |
|
520
|
1 |
|
$email = '"'. $email .'"'; |
|
521
|
1 |
|
} |
|
522
|
1 |
|
$emails[] = $email . ' ' . '<' . $key . '>'; |
|
523
|
|
|
} |
|
524
|
1 |
|
} |
|
525
|
1 |
|
$emails = implode(', ', $emails); |
|
526
|
1 |
|
} elseif (is_string($emailsData) === true) { |
|
527
|
1 |
|
$emails = $emailsData; |
|
528
|
1 |
|
} |
|
529
|
1 |
|
} |
|
530
|
1 |
|
return $emails; |
|
531
|
|
|
} |
|
532
|
|
|
|
|
533
|
|
|
|
|
534
|
|
|
} |