If you've ever put your hands on a Unix-based machine, the you may know that it includes a popular program called "sendmail," which can be used for sending email. Fortunately, the developer team behind Swift Mailer has incorporated into the library the ability to work seamlessly with this program, instead of using a different SMTP transport.
To demonstrate how to use "sendmail" with Swift Mailer, below I coded a script similar to the one that you learned previously. It shows how to perform this operation in a simple way. Here's the script in question:
// example on sending a basic email message with Swift Mailer (uses sendmail as the SMTP transport)
// include required files
// create the mail transport using the 'newInstance()' method
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/exim -bs');
// 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 sendmail')
// 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, dispatching an email message through the "sendmail" program doesn't different too much from using another SMTP transport. In this case, the transport object has been created by using a new class called "Swift_SendmailTransport" and by specifying a common path to the program. It's actually that easy.
The rest of the script remains nearly the same, so I don't want to waste more of your time explaining how it works. Instead, the next thing I'm going to teach you in this tutorial is how to code another script that will use the "mail()" PHP function to send a basic email message.
Want to see how this script will be created? Then click on the link below and read the following section.
blog comments powered by Disqus