Home arrow PHP arrow Page 4 - Using Different SMTP Transports with Swift Mailer

Swapping out between SMTP servers - PHP

In this second part of the series, I develop a couple of examples that demonstrate how easy it is to send email messages by using different SMTP servers, thanks to the versatility provided by the Swift Mailer PHP library.

TABLE OF CONTENTS:
  1. Using Different SMTP Transports with Swift Mailer
  2. Review: sending basic email messages with Swift Mailer
  3. Extending the use of the Swift_SmtpTransport class
  4. Swapping out between SMTP servers
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
February 11, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: