skip to Main Content

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


  1. 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).

    $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));
    
    Login or Signup to reply.
  2. 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:

    To control whether recipients can see each others’ information in the To header of your messages, navigate to the Sending Defaults page and select or unselect Expose The List Of Recipients When Sending To Multiple Addresses. You can override these global settings on a per-message basis with the preserve_recipients parameter in your API calls, or with the X-MC-PreserveRecipients SMTP header.

    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 your PortalEmail mailable:

    use IlluminateMailMailablesHeaders;
     
    public function headers(): Headers
    {
        return new Headers(
            text: [
                'X-MC-PreserveRecipients' => 'true',
            ],
        );
    }
    

    This will turn on the preserve_recipients setting so that Mandrill will not modify the To/Cc headers.


    Documentation for the customer header is at https://mailchimp.com/developer/transactional/docs/smtp-integration/#x-mc-preserverecipients.

    From the docs:

    If you send to more than one recipient at a time and want all recipients to see each other’s information, set this option to true. If it’s set to false, Mailchimp will rewrite the To and Cc headers to only show information about an individual recipient.


    Documentation on Laravel mail headers is at https://laravel.com/docs/10.x/mail#headers.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search