@@ -89,7 +89,7 @@ class Message extends BaseMessage
89
89
/**
90
90
* Headers other than "Subject", "From", "To", and "Reply-To":
91
91
* [
92
- * 'Cc' => string, // must be set (only if a template isn't used) to mark some recipients as CC recipients
92
+ * 'Cc' => string, // will be set (only if a template isn't used) to mark some recipients as CC recipients
93
93
* ]
94
94
* @var array
95
95
*/
@@ -251,21 +251,23 @@ public function setFrom($from)
251
251
*/
252
252
public function getTo ()
253
253
{
254
- if (isset ( $ this ->_to [ ' list_id ' ] )) {
254
+ if ($ this ->isListUsed ( )) {
255
255
return [$ this ->_to ['list_id ' ]];
256
256
}
257
257
258
258
$ addresses = [];
259
259
foreach ($ this ->_to as $ item ) {
260
+ $ item = $ item ['address ' ];
261
+
260
262
// skip recipients with set header_to, i.e. CC and BCC recipients
261
263
if (isset ($ item ['header_to ' ])) {
262
264
continue ;
263
265
}
264
266
265
- if (isset ($ item ['address ' ][ ' name ' ])) {
266
- $ addresses [$ item ['address ' ][ ' email ' ]] = $ item[ ' address ' ] ['name ' ];
267
+ if (isset ($ item ['name ' ])) {
268
+ $ addresses [$ item ['email ' ]] = $ item ['name ' ];
267
269
} else {
268
- $ addresses [] = $ item ['address ' ][ ' email ' ];
270
+ $ addresses [] = $ item ['email ' ];
269
271
}
270
272
}
271
273
@@ -295,7 +297,7 @@ public function setTo($to)
295
297
* @param string $listId Stored recipients list id.
296
298
* @return $this
297
299
*/
298
- public function setStoredRecipientsList ($ listId )
300
+ public function setRecipientsListId ($ listId )
299
301
{
300
302
$ this ->_to = ['list_id ' => $ listId ];
301
303
@@ -321,11 +323,9 @@ public function getReplyTo()
321
323
*/
322
324
public function setReplyTo ($ replyTo )
323
325
{
324
- if (is_string ($ replyTo )) {
325
- $ this ->_replyTo = $ replyTo ;
326
- } elseif (is_array ($ replyTo )) {
327
- $ this ->_replyTo = $ this ->emailsToString ($ replyTo );
328
- }
326
+ $ this ->_replyTo = is_array ($ replyTo ) ?
327
+ $ this ->emailsToString ($ replyTo ) :
328
+ $ replyTo ;
329
329
330
330
return $ this ;
331
331
}
@@ -336,21 +336,15 @@ public function setReplyTo($replyTo)
336
336
*/
337
337
public function getCc ()
338
338
{
339
+ if ($ this ->isListUsed ()) {
340
+ return [];
341
+ }
342
+
339
343
$ addresses = [];
340
344
foreach ($ this ->_to as $ item ) {
341
- if (is_string ($ item ['address ' ])) {
342
- continue ;
343
- } else {
344
- $ item = $ item ['address ' ];
345
- }
346
-
347
- if (!isset ($ item ['header_to ' ])) {
348
- continue ;
349
- }
345
+ $ item = $ item ['address ' ];
350
346
351
- // fixme: better way to find substring
352
- // if email is not represented in Cc header - it's the Bcc email
353
- if (!isset ($ this ->_headers ['Cc ' ]) || strpos ($ this ->_headers ['Cc ' ], $ item ['email ' ]) === false ) {
347
+ if (!$ this ->isCopyRecipient ($ item )) {
354
348
continue ;
355
349
}
356
350
@@ -395,21 +389,15 @@ public function setCc($cc)
395
389
*/
396
390
public function getBcc ()
397
391
{
392
+ if ($ this ->isListUsed ()) {
393
+ return [];
394
+ }
395
+
398
396
$ addresses = [];
399
397
foreach ($ this ->_to as $ item ) {
400
- if (is_string ($ item ['address ' ])) {
401
- continue ;
402
- } else {
403
- $ item = $ item ['address ' ];
404
- }
405
-
406
- if (!isset ($ item ['header_to ' ])) {
407
- continue ;
408
- }
398
+ $ item = $ item ['address ' ];
409
399
410
- // fixme: better way to find substring
411
- // if email is represented in the Cc header, it's not the Bcc email
412
- if (isset ($ this ->_headers ['Cc ' ]) && strpos ($ this ->_headers ['Cc ' ], $ item ['email ' ]) !== false ) {
400
+ if (!$ this ->isCopyRecipient ($ item , true )) {
413
401
continue ;
414
402
}
415
403
@@ -463,6 +451,22 @@ public function setSubject($subject)
463
451
return $ this ;
464
452
}
465
453
454
+ /**
455
+ * @return string
456
+ */
457
+ public function getTemplateId ()
458
+ {
459
+ return $ this ->_templateId ;
460
+ }
461
+
462
+ /**
463
+ * @param string $templateId
464
+ */
465
+ public function setTemplateId ($ templateId )
466
+ {
467
+ $ this ->_templateId = $ templateId ;
468
+ }
469
+
466
470
/**
467
471
* Sets message plain text content.
468
472
* @param string $text message plain text content.
@@ -487,6 +491,22 @@ public function setHtmlBody($html)
487
491
return $ this ;
488
492
}
489
493
494
+ /**
495
+ * @return string
496
+ */
497
+ public function getRfc822 ()
498
+ {
499
+ return $ this ->_rfc822 ;
500
+ }
501
+
502
+ /**
503
+ * @param string $rfc822
504
+ */
505
+ public function setRfc822 ($ rfc822 )
506
+ {
507
+ $ this ->_rfc822 = $ rfc822 ;
508
+ }
509
+
490
510
/**
491
511
* Attaches existing file to the email message.
492
512
* @param string $fileName full file name
@@ -611,22 +631,6 @@ public function toString()
611
631
. ' [BCC] ' . implode ('; ' , $ this ->getBcc ());
612
632
}
613
633
614
- /**
615
- * @return string
616
- */
617
- public function getTemplateId ()
618
- {
619
- return $ this ->_templateId ;
620
- }
621
-
622
- /**
623
- * @param string $templateId
624
- */
625
- public function setTemplateId ($ templateId )
626
- {
627
- $ this ->_templateId = $ templateId ;
628
- }
629
-
630
634
/**
631
635
* @return boolean
632
636
*/
@@ -765,22 +769,6 @@ public function setUseDraftTemplate($useDraftTemplate)
765
769
$ this ->_useDraftTemplate = $ useDraftTemplate ;
766
770
}
767
771
768
- /**
769
- * @return string
770
- */
771
- public function getRfc822 ()
772
- {
773
- return $ this ->_rfc822 ;
774
- }
775
-
776
- /**
777
- * @param string $rfc822
778
- */
779
- public function setRfc822 ($ rfc822 )
780
- {
781
- $ this ->_rfc822 = $ rfc822 ;
782
- }
783
-
784
772
/**
785
773
* Prepares the message and gives it's array representation to send it through SparkSpot API
786
774
* @see \SparkPost\Transmission::send()
@@ -823,17 +811,22 @@ public function toSparkPostArray()
823
811
*/
824
812
protected function addRecipient ($ emails , $ copy = false )
825
813
{
814
+ // unset possible used list id, if user is going to set recipients
815
+ unset($ this ->_to ['list_id ' ]);
816
+
826
817
if (is_string ($ emails )) {
827
818
$ emails = [$ emails ];
828
819
}
829
820
830
821
foreach ($ emails as $ email => $ name ) {
831
822
if (is_int ($ email )) {
832
- $ address ['email ' ] = $ name ;
823
+ $ address = [
824
+ 'email ' => trim ($ name ),
825
+ ];
833
826
} else {
834
827
$ address = [
835
- 'name ' => $ name ,
836
- 'email ' => $ email ,
828
+ 'name ' => trim ( $ name) ,
829
+ 'email ' => trim ( $ email) ,
837
830
];
838
831
}
839
832
@@ -854,9 +847,12 @@ private function emailsToString($emails)
854
847
{
855
848
$ addresses = [];
856
849
foreach ($ emails as $ email => $ name ) {
850
+ $ name = trim ($ name );
851
+
857
852
if (is_int ($ email )) {
858
853
$ addresses [] = $ name ;
859
854
} else {
855
+ $ email = trim ($ email );
860
856
$ addresses [] = "\"{$ name }\" < {$ email }> " ;
861
857
}
862
858
}
@@ -879,10 +875,12 @@ private function prepareCopyRecipients()
879
875
}
880
876
}
881
877
882
- foreach ($ this ->_to as &$ recipient ) {
883
- if (isset ($ recipient ['address ' ]['header_to ' ])) {
884
- $ recipient ['address ' ]['header_to ' ] = str_replace ('%mainRecipient ' , $ main ,
885
- $ recipient ['address ' ]['header_to ' ]);
878
+ if ($ main ) {
879
+ foreach ($ this ->_to as &$ recipient ) {
880
+ if (isset ($ recipient ['address ' ]['header_to ' ])) {
881
+ $ recipient ['address ' ]['header_to ' ] = str_replace ('%mainRecipient ' , $ main ,
882
+ $ recipient ['address ' ]['header_to ' ]);
883
+ }
886
884
}
887
885
}
888
886
}
@@ -898,4 +896,38 @@ private function getBinaryMimeType($content)
898
896
899
897
return $ finfo ->buffer ($ content );
900
898
}
899
+
900
+ private function isListUsed ()
901
+ {
902
+ return isset ($ this ->_to ['list_id ' ]);
903
+ }
904
+
905
+ /**
906
+ * Checks whether the $recipient is a copy (CC) recipient or BCC recipient, if $checkBcc is true.
907
+ * CC differs from BCC: CC has email presented in 'Cc' header
908
+ *
909
+ * @param array $recipient ['email' => $email]
910
+ * @param bool $checkBcc do we check whether the recipient is a CC recipient or BCC
911
+ * @return bool
912
+ */
913
+ private function isCopyRecipient ($ recipient , $ checkBcc = false )
914
+ {
915
+ if (!isset ($ recipient ['header_to ' ])) {
916
+ return false ;
917
+ }
918
+
919
+ $ result = $ checkBcc ? true : false ;
920
+ $ ccRecipients = explode (', ' , ArrayHelper::getValue ($ this ->_headers , 'Cc ' , []));
921
+ foreach ($ ccRecipients as $ ccRecipient ) {
922
+ if (preg_match ("/^ {$ recipient ['email ' ]}$|< {$ recipient ['email ' ]}>$/ " , $ ccRecipient )) {
923
+ // email is presented in Cc header
924
+
925
+ // if we search Bcc, then if email is presented in Cc header - it's not a Bcc email
926
+ $ result = $ checkBcc ? false : true ;
927
+ break ;
928
+ }
929
+ }
930
+
931
+ return $ result ;
932
+ }
901
933
}
0 commit comments