As you'll recall from the previous section, it's necessary to add to the earlier script the block of code that really sends out the email message, along with the specified copies.
So, with that requirement in mind, below I listed the script's full source code, this time including the part that calls the "send()" method. Here it is:
// example on sending a basic email message with Swift Mailer (uses the 'setCc() and 'setBcc() methods')
// 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' =>' John Doe'))
->setCc(array('email@example.com' => 'Mary Jackson'))
->setBcc(array('firstname.lastname@example.org' => 'Susan Norton'))
// build the body part of the message
->setBody('Hey, how are you? I am sending you a message with the cool Swift Mailer library');
// send the email message
echo 'The message was sent successfully!';
echo 'Error sending email message';
There you have it. Thanks to the clean and tight interface provided by Swift Mailer, it's possible to send multiple copies of an email message to a certain number of recipients. And regardless of the additional tasks that may be required by a particular web application, sending email with Swift Mailer is reduced most of the time to creating first the appropriate SMTP transport object, then composing the message with the additional copies, and finally dispatching it via the "send()" method. Voila!
Feel free to edit and enhance all of the code samples included in this tutorial, which surely will arm you with a better background in using this powerful email library.
In this fifth part of the series, you expanded your background in the Swift Mailer library, since you learned how to use it for sending blind carbon copies of a basic email message to a predefined recipient. The process was as simple as using its "setBcc()" method and nothing else.
Now, moving forward, it's time to talk about the topics that I plan to cover in the next article, so you know what to expect from it. Now that you learned how to send basic plain-text messages with Swift Mailer, in the following tutorial I'm going to discuss how to handle attachments in a very simple and intuitive manner.
Don't miss the upcoming part!
blog comments powered by Disqus