As I mentioned in the previous segment, though Swift Mailer uses PHP sockets internally to communicate directly with a SMTP host to send email messages, it also gives you the possibility to use the "mail()" PHP function to perform the same task.
If you're a big fan of this function and are wondering how to use it with Swift Mailer, here's an example. Look at it, please:
// example on sending a basic email message with Swift Mailer (uses mail() function as the SMTP transport)
// include required files
// create the mail transport using the 'newInstance()' method
$transport = Swift_SendmailTransport::newInstance();
// 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('Sending email with Swift Mailer via the mail() PHP function')
// specify the From argument
->setFrom(array('email@example.com' => 'Alejandro Gervasio'))
// specify the To argument
->setTo(array('firstname.lastname@example.org' =>' John Doe'))
// 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';
As you can see above, to use the "mail()" PHP function, the "getInstance()" method of the "Swift_SendmailTransport" class has to be called with no arguments. In doing so, the library will automatically utilize this function to send email, as you've done possibly hundreds of times in the past with your own scripts.
And with this last practical example, I'm finishing this third chapter of the series that explores the main features provided by the handy Swift Mailer library. Of course, you're free to edit all of the code samples that appear in this tutorial, so you can improve your skills when it comes to working with this powerful package.
Over this part of the series, I explained how to configure Swift Mailer to work seamlessly with the "sendmail" Unix program and the "mail()" PHP function as well. As you saw in previous examples, this process is extremely straightforward, so in theory you shouldn't have major trouble understanding how it functions.
In the next part, things will be even more interesting, since I'm going to discuss in detail how to use the library to send email messages with a carbonic copy (Cc).
Here's my final piece of advice: don't miss the following tutorial!
blog comments powered by Disqus