Specifying Sender Details
An email must include information about who sent it. Usually this is managed by the From: address, however there are other options.
- From: – the address(es) of who wrote the message (required)
- Sender: – the address of the single person who sent the message (optional)
- Return-Path: – the address where bounces should go to (optional)
You must always include a From: address by using setFrom() on the message. Swift Mailer will use this as the default Return-Path: unless otherwise specified.
The Sender: address exists because the person who actually sent the email may not be the person who wrote the email. It has a higher precedence than the From: address and will be used as the Return-Path: unless otherwise specified.
Syntax for Addresses
If you only wish to refer to a single email address (for example your From: address) then you can just use a string.
$message->setFrom('some@address.tld');
If you want to include a name then you must use an associative array.
$message->setFrom(array('some@address.tld' => 'The Name'));
If you want to include multiple addresses then you must use an array.
$message->setTo(array('some@address.tld', 'other@address.tld'));
You can mix personalized (addresses with a name) and non-personalized addresses in the same list by mixing the use of associative and non-associative array syntax.
$message->setTo(array( 'recipient-with-name@example.org' => 'Recipient Name One', 'no-name@example.org', //Note that this is not a key-value pair 'named-recipient@example.org' => 'Recipient Name Two' ));
-
Setting the From: Address
A From: address is required and is set with the setFrom() method of the message. -
Setting the Sender: Address
A Sender: address specifies who sent the message and is set with the setSender() method of the message. -
Setting the Return-Path: (Bounce) Address
The Return-Path: address specifies where bounce notifications should be sent and is set with the setReturnPath() method of the message.