9951 explained code solutions for 126 technologies


php-swiftmailerHow to add CC to an email using Swiftmailer?


Adding CC to an email using Swiftmailer is a simple process.

Example code

$message = (new \Swift_Message('Hello World'))
  ->setFrom(['[email protected]' => 'John Doe'])
  ->setTo(['[email protected]', '[email protected]' => 'A name'])
  ->setCc(['[email protected]'])
  ->setBody('Here is the message itself')
;

The code above will add a CC to the email.

Code explanation

  • \Swift_Message('Hello World') - creates a new Swift_Message object with the subject 'Hello World'
  • setFrom(['[email protected]' => 'John Doe']) - sets the sender of the email
  • setTo(['[email protected]', '[email protected]' => 'A name']) - sets the primary recipients of the email
  • setCc(['[email protected]']) - sets the CC recipients of the email
  • setBody('Here is the message itself') - sets the body of the email

Helpful links

Edit this code on GitHub