forked from nickcv-ln/yii2-mandrill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Message.php
1134 lines (1030 loc) · 29.8 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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Contains the Message class
*
* @link http://www.creationgears.com/
* @copyright Copyright (c) 2014 Nicola Puddu
* @license http://www.gnu.org/copyleft/gpl.html
* @package nickcv/yii2-mandrill
* @author Nicola Puddu <n.puddu@outlook.com>
*/
namespace nickcv\mandrill;
use yii\mail\BaseMessage;
use yii\helpers\Html;
use yii\helpers\HtmlPurifier;
use yii\helpers\ArrayHelper;
use yii\helpers\FileHelper;
/**
* Message is the class that is used to store the data of an email message that
* will be sent through Mandrill API.
*
* @author Nicola Puddu <n.puddu@outlook.com>
* @version 1.0
*/
class Message extends BaseMessage
{
public $response;
const LANGUAGE_MAILCHIMP = 'mailchimp';
const LANGUAGE_HANDLEBARS = 'handlebars';
/**
* Contains the custom from address. If empty the adminEmail param of the
* application will be used.
*
* @see \nickcv\mandrill\Message::setFrom() setter
* @see \nickcv\mandrill\Message::getFrom() getter
*
* @var string
*/
private $_fromAddress;
/**
* Contains the custom from name. If empty the app name will be used.
*
* @see \nickcv\mandrill\Message::setFrom() setter
* @see \nickcv\mandrill\Message::getFrom() getter
*
* @var string
*/
private $_fromName;
/**
* Contains the TO address list.
*
* @see \nickcv\mandrill\Message::setTo() setter
* @see \nickcv\mandrill\Message::getTo() getter
*
* @var array
*/
private $_to = [];
/**
* Contains the reply-to address list.
*
* @see \nickcv\mandrill\Message::setReplyTo() setter
* @see \nickcv\mandrill\Message::getReplyTo() getter
*
* @var array
*/
private $_replyTo = [];
/**
* Contains the CC address list.
*
* @see \nickcv\mandrill\Message::setCc() setter
* @see \nickcv\mandrill\Message::getCc() getter
*
* @var array
*/
private $_cc = [];
/**
* Contains the BCC address list.
*
* @see \nickcv\mandrill\Message::setBcc() setter
* @see \nickcv\mandrill\Message::getBcc() getter
*
* @var array
*/
private $_bcc = [];
/**
* Contains the tags list.
*
* @see \nickcv\mandrill\Message::setTags() setter
* @see \nickcv\mandrill\Message::getTags() getter
*
* @var array
*/
private $_tags = [];
/**
* Contains the html-encoded subject.
*
* @see \nickcv\mandrill\Message::setSubject() setter
* @see \nickcv\mandrill\Message::getSubject() getter
*
* @var string
*/
private $_subject;
/**
* Contains the email raw text.
*
* @see \nickcv\mandrill\Message::setTextBody() setter
* @see \nickcv\mandrill\Message::getTextBody() getter
*
* @var string
*/
private $_text;
/**
* Contains the email HTML test.
*
* @see \nickcv\mandrill\Message::setHtmlBody() setter
* @see \nickcv\mandrill\Message::getHtmlBody() getter
*
* @var string
*/
private $_html;
/**
* Contains the list of attachments already processed to be used by Mandrill.
* Each entry within the array is an array with the following keys:
*
* ~~~
* [
* 'name' => 'file.png', //the file name
* 'type' => 'image/png', //the file mime type
* 'content' => 'dGhpcyBpcyBzb21lIHRleHQ=' //the base64 encoded binary
* ]
* ~~~
*
* @see \nickcv\mandrill\Message::attach() setter
* @see \nickcv\mandrill\Message::getAttachments() getter
*
* @var array
*/
private $_attachments = [];
/**
* Contains the list of embedded images already processed to be used by Mandrill.
* Each entry within the array is an array with the following keys:
*
* ~~~
* [
* 'name' => 'file.png', //the file name
* 'type' => 'image/png', //the file mime type
* 'content' => 'dGhpcyBpcyBzb21lIHRleHQ=' //the base64 encoded binary
* ]
* ~~~
*
* @see \nickcv\mandrill\Message::embed() setter
* @see \nickcv\mandrill\Message::getEmbeddedContent() getter
*
* @var array
*/
private $_images = [];
/**
* Contains the instance of \finfo used to get mime type.
*
* @var \finfo
*/
private $_finfo;
/**
* In async mode, messages/send will immediately return a status of
* "queued" for every recipient.
*
* Mandrill defaults this value to false for messages with no more than
* 10 recipients; messages with more than 10 recipients are always sent
* asynchronously, regardless of the value of async.
*
* @var boolean
* @since 1.3.0
*/
private $_async = false;
/**
* The name of the template inside mandrill.
*
* @var string
* @since 1.3.0
*/
private $_templateName;
/**
* The values that will be used to replace the placeholders inside the template.
*
* @var array
* @since 1.3.0
*/
private $_templateContent;
/**
* Value use to decide whether the message should calculate default values
* for the sender based on the application settings or return nulls to use
* mandrill template defaults.
*
* @var boolean
* @since 1.4.0
*/
private $_ = false;
/**
* Global merge vars used when sending the message to mandrill.
*
* @var array
* @since 1.4.0
*/
private $_globalMergeVars = [];
/**
* What language will be used in the template
* Check @link http://handlebarsjs.com/ for more documentation about handlebars language
*
* @var string
* @since 1.5.0
*/
private $_mergeLanguage = self::LANGUAGE_MAILCHIMP;
/**
* Mandrill does not let users set a charset.
*
* @see \nickcv\mandrill\Message::setCharset() setter
*
* @return null
*/
public function getCharset()
{
return null;
}
/**
* Mandrill does not let users set a charset.
*
* @see \nickcv\mandrill\Message::getCharset() getter
*
* @param string $charset character set name.
* @return \nickcv\mandrill\Message
*/
public function setCharset($charset)
{
return $this;
}
/**
* Returns the list of tags you already set for this message.
*
* @see \nickcv\mandrill\Message::setTags() setter
*
* @return array the list of tags
*/
public function getTags()
{
return $this->_tags;
}
/**
* Mandrill lets you use tags to categorize your messages, making it much
* easier to find the messages your are looking for within their website
* dashboard.
*
* Stats are accumulated within mandrill using tags, though they only store
* the first 100 they see, so this should not be unique or change frequently.
* Tags should be 50 characters or less.
* Any tags starting with an underscore are reserved for internal use and
* will be ignored.
*
* Some common tags include *registration* and *password reset*.
*
* @see \nickcv\mandrill\Message::getTags() getter
*
* @param string|array $tag tag or list of tags
* @return \nickcv\mandrill\Message
*/
public function setTags($tag)
{
if (is_string($tag) && $this->isTagValid($tag, '_tags')) {
$this->_tags[] = $tag;
}
if (is_array($tag)) {
foreach ($tag as $singleTag) {
if ($this->isTagValid($singleTag, '_tags')) {
$this->_tags[] = $singleTag;
}
}
}
return $this;
}
/**
* Tells whether or not the message will be sent asynchronously.
*
* @return boolean
* @since 1.3.0
*/
public function isAsync()
{
return $this->_async;
}
/**
* Enables async sending for this message.
*
* @return \nickcv\mandrill\Message
* @since 1.3.0
*/
public function enableAsync()
{
$this->_async = true;
return $this;
}
/**
* Disables async sending the this message.
*
* @return \nickcv\mandrill\Message
* @since 1.3.0
*/
public function disableAsync()
{
$this->_async = false;
return $this;
}
/**
* Returns the from email address in this format:
*
* ~~~
* Sender Name <email@example.com>
* ~~~
*
* The default value for the sender name is the application name
* configuration parameter inside `config/web.php`.
*
* The default value for the sender address is the adminEmail parameter
* inside `config/params.php`.
*
* @see \nickcv\mandrill\Message::setFrom() setter
*
* @return string
*/
public function getFrom()
{
$from = null;
if ($this->getFromName()) {
$from .= $this->getFromName();
}
if ($this->getFromAddress()) {
$from .= $from === null ? $this->getFromAddress() : '<' . $this->getFromAddress() . '>';
}
return $from;
}
/**
* Sets the message sender.
*
* @see \nickcv\mandrill\Message::getFrom() getter
*
* @param string|array $from sender email address.
* You may specify sender name in addition to email address using format:
* `[email => name]`.
* If you don't set this parameter the application adminEmail parameter will
* be used as the sender email address and the application name will be used
* as the sender name.
* @return \nickcv\mandrill\Message
*/
public function setFrom($from)
{
if (is_string($from) && filter_var($from, FILTER_VALIDATE_EMAIL)) {
$this->_fromAddress = $from;
$this->_fromName = null;
}
if (is_array($from)) {
$address = key($from);
$name = array_shift($from);
if (!filter_var($address, FILTER_VALIDATE_EMAIL)) {
return $this;
}
$this->_fromAddress = $address;
if (is_string($name) && strlen(trim($name)) > 0) {
$this->_fromName = trim($name);
} else {
$this->_fromName = null;
}
}
return $this;
}
/**
* Returns an array of email addresses in the following format:
*
* ~~~
* [
* 'email1@example.com', //in case no recipient name was submitted
* 'email2@example.com' => 'John Doe', //in case a recipient name was submitted
* ]
* ~~~
*
* @see \nickcv\mandrill\Message::setTo() setter
*
* @return array
*/
public function getTo()
{
return $this->_to;
}
/**
* Sets the message recipient(s).
*
* @see \nickcv\mandrill\Message::getTo() getter
*
* @param string|array $to receiver email address.
* You may pass an array of addresses if multiple recipients should receive this message.
* You may also specify receiver name in addition to email address using format:
* `[email => name]`.
* @return \nickcv\mandrill\Message
*/
public function setTo($to)
{
$this->storeEmailAddressesInContainer($to, '_to');
return $this;
}
/**
* Returns an array of email addresses in the following format:
*
* ~~~
* [
* 'email1@example.com', //in case no recipient name was submitted
* 'email2@example.com' => 'John Doe', //in case a recipient name was submitted
* ]
* ~~~
*
* @see \nickcv\mandrill\Message::setReplyTo() setter
*
* @return array
*/
public function getReplyTo()
{
return $this->_replyTo;
}
/**
* Sets the message recipient(s).
*
* @see \nickcv\mandrill\Message::getReplyTo() getter
*
* @param string|array $replyTo Reply-To email address.
* You may pass an array of addresses if multiple recipients should receive this message.
* You may also specify receiver name in addition to email address using format:
* `[email => name]`.
* @return \nickcv\mandrill\Message
*/
public function setReplyTo($replyTo)
{
$this->storeEmailAddressesInContainer($replyTo, '_replyTo');
return $this;
}
/**
* Returns an array of email addresses in the following format:
*
* ~~~
* [
* 'email1@example.com', //in case no recipient name was submitted
* 'email2@example.com' => 'John Doe', //in case a recipient name was submitted
* ]
* ~~~
*
* @see \nickcv\mandrill\Message::setCc() setter
*
* @return array
*/
public function getCc()
{
return $this->_cc;
}
/**
* Sets the message recipient(s).
*
* @see \nickcv\mandrill\Message::getCc() getter
*
* @param string|array $cc cc email address.
* You may pass an array of addresses if multiple recipients should receive this message.
* You may also specify receiver name in addition to email address using format:
* `[email => name]`.
* @return \nickcv\mandrill\Message
*/
public function setCc($cc)
{
$this->storeEmailAddressesInContainer($cc, '_cc');
return $this;
}
/**
* Returns an array of email addresses in the following format:
*
* ~~~
* [
* 'email1@example.com', //in case no recipient name was submitted
* 'email2@example.com' => 'John Doe', //in case a recipient name was submitted
* ]
* ~~~
*
* @see \nickcv\mandrill\Message::setBcc() setter
*
* @return array
*/
public function getBcc()
{
return $this->_bcc;
}
/**
* Sets the message recipient(s).
*
* @see \nickcv\mandrill\Message::getBcc() getter
*
* @param string|array $bcc bcc email address.
* You may pass an array of addresses if multiple recipients should receive this message.
* You may also specify receiver name in addition to email address using format:
* `[email => name]`.
* @return \nickcv\mandrill\Message
*/
public function setBcc($bcc)
{
$this->storeEmailAddressesInContainer($bcc, '_bcc');
return $this;
}
/**
* Returns the html-encoded subject.
*
* @see \nickcv\mandrill\Message::setSubject() setter
*
* @return string
*/
public function getSubject()
{
return $this->_subject;
}
/**
* Sets the message subject.
*
* @see \nickcv\mandrill\Message::getSubject() getter
*
* @param string $subject
* The subject will be trimmed and html-encoded.
* @return \nickcv\mandrill\Message
*/
public function setSubject($subject)
{
if (is_string($subject)) {
$this->_subject = trim(Html::encode($subject));
}
return $this;
}
/**
* Returns the html-purified version of the raw text body.
*
* @see \nickcv\mandrill\Message::setTextBody() setter
*
* @return string
*/
public function getTextBody()
{
return $this->_text;
}
/**
* Sets the raw text body.
*
* @see \nickcv\mandrill\Message::getTextBody() getter
*
* @param string $text
* The text will be purified.
* @return \nickcv\mandrill\Message
*/
public function setTextBody($text)
{
if (is_string($text)) {
$this->_text = HtmlPurifier::process($text);
}
return $this;
}
/**
* Returns the html purified version of the html body.
*
* @see \nickcv\mandrill\Message::setHtmlBody() setter
*
* @return string
*/
public function getHtmlBody()
{
return $this->_html;
}
/**
* Sets the html body.
*
* @see \nickcv\mandrill\Message::getHtmlBody() getter
*
* @param string $html
* The html will be purified.
* @return \nickcv\mandrill\Message
*/
public function setHtmlBody($html)
{
if (is_string($html)) {
$this->_html = $html;
}
return $this;
}
/**
* Returns the attachments array.
*
* @see \nickcv\mandrill\Message::attach() setter for file name
* @see \nickcv\mandrill\Message::attachContent() setter for binary
*
* @return array
*/
public function getAttachments()
{
return $this->_attachments;
}
/**
* Attaches existing file to the email message.
*
* @see \nickcv\mandrill\Message::getAttachments() getter
*
* @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 \nickcv\mandrill\Message
*/
public function attach($fileName, array $options = [])
{
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 \nickcv\mandrill\Message::getAttachments() 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 \nickcv\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 images array.
*
* @see \nickcv\mandrill\Message::embed() setter for file name
* @see \nickcv\mandrill\Message::embedContent() setter for binary
*
* @return array list of embedded content
*/
public function getEmbeddedContent()
{
return $this->_images;
}
/**
* Embeds an image in the email message.
*
* @see \nickcv\mandrill\Message::getEmbeddedContent() getter
*
* @param string $fileName 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 \nickcv\mandrill\Message
*/
public function embed($fileName, array $options = [])
{
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;
}
/**
* Embed a binary as an image in the message.
*
* @see \nickcv\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 \nickcv\mandrill\Message
*/
public function embedContent($content, array $options = [])
{
$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;
}
/**
* Sets the data to be used by the Mandrill template system.
*
* @param string $templateName
* @param array $templateContent
* @param string $templateLanguage
*
* @return \nickcv\mandrill\Message
* @since 1.3.0
*/
public function setTemplateData($templateName, array $templateContent = [], $templateLanguage = self::LANGUAGE_MAILCHIMP)
{
$this->_templateName = $templateName;
if ($templateLanguage === self::LANGUAGE_MAILCHIMP) {
$this->_templateContent = $this->convertParamsForTemplate($templateContent);
} elseif ($templateLanguage === self::LANGUAGE_HANDLEBARS) {
$this->setGlobalMergeVars($templateContent);
}
$this->_mergeLanguage = $templateLanguage;
return $this;
}
/**
* Returns the name of the mandrill template to be used.
*
* @return string
* @since 1.3.0
*/
public function getTemplateName()
{
return $this->_templateName;
}
/**
* Returns the dynamic content used to replace blocks in the template.
*
* @return array
* @since 1.3.0
*/
public function getTemplateContent()
{
return $this->_templateContent;
}
/**
* Enable the use of template defaults.
*
* @return \nickcv\mandrill\Message
* @since 1.4.0
*/
public function enableTemplateDefaults()
{
$this->_ = true;
return $this;
}
/**
* Disable the use of template defaults.
*
* @return \nickcv\mandrill\Message
* @since 1.4.0
*/
public function disableTemplateDefaults()
{
$this->_ = false;
return $this;
}
/**
* Returns the global merge vars that will be submitted to mandrill.
*
* @return array
* @since 1.4.0
*/
public function getGlobalMergeVars()
{
return $this->_globalMergeVars;
}
/**
* Adds the given merge vars to the global merge vars array.
* Merge vars are case insensitive and cannot start with _
*
* @param array $mergeVars
*
* @return \nickcv\mandrill\Message
* @since 1.4.0
*/
public function setGlobalMergeVars(array $mergeVars)
{
foreach ($mergeVars as $name => $content) {
if ($name{0} === '_') {
continue;
}
array_push($this->_globalMergeVars, [
'name' => $name,
'content' => $content
]);
}
return $this;
}
/**
* Returns the string representation of this message.
*
* @return string
*/
public function toString()
{
return $this->getSubject() . ' - Recipients:'
. ' [TO] ' . implode('; ', $this->getTo())
. ' [CC] ' . implode('; ', $this->getCc())
. ' [BCC] ' . implode('; ', $this->getBcc());
}
/**
* 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->getHtmlBody(),
'text' => $this->getTextBody(),
'subject' => $this->getSubject(),
'from_email' => $this->getFromAddress(),
'from_name' => $this->getFromName(),
'to' => $this->getAllRecipients(),
'track_opens' => true,
'track_clicks' => true,
'tags' => $this->_tags,
'merge_language' => $this->_mergeLanguage,
'global_merge_vars' => $this->_globalMergeVars,
'attachments' => $this->_attachments,
'images' => $this->_images,
];
}
/**
* Stores email addresses in a private variable.
*
* @param string|array $emailAddresses
* @param string $container
*/
private function storeEmailAddressesInContainer($emailAddresses, $container)
{
if (is_string($emailAddresses) && $this->isRecipientValid($emailAddresses, $container)) {
$this->{$container}[] = $emailAddresses;
}
if (is_array($emailAddresses)) {
foreach ($emailAddresses as $key => $value) {
$this->storeArrayEmailAddressInContainer($key, $value, $container);
}
}
}
/**
* Stores the email address coming from an array, correctly placing the
* recipient name if it exists.
*
* @param string|integer $key
* @param string $value
* @param string $container
*/
private function storeArrayEmailAddressInContainer($key, $value, $container)
{
$name = is_string($key) ? $value : null;
$singleAddress = is_string($key) ? $key : $value;
if ($this->isRecipientValid($singleAddress, $container)) {
if ($name) {
$this->{$container}[$singleAddress] = $name;
} else {
$this->{$container}[] = $singleAddress;
}
}
}
/**
* Checks if an email address is valid and that is not already present within
* the private attribute.
*
* @param string $emailAddress
* @param string $privateAttributeName
* @return boolean
*/
private function isRecipientValid($emailAddress, $privateAttributeName)
{
if (!filter_var($emailAddress, FILTER_VALIDATE_EMAIL)) {
return false;
}
if (array_search($emailAddress, $this->{$privateAttributeName}) !== false) {
return false;
}
if (array_key_exists($emailAddress, $this->{$privateAttributeName}) !== false) {
return false;
}
return true;
}
/**
* Checks that the tag is not already in the private attribute, that is not
* exceeding the 50 characters limit and that is not starting with an underscore.
*
* @param string $string
* @param string $privateAttributeName
* @return boolean
*/
private function isTagValid($string, $privateAttributeName)
{
if (array_search($string, $this->{$privateAttributeName}) !== false) {