Skip to content

Commit

Permalink
Merge pull request #146 from nickvanderveeken/bugfix/sparkpost-transm…
Browse files Browse the repository at this point in the history
…ission-call-sends-invalid-header-section

Fix SparkPost Transmission calls that failed with error 422 due to invalid content of the 'headers' section
  • Loading branch information
roelvanduijnhoven authored Sep 13, 2021
2 parents bee33da + 503886f commit 80c2e34
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/Service/SparkPostService.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,25 @@ protected function prepareFromAddress(Message $message): array
*/
protected function prepareRecipients(Message $message): array
{
#if ($this->getEnvelope() && $this->getEnvelope()->getTo()) {
# return (array) $this->getEnvelope()->getTo();
#}

$recipients = [];
$recipients['recipients'] = $this->prepareAddresses($message->getTo(), $message);
//preparing email recipients we set $recipients['xx'] to be equal to prepareAddress() for different messages
!($cc = $this->prepareAddresses($message->getCc(), $message)) || $recipients['cc'] = $cc;
!($bcc = $this->prepareAddresses($message->getBcc(), $message)) || $recipients['bcc'] = $bcc;

$message->getTo()->rewind();
$firstToAddress = $message->getTo()->current()->getEmail();

$ccRecipients = $this->prepareAddresses($message->getCc(), $message);

foreach($ccRecipients as $ccRecipient) {
$ccRecipient['address']['header_to'] = $firstToAddress;
$recipients['recipients'][] = $ccRecipient;
}

$bccRecipients = $this->prepareAddresses($message->getBcc(), $message);

foreach ($bccRecipients as $bccRecipient) {
$bccRecipient['address']['header_to'] = $firstToAddress;
$recipients['recipients'][] = $bccRecipient;
}

return $recipients;
}
Expand Down Expand Up @@ -237,12 +247,17 @@ protected function prepareHeaders(Message $message): array
'Reply-To',
'Content-Type',
'Content-Transfer-Encoding',
'MIME-Version',
];

foreach ($removeTheseHeaders as $headerName) {
$headers->removeHeader($headerName);
}

if ($message->getCc()->count() === 0) {
$headers->removeHeader('Cc');
}

return $headers->toArray();
}

Expand Down

0 comments on commit 80c2e34

Please sign in to comment.