Before I explain how to send out email messages by using both the "sendmail" Unix application and the "mail()" PHP function, it'd be helpful to reintroduce the examples created in the previous chapter of the series. They demonstrated how to perform this same task by utilizing a local and a remote SMTP server. That being clarified, here's the first example, which shows how to send email through a local host. Take a look at it: // example on sending a basic email message with Swift Mailer (uses localhost as the SMTP transport)
// include required files require_once 'lib/swift_required.php';
// create the mail transport using the 'newInstance()' method $transport = Swift_SmtpTransport::newInstance('localhost', 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('Sending email with Swift Mailer via localhost') // specify the From argument ->setFrom(array('alejandro@domain.com' => 'Alejandro Gervasio')) // specify the To argument ->setTo(array('john@domain.com' =>' 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 if ($mailer->send($message)) { echo 'The message was sent successfully!'; } else { echo 'Error sending email message'; } Undeniably, understanding how the above script works is a pretty straightforward process. The most relevant part to analyze is the one that creates a SMTP transport via the "getInstance()" method of the "Swift_SmtpTransport" class. It specifies that the server to be used to send email messages will be a local host, running on port 25. Simple to code and read, right? Now, moving forward, here's another sample script that performs the same operation, but by swapping between a local and a remote server. Here it is: // example on sending a basic email message with Swift Mailer (uses multiple SMTP transports)
// include required files require_once 'lib/swift_required.php';
// create the mail transport using the 'newInstance()' method $transport = Swift_SmtpTransport::newInstance('localhost', 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('Sending email with Swift Mailer via localhost') // specify the From argument ->setFrom(array('alejandro@domain.com' => 'Alejandro Gervasio')) // specify the To argument ->setTo(array('john@domain.com' =>' 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 if ($mailer->send($message)) { echo 'The message was sent successfully!'; } else { echo 'Error sending email message'; }
// specify another SMTP transport using the 'setHost()' and 'setPort()' methods $transport = Swift_SmtpTransport::newInstance() ->setHost('mail.domain.com') ->setPort(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('Sending email with Swift Mailer via a remote host') // specify the From argument ->setFrom(array('alejandro@domain.com' => 'Alejandro Gervasio')) // specify the To argument ->setTo(array('john@domain.com' =>' 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 if ($mailer->send($message)) { echo 'The message was sent successfully!'; } else { echo 'Error sending email message'; } There you have it. Thanks to the clever use of the "setHost()" and "setPort()" methods shown previously, it's possible to use different transports to send email messages within the same program. Of course, the scenario shown above is rather unusual, but it's useful for illustrating how flexible Swift Mailer can be when it comes to switching between different SMTP servers. Well, now that you hopefully grasped the logic that drive the two previous examples, it's time to explore other features provided by the Swift Mailer library. So, in the next section I'm going to discuss how to use the library for sending email messages by using the PHP "mail()" function and the Unix "sendmail" program. As usual, to learn more about these interesting topics, click on the link below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|