I am trying to get a Laravel Mail setup to correctly send emails with CC’d recipients.
$email_addresses = ['[email protected]','[email protected]','[email protected]']
$to_email = array_shift($email_addresses);
Mail::mailer('mandrill')
->to([$to_email])
->cc($email_addresses)
->send(new PortalEmail($message));
But it is sending an email to every address in the list of CC’d emails, with no CC’d recipients. Instead sends all of the CC’d address recipients a copy of the email, with the email address as the to
email address.
2
Answers
You are correctly setting the to recipient to the first email address in the list using ->to($to_email). Then, you can pass the remaining email addresses to the cc method using ->cc($email_addresses).
This is a feature of Mandrill. You can either change this setting globally in your account, or you can pass in a custom header to enforce the setting per message.
Global account setting
The documentation for changing the setting globally is at https://mailchimp.com/developer/transactional/docs/outbound-email/#multiple-recipients.
From the docs:
Per-message override
To override this global setting on a per-message basis, you can send your email with the custom
X-MC-PreserveRecipients
header.To do this, add a
headers()
function to yourPortalEmail
mailable:This will turn on the
preserve_recipients
setting so that Mandrill will not modify theTo
/Cc
headers.Documentation for the customer header is at https://mailchimp.com/developer/transactional/docs/smtp-integration/#x-mc-preserverecipients.
From the docs:
Documentation on Laravel mail headers is at https://laravel.com/docs/10.x/mail#headers.