Skip to content

Commit a98753f

Browse files
author
danil zakablukovskii
committed
cleanup
1 parent c71e4c1 commit a98753f

File tree

1 file changed

+106
-74
lines changed

1 file changed

+106
-74
lines changed

Message.php

+106-74
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class Message extends BaseMessage
8989
/**
9090
* Headers other than "Subject", "From", "To", and "Reply-To":
9191
* [
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
9393
* ]
9494
* @var array
9595
*/
@@ -251,21 +251,23 @@ public function setFrom($from)
251251
*/
252252
public function getTo()
253253
{
254-
if (isset($this->_to['list_id'])) {
254+
if ($this->isListUsed()) {
255255
return [$this->_to['list_id']];
256256
}
257257

258258
$addresses = [];
259259
foreach ($this->_to as $item) {
260+
$item = $item['address'];
261+
260262
// skip recipients with set header_to, i.e. CC and BCC recipients
261263
if (isset($item['header_to'])) {
262264
continue;
263265
}
264266

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'];
267269
} else {
268-
$addresses[] = $item['address']['email'];
270+
$addresses[] = $item['email'];
269271
}
270272
}
271273

@@ -295,7 +297,7 @@ public function setTo($to)
295297
* @param string $listId Stored recipients list id.
296298
* @return $this
297299
*/
298-
public function setStoredRecipientsList($listId)
300+
public function setRecipientsListId($listId)
299301
{
300302
$this->_to = ['list_id' => $listId];
301303

@@ -321,11 +323,9 @@ public function getReplyTo()
321323
*/
322324
public function setReplyTo($replyTo)
323325
{
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;
329329

330330
return $this;
331331
}
@@ -336,21 +336,15 @@ public function setReplyTo($replyTo)
336336
*/
337337
public function getCc()
338338
{
339+
if ($this->isListUsed()) {
340+
return [];
341+
}
342+
339343
$addresses = [];
340344
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'];
350346

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)) {
354348
continue;
355349
}
356350

@@ -395,21 +389,15 @@ public function setCc($cc)
395389
*/
396390
public function getBcc()
397391
{
392+
if ($this->isListUsed()) {
393+
return [];
394+
}
395+
398396
$addresses = [];
399397
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'];
409399

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)) {
413401
continue;
414402
}
415403

@@ -463,6 +451,22 @@ public function setSubject($subject)
463451
return $this;
464452
}
465453

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+
466470
/**
467471
* Sets message plain text content.
468472
* @param string $text message plain text content.
@@ -487,6 +491,22 @@ public function setHtmlBody($html)
487491
return $this;
488492
}
489493

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+
490510
/**
491511
* Attaches existing file to the email message.
492512
* @param string $fileName full file name
@@ -611,22 +631,6 @@ public function toString()
611631
. ' [BCC] ' . implode('; ', $this->getBcc());
612632
}
613633

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-
630634
/**
631635
* @return boolean
632636
*/
@@ -765,22 +769,6 @@ public function setUseDraftTemplate($useDraftTemplate)
765769
$this->_useDraftTemplate = $useDraftTemplate;
766770
}
767771

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-
784772
/**
785773
* Prepares the message and gives it's array representation to send it through SparkSpot API
786774
* @see \SparkPost\Transmission::send()
@@ -823,17 +811,22 @@ public function toSparkPostArray()
823811
*/
824812
protected function addRecipient($emails, $copy = false)
825813
{
814+
// unset possible used list id, if user is going to set recipients
815+
unset($this->_to['list_id']);
816+
826817
if (is_string($emails)) {
827818
$emails = [$emails];
828819
}
829820

830821
foreach ($emails as $email => $name) {
831822
if (is_int($email)) {
832-
$address['email'] = $name;
823+
$address = [
824+
'email' => trim($name),
825+
];
833826
} else {
834827
$address = [
835-
'name' => $name,
836-
'email' => $email,
828+
'name' => trim($name),
829+
'email' => trim($email),
837830
];
838831
}
839832

@@ -854,9 +847,12 @@ private function emailsToString($emails)
854847
{
855848
$addresses = [];
856849
foreach ($emails as $email => $name) {
850+
$name = trim($name);
851+
857852
if (is_int($email)) {
858853
$addresses[] = $name;
859854
} else {
855+
$email = trim($email);
860856
$addresses[] = "\"{$name}\" <{$email}>";
861857
}
862858
}
@@ -879,10 +875,12 @@ private function prepareCopyRecipients()
879875
}
880876
}
881877

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+
}
886884
}
887885
}
888886
}
@@ -898,4 +896,38 @@ private function getBinaryMimeType($content)
898896

899897
return $finfo->buffer($content);
900898
}
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+
}
901933
}

0 commit comments

Comments
 (0)