As I expressed in the introduction, the Swift Mailer library includes another method that allows you to send email messages, but in a slightly different way. What does it do differently from its counterpart “send()”? It'll simply send a completely separate message to each recipient specified within the “To” header. This means the targeted recipient will see only their email address, which can be really useful in certain situations.
To show how to use this method, below I modified the script developed in the previous section, so you can grasp how it works. Here it is:
// example on sending a basic email message with Swift Mailer (uses the batchSend() method)
// include required files
// create the mail transport using the 'newInstance()' method
$transport = Swift_SmtpTransport::newInstance('mail.domain.com', 25)
// create the mailer using the 'newInstance()' method
$mailer = Swift_Mailer::newInstance($transport);
// create a simple message using the 'newInstance()' method
$message = Swift_Message::newInstance()
// specify the subject of the message
->setSubject('Testing Swift Mailer')
// specify the From argument
->setFrom(array('email@example.com' => 'Alejandro Gervasio'))
// specify the To argument
->setTo(array('firstname.lastname@example.org', 'email@example.com' => 'Hello from here'))
// build the body part of the message
->setBody('I am sending an email message with the cool Swift Mailer library');
// send the email message
echo 'The message was sent successfully!';
echo 'Error sending email message';
Even though the script that sends the above email looks nearly identical to the previous ones, in this case my friends Susan and Mary will see only their email addresses when receiving the message (and hopefully, this will make them feel a bit less jealous, too).
Having already spotted the subtle differences between using the “send()” and “batchSend()” methods, it’s time for the last part of this tutorial. I’ll be coding a final script, which will show how to specify a return path when sending emails.
Please go ahead and read the last section.
blog comments powered by Disqus