Skip to content

Commit

Permalink
Merge pull request #2 from mailjet/handle_null_values
Browse files Browse the repository at this point in the history
exclude null values from payload
  • Loading branch information
Lyubomir Atanasov authored Jul 17, 2017
2 parents 84d59fd + 0a3f4ad commit ff53c48
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 34 deletions.
43 changes: 20 additions & 23 deletions SwiftMailer/MessageFormat/MessagePayloadV3.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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') {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
43 changes: 32 additions & 11 deletions SwiftMailer/MessageFormat/MessagePayloadV31.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit ff53c48

Please sign in to comment.