Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing support for cc and bcc recipients #34

Merged
merged 4 commits into from
Sep 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/MandrillTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use MailchimpTransactional\ApiClient;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\AbstractTransport;
use Symfony\Component\Mime\Email;

class MandrillTransport extends AbstractTransport
{
Expand All @@ -16,6 +17,7 @@ class MandrillTransport extends AbstractTransport
*/
public function __construct(protected ApiClient $mailchimp, protected array $headers)
{
parent::__construct();
}

/**
Expand All @@ -28,9 +30,53 @@ protected function doSend(SentMessage $message): void
$this->mailchimp->messages->sendRaw([
'raw_message' => $message->toString(),
'async' => true,
'to' => $this->getTo($message),
]);
}

/**
* Retrieves recipients from the original message or envelope.
*
* @param SentMessage $message
* @return array
*/
protected function getTo(SentMessage $message): array
{
$recipients = [];

$original_message = $message->getOriginalMessage();

if ($original_message instanceof Email) {

if (!empty($original_message->getTo())) {
foreach ($original_message->getTo() as $to) {
$recipients[] = $to->getEncodedAddress();
}
}

if (!empty($original_message->getCc())) {
foreach ($original_message->getCc() as $cc) {
$recipients[] = $cc->getEncodedAddress();
}
}

if (!empty($original_message->getBcc())) {
foreach ($original_message->getBcc() as $bcc) {
$recipients[] = $bcc->getEncodedAddress();
}
}
}

// Fall-back to envelope recipients
if (empty($recipients)) {
foreach ($message->getEnvelope()->getRecipients() as $recipient) {
$recipients[] = $recipient->getEncodedAddress();
}
}

return $recipients;
}

/**
* Set headers of email.
*
Expand Down