As I stated in the section you just read, Swift Mailer allows you to send email messages by using local and remote hosts, as demonstrated in a previous example. However, it's also possible to implement the two options within the same script by means of a couple of new methods provided by the "Swift_SmtpTransport" class, called "setHost()" and "setPort()" respectively. To illustrate how to use these methods, below I created a new script that sends the same email message first by using a local SMTP server, and then a remote host. Pay close attention to the following code sample, please: // 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'; } See how simple it is to send email using two different hosts? I guess you do. Notice also the utilization of the "setHost()" and "setPort()" methods, which permit you to perform the swapping process between servers in a pretty painless way. Of course, the example shown above should be considered pretty unusual, since normally only one server will be utilized. However, it's handy for illustrating, with just a few lines of code, the flexibility offered by Swift Mailer. Feel free to edit all of the code samples developed earlier, so you can arm yourself with a better background on using this handy email library. Final thoughts In this second episode of the series, I developed a couple of examples that hopefully demonstrated how easy it is to send email messages by using different SMTP servers, thanks to the versatility provided by Swift Mailer. However, the library offers other useful options for sending emails that may be particularly appealing to you. Thus, in the upcoming tutorial I'm going to discuss how to configure the already familiar "Swift_SmtpTransport" class to dispatch messages using the "sendmail" UNIX application and the native "mail()" PHP function. Don't miss the forthcoming article!
blog comments powered by Disqus |
|
|
|
|
|
|
|