From 0a3f4adc90d185ce0240ecd08693509f7fec6b4b Mon Sep 17 00:00:00 2001 From: lyubomir atanasov Date: Mon, 17 Jul 2017 16:26:29 +0300 Subject: [PATCH] exclude null values from payload --- .../MessageFormat/MessagePayloadV3.php | 43 +++++++++---------- .../MessageFormat/MessagePayloadV31.php | 43 ++++++++++++++----- 2 files changed, 52 insertions(+), 34 deletions(-) diff --git a/SwiftMailer/MessageFormat/MessagePayloadV3.php b/SwiftMailer/MessageFormat/MessagePayloadV3.php index 0e73c3d..ef66d24 100644 --- a/SwiftMailer/MessageFormat/MessagePayloadV3.php +++ b/SwiftMailer/MessageFormat/MessagePayloadV3.php @@ -22,9 +22,6 @@ public function getMailjetMessage(Swift_Mime_Message $message) { $contentType = $this->getMessagePrimaryContentType($message); $fromAddresses = $message->getFrom(); $fromEmails = array_keys($fromAddresses); - $toAddresses = $message->getTo(); - $ccAddresses = $message->getCc() ? $message->getCc() : []; - $bccAddresses = $message->getBcc() ? $message->getBcc() : []; $attachments = array(); $inline_attachments = array(); // Process Headers @@ -33,19 +30,7 @@ public function getMailjetMessage(Swift_Mime_Message $message) { if ($replyTo = $this->getReplyTo($message)) { $userDefinedHeaders = array_merge($userDefinedHeaders, array('Reply-To' => $replyTo)); } - // @TODO only Format To, Cc, Bcc - $to = ""; - foreach ($toAddresses as $toEmail => $toName) { - $to .= "$toName <$toEmail>"; - } - $cc = ""; - foreach ($ccAddresses as $ccEmail => $ccName) { - $cc .= "$toName <$toEmail>"; - } - $bcc = ""; - foreach ($bccAddresses as $bccEmail => $bccName) { - $bcc .= "$toName <$toEmail>"; - } + // Handle content $bodyHtml = $bodyText = null; if ($contentType === 'text/plain') { @@ -81,12 +66,20 @@ public function getMailjetMessage(Swift_Mime_Message $message) { } } } - $mailjetMessage = array( - 'FromEmail' => $fromEmails[0], - 'FromName' => $fromAddresses[$fromEmails[0]], - 'Subject' => $message->getSubject(), - 'Recipients' => $this->getRecipients($message) - ); + $mailjetMessage = array(); + $recipients = $this->getRecipients($message); + if (count($recipients) > 0) { + $mailjetMessage['Recipients'] = $recipients; + } + if (!is_null($fromEmails[0])) { + $mailjetMessage['FromEmail'] = $fromEmails[0]; + } + if (!is_null($message->getSubject())) { + $mailjetMessage['Subject'] = $message->getSubject(); + } + if (!is_null($fromAddresses[$fromEmails[0]])) { + $mailjetMessage['FromName'] = $fromAddresses[$fromEmails[0]]; + } if (!is_null($bodyHtml)) { $mailjetMessage['Html-part'] = $bodyHtml; } @@ -165,7 +158,11 @@ protected function getRecipients(Swift_Mime_Message $message) { } $recipients = []; foreach ($to as $address => $name) { - $recipients[] = ['Email' => $address, 'Name' => $name]; + if (!is_null($name)) { + $recipients[] = ['Email' => $address, 'Name' => $name]; + } else { + $recipients[] = ['Email' => $address]; + } } return $recipients; } diff --git a/SwiftMailer/MessageFormat/MessagePayloadV31.php b/SwiftMailer/MessageFormat/MessagePayloadV31.php index ab02f5d..8145e7b 100644 --- a/SwiftMailer/MessageFormat/MessagePayloadV31.php +++ b/SwiftMailer/MessageFormat/MessagePayloadV31.php @@ -38,15 +38,27 @@ public function getMailjetMessage(Swift_Mime_Message $message) { //@TODO array_push is not recommended $to = array(); foreach ($toAddresses as $toEmail => $toName) { - array_push($to, ['Email' => $toEmail, 'Name' => $toName]); + if (!is_null($toName)) { + array_push($to, ['Email' => $toEmail, 'Name' => $toName]); + } else { + array_push($to, ['Email' => $toEmail]); + } } $cc = array(); foreach ($ccAddresses as $ccEmail => $ccName) { - array_push($cc, ['Email' => $ccEmail, 'Name' => $ccName]); + if (!is_null($ccName)) { + array_push($cc, ['Email' => $ccEmail, 'Name' => $ccName]); + } else { + array_push($cc, ['Email' => $ccEmail]); + } } $bcc = array(); foreach ($bccAddresses as $bccEmail => $bccName) { - array_push($bcc, ['Email' => $bccEmail, 'Name' => $bccName]); + if (!is_null($bccName)) { + array_push($bcc, ['Email' => $bccEmail, 'Name' => $bccName]); + } else { + array_push($bcc, ['Email' => $bccEmail]); + } } // Handle content @@ -86,21 +98,30 @@ public function getMailjetMessage(Swift_Mime_Message $message) { } } } - - $mailjetMessage = array( - 'From' => array( - 'Email' => $fromEmails[0], - 'Name' => $fromAddresses[$fromEmails[0]] - ), - 'To' => $to, - 'Subject' => $message->getSubject(), + $mailjetMessage = array(); + $from = array( + 'Email' => $fromEmails[0], + 'Name' => $fromAddresses[$fromEmails[0]] ); + + if (!empty($from)) { + if (is_null($from['Name'])) { + unset($from['Name']); + } + $mailjetMessage['From'] = $from; + } + if (!empty($to)) { + $mailjetMessage['To'] = $to; + } if (!empty($cc)) { $mailjetMessage['Cc'] = $cc; } if (!empty($bcc)) { $mailjetMessage['Bcc'] = $bcc; } + if (!is_null($message->getSubject())) { + $mailjetMessage['Subject'] = $message->getSubject(); + } if (!is_null($bodyHtml)) { $mailjetMessage['HTMLPart'] = $bodyHtml; }