In the previous section, I built a script that showed how to specify the “Cc” argument for a sample email message. So, the only thing that remains undone is coding the part that actually sends the message, along with the corresponding carbon copy.
Below I included the complete source code of the script, this time adding the block that calls the “send()” method. Look at it, please:
// example on sending a basic email message with Swift Mailer (uses the 'setCc()' 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('firstname.lastname@example.org' => 'Alejandro Gervasio'))
// specify the To argument
->setTo(array('email@example.com' =>' John Doe'))
// specify the Cc argument
->setCc(array('firstname.lastname@example.org' => 'Mary Jackson'))
// 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';
Definitely, the above script is starting to reveal the actual power that stands behind the Swift Mailer library, since it shows how to send a single carbon copy of an email message to a hypothetical recipient through an extremely intuitive and compact API. Again, at the risk of being repetitive, it’s valid to mention here the convenience of using chainable methods in PHP 5, which certainly permits you to build scripts that are both quite readable and shorter in length.
Feel free to edit and improve all of the examples included in this article, so you can practice using the handy “setCc()” method provided by Swift Mailer.
That’s all for the moment. In this fourth part of the series you learned how easy it is to send carbon copies of an email message with the Swift Mailer library, thanks to its “setCc()” method.
Of course, it’s natural to think that if the library can work with carbon copies, it’ll also allow us to use blind carbon copies. And you’d be right; Swift Mailer comes with another method, called “setBcc(),” that performs this task painlessly.
If you want to learn how to use this new method, then you can’t miss the next tutorial!
blog comments powered by Disqus