Sending to Several Recipients

This is much the same as sending a basic email. You simply add more addresses to the array of setTo().

First you need to include the “swift_required.php” file, then you create an instance of the Mailer using any of the Transports (probably Swift_SmtpTransport, Swift_SendmailTransport or Swift_MailTransport). Then you create a a message and send it with the Mailer.

<?php
 
//Include this needed file
require_once '/path/to/swift/lib/swift_required.php';
 
//Start the mailer
$mailer = new Swift_Mailer(new Swift_SmtpTransport('smtp.your-isp.tld'));
 
//Create a message
$message = Swift_Message::newInstance('Your subject', 'Your Message')
  ->setFrom(array('your@address.tld' => 'Your Name'))
  ->setTo(array(
    'someone@address.tld' => 'Person name',
    'other@address.tld' => 'Other name',
    'more@address.tld' => 'Yet another name'
  ));
 
//Send it
$mailer->send($message);

This will result in a single email being sent to all of those addresses, where the “To:” header shows all addresses.

Subject: Your subject
From: Your Name <your@address.tld>
To: Person name <someone@address.tld>, Other name <other@address.tld>, Yet another name <more@address.tld>
-------------------------------------

Your message

If you want to include some Cc recipients then call setCc()...

//Create a message
$message = Swift_Message::newInstance('Your subject', 'Your Message')
  ->setFrom(array('your@address.tld' => 'Your Name'))
  ->setTo(array(
    'someone@address.tld' => 'Person name',
    'other@address.tld' => 'Other name',
    'more@address.tld' => 'Yet another name'
  ))
  ->setCc(array(
    'copied@address.tld' => 'Copied recipient',
    'another-copy@address.tld' => 'Another copy'
  ));

This results in the following message sent to all recipients:

Subject: Your subject
From: Your Name <your@address.tld>
To: Person name <someone@address.tld>, Other name <other@address.tld>, Yet another name <more@address.tld>
Cc: Copied recipient <copied@address.tld>, Another copy <another-copy@address.tld>
-------------------------------------

Your message

And finally, if you want to include some Bcc recipients you need to call setBcc()...

//Create a message
$message = Swift_Message::newInstance('Your subject', 'Your Message')
  ->setFrom(array('your@address.tld' => 'Your Name'))
  ->setTo(array(
    'someone@address.tld' => 'Person name',
    'other@address.tld' => 'Other name',
    'more@address.tld' => 'Yet another name'
  ))
  ->setBcc(array(
    'copied@address.tld' => 'Copied recipient',
    'another-copy@address.tld' => 'Another copy'
  ));

Unlike Cc recipients, Bcc recipients are hidden from everyone but themselves so what happens here is that all recipients in the “To:” field get this message:

Subject: Your subject
From: Your Name <your@address.tld>
To: Person name <someone@address.tld>, Other name <other@address.tld>, Yet another name <more@address.tld>
-------------------------------------

Your message

And each recipient in the Bcc field gets their own message:

Subject: Your subject
From: Your Name <your@address.tld>
To: Person name <someone@address.tld>, Other name <other@address.tld>, Yet another name <more@address.tld>
Bcc: Copied recipient <copied@address.tld>
-------------------------------------

Your message
Subject: Your subject
From: Your Name <your@address.tld>
To: Person name <someone@address.tld>, Other name <other@address.tld>, Yet another name <more@address.tld>
Bcc: Yet another copy <another-copy@address.tld>
-------------------------------------

Your message
 
v4/examples/multiple_recipients.txt · Last modified: 2008/05/22 06:15 by d11wtq
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki