forked from francisberesford/snapcms-yii2-mandrill
/
Message.php
439 lines (406 loc) · 11.5 KB
/
Message.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php
namespace snapcms\mandrill;
use Yii;
use yii\mail\BaseMessage;
use yii\helpers\HtmlPurifier;
use yii\validators\EmailValidator;
use yii\helpers\ArrayHelper;
use yii\helpers\FileHelper;
/**
* @author Francis Beresford <francis@snapfrozen.com>
* Some parts by Nicola Puddu which were lifted from: https://github.com/nickcv-ln/yii2-mandrill
*/
class Message extends BaseMessage
{
public $throwErrors = true;
public $emailErrorMessage = 'Email {type} field "{email}" is not valid';
private $_to = [];
private $_cc = [];
private $_bcc = [];
private $_from;
private $_fromName;
private $_replyTo = [];
private $_subject;
private $_htmlBody;
private $_textBody;
private $_recipients = [];
private $_tags = [];
private $_attachments = [];
private $_images = [];
private $_finfo;
/**
* @inheritdoc
*/
public function setTo($to)
{
$this->setRecipients('to', $to);
return $this;
}
/**
* @inheritdoc
*/
public function getTo()
{
return $this->_to;
}
/**
* @inheritdoc
*/
public function setCc($cc)
{
$this->setRecipients('cc', $cc);
return $this;
}
/**
* @inheritdoc
*/
public function getCc()
{
return $this->_cc;
}
/**
* @inheritdoc
*/
public function setBcc($bcc)
{
$this->setRecipients('bcc', $bcc);
return $this;
}
/**
* @inheritdoc
*/
public function getBcc()
{
return $this->_bcc;
}
/**
* @inheritdoc
*/
public function setFrom($from)
{
if (is_string($from) && $this->validateEmail($from))
{
$this->_from = $from;
$this->_fromName = null;
}
if (is_array($from))
{
$email = key($from);
$name = array_shift($from);
if (!$this->validateEmail($email))
{
return $this;
}
$this->_from = $email;
$this->_fromName = trim($name);
}
return $this;
}
/**
* @inheritdoc
*/
public function getFrom()
{
return $this->_from;
}
/**
* @inheritdoc
*/
public function setReplyTo($replyTo)
{
$this->_replyTo [] = $replyTo;
return $this;
}
/**
* @inheritdoc
*/
public function getReplyTo()
{
return $this->_replyTo;
}
/**
* @inheritdoc
*/
public function setSubject($subject)
{
$this->_subject = $subject;
return $this;
}
/**
* @inheritdoc
*/
public function getSubject()
{
return $this->_subject;
}
/**
* @inheritdoc
*/
public function setHtmlBody($html)
{
$this->_htmlBody = $html;
return $this;
}
public function getHtmlBody()
{
return $this->_htmlBody;
}
public function setTextBody($text)
{
$this->_textBody = HtmlPurifier::process($text);
return $this;
}
/**
* Cannot set charset with Mandrill
* @param type $charset
* @return \snapcms\mandrill\Message
*/
public function setCharset($charset)
{
return $this;
}
/**
* Cannot set charset with Mandrill
*/
public function getCharset()
{
return null;
}
/**
* Taken from: https://github.com/nickcv-ln/yii2-mandrill
* @todo: Test and possibly hook into SnapCMS media files system (also to be created)
*
* Attaches existing file to the email message.
*
* @see \snapcms\mandrill\Message::getAttachments() getter
* @see \snapcms\mandrill\Message::$_attachments private attribute
*
* @param string $fileName full file name
* @param array $options options for embed file. Valid options are:
*
* - fileName: name, which should be used to attach file.
* - contentType: attached file MIME type.
*
* @return \snapcms\mandrill\Message
*/
public function attach($fileName, array $options = array())
{
if (file_exists($fileName) && !is_dir($fileName))
{
$purifiedOptions = [
'fileName' => ArrayHelper::getValue($options, 'fileName', basename($fileName)),
'contentType' => ArrayHelper::getValue($options, 'contentType', FileHelper::getMimeType($fileName)),
];
$this->attachContent(file_get_contents($fileName), $purifiedOptions);
}
return $this;
}
/**
* Attach specified content as file for the email message.
*
* @see \snapcms\mandrill\Message::getAttachments() getter
* @see \snapcms\mandrill\Message::$_attachments private attribute
*
* @param string $content attachment file content.
* @param array $options options for embed file. Valid options are:
*
* - fileName: name, which should be used to attach file.
* - contentType: attached file MIME type.
*
* @return \snapcms\mandrill\Message
*/
public function attachContent($content, array $options = [])
{
$purifiedOptions = is_array($options) ? $options : [];
if (is_string($content) && strlen($content) !== 0)
{
$this->_attachments[] = [
'name' => ArrayHelper::getValue($purifiedOptions, 'fileName', ('file_' . count($this->_attachments))),
'type' => ArrayHelper::getValue($purifiedOptions, 'contentType', $this->getMimeTypeFromBinary($content)),
'content' => base64_encode($content),
];
}
return $this;
}
/**
* Returns the Mime Type from the file binary.
*
* @param string $binary
* @return string
*/
private function getMimeTypeFromBinary($binary)
{
if ($this->_finfo === null)
{
$this->_finfo = new \finfo(FILEINFO_MIME_TYPE);
}
return $this->_finfo->buffer($binary);
}
/**
* Embed a binary as an image in the message.
*
* @see \snapcms\mandrill\Message::$_images private attribute
* @see \snapcms\mandrill\Message::getEmbeddedContent() getter
*
* @param string $content attachment file content.
* @param array $options options for embed file. Valid options are:
*
* - fileName: name, which should be used to attach file.
* - contentType: attached file MIME type.
*
* @return \snapcms\mandrill\Message
*/
public function embedContent($content, array $options = array())
{
$purifiedOptions = is_array($options) ? $options : [];
if (is_string($content) && strlen($content) !== 0 && strpos($this->getMimeTypeFromBinary($content), 'image') === 0)
{
$this->_images[] = [
'name' => ArrayHelper::getValue($purifiedOptions, 'fileName', ('file_' . count($this->_images))),
'type' => ArrayHelper::getValue($purifiedOptions, 'contentType', $this->getMimeTypeFromBinary($content)),
'content' => base64_encode($content),
];
}
return $this;
}
/**
* Taken from: https://github.com/nickcv-ln/yii2-mandrill
*/
public function embed($fileName, array $options = array())
{
if (file_exists($fileName) && !is_dir($fileName) && strpos(FileHelper::getMimeType($fileName), 'image') === 0)
{
$purifiedOptions = [
'fileName' => ArrayHelper::getValue($options, 'fileName', basename($fileName)),
'contentType' => ArrayHelper::getValue($options, 'contentType', FileHelper::getMimeType($fileName)),
];
$this->embedContent(file_get_contents($fileName), $purifiedOptions);
}
return $this;
}
/**
* Returns the images array.
*
* @see \snapcms\mandrill\Message::$_images private attribute
* @see \snapcms\mandrill\Message::embed() setter for file name
* @see \snapcms\mandrill\Message::embedContent() setter for binary
*
* @return array list of embedded content
*/
public function getEmbeddedContent()
{
return $this->_images;
}
/**
* @inheritdoc
*/
public function toString()
{
return $this->getSubject() . ' - Recipients:'
. ' [TO] ' . implode('; ', $this->getTo())
. ' [CC] ' . implode('; ', $this->getCc())
. ' [BCC] ' . implode('; ', $this->getBcc());
}
/**
* Gets the string rappresentation of Reply-To to be later used in the
* email header.
*
* @return string
*/
private function getReplyToString()
{
$addresses = [];
foreach ($this->_replyTo as $key => $value)
{
if (is_string($key)) {
$addresses[] = $value . ' <' . $key . '>';
} else {
$addresses[] = $value;
}
}
return implode(';', $addresses);
}
/**
* Used to validate any emails going into Mandrill
* @param string $email
* @return boolean Whether the supplied argurment is a valid email or not
*/
protected function validateEmail($email)
{
$validator = new EmailValidator;
return $validator->validate($email);
}
/**
* Process and array of recipients in the format
* `[[email => name], [email => name]]`
* or just an array of emails without the name
* `[email, email, email]`
* @param type $type
* @param type $emails
*/
protected function setRecipients($type, $emails)
{
if (is_array($emails))
{
foreach ($emails as $email => $name)
{
if (is_numeric($email)) { //we have an array index, so the "$name" must be the email
$this->setRecipient($type, $name);
} else {
$this->setRecipient($type, $email, $name);
}
}
} else {
$this->setRecipient($type, $emails);
}
}
/**
* Set a single recipient ready to be sent.
* @param string $type "to", "cc" or "bcc"
* @param string $email
* @param string $name
* @throws \yii\base\Exception
*/
protected function setRecipient($type, $email, $name = null)
{
$errorMessage = Yii::t('app', $this->emailErrorMessage, ['email' => $email, 'type' => $type]);
$field = '_' . $type;
if ($this->validateEmail($email)) {
array_push($this->$field, $email);
} else if ($this->throwErrors) {
throw new \yii\base\Exception($errorMessage);
} else {
Yii::error($errorMessage, __METHOD__);
}
$this->_recipients[] = [
'email' => $email,
'name' => $name,
'type' => $type,
];
}
/**
* Returns the array used by the Mandrill Class to initialize a message
* and submit it.
*
* @return array
*/
public function getMandrillMessageArray()
{
return [
'headers' => [
'Reply-To' => $this->getReplyToString(),
],
'html' => $this->_htmlBody,
'text' => $this->_textBody,
'subject' => $this->getSubject(),
'from_email' => $this->_from,
'from_name' => $this->_fromName,
'to' => $this->_recipients,
'track_opens' => true,
'track_clicks' => true,
'tags' => $this->_tags,
'attachments' => $this->_attachments,
'images' => $this->_images,
];
}
}