Using Different SMTP Transports with Swift Mailer

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.

In case you still haven’t heard about it, Swift Mailer is a powerful object-oriented PHP library written by Chris Corbin and available at http://swiftmailer.org. It allows you to develop email applications in a truly effortless fashion by means of a set of comprehensive and well-structured classes that simplify the whole development process.

What’s more, one of the strongest points of Swift Mailer is its clever implementation of popular software design concepts. These include the proper use of the Factory pattern, object aggregation and method chaining, all features that make this library the choice of many PHP developers nowadays.

So, if you’re searching for a friendly guide that shows you how to take advantage of the numerous capabilities offered by Swift Mailer for sending email with PHP 5 the easy way, then start reading this series of articles right now!

Naturally, if you already had the chance to look at the first part of the series, then you already have a pretty clear idea of how to send basic email messages with Swift Mailer. In that tutorial I first explained how to use it to create a fictional SMTP transport, then to build a mailer object, and finally to dispatch a text message via its “send()” method. It was that simple, really.

Nevertheless, as I said before, Swift Mailer comes packaged with many other useful features that are worth looking at, including its ability to work with different email transports. In the next few lines I’m going to show you how to send email using both local SMTP and remote servers, so you can choose the option that best suits your needs.

Ready to continue this hopefully instructive tour of the Swift Mailer library? Then let’s get started!

{mospagebreak title=Review: sending basic email messages with Swift Mailer}

It’s possible you haven’t read the first chapter of this series, where I showed how to use the Swift Mailer library to send a simple email message by means of a remote SMTP server that required authentication. Therefore, below I reintroduced the script developed in that tutorial. It not only describes in a step-by-step fashion how to dispatch emails, but how to use a specific SMTP transport.

Having said that, here’s the entire source code corresponding to the script in question. Take a look at it, please:

// include required file

require_once ‘lib/swift_required.php';

 

// create the mail transport using the ‘newInstance()’ method

$transport = Swift_SmtpTransport::newInstance(‘mail.domain.com’, 25)

->setUsername(‘alejandro@domain.com’)

->setPassword(‘password’);

 

// 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(‘Testing Swift Mailer’)

// 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';

}

If you closely examine the above example, you’ll realize that my opinions on the way that Swift Mailer does its thing aren’t unfounded. As shown above, the steps required for sending an email message are extremely well-differentiated. They consist of creating the proper SMTP transport (in this particular case, a remote server that uses authentication), building a mailer object, and finally assembling the different parts of the message, which is sent via the “send()” method.

As I expressed in the introduction, the library correctly implements the Factory pattern when using the “newInstance()” method associated with each involved class. This permits you to chain different methods very easily, thus making the code much more compact.

Having reintroduced the example created in the first article of the series, it’s time to explore other useful features offered by Swift Mailer. So, as I explained at the beginning, the library allows you to send email by using different SMTP transports, either from local or remote servers. Of course, you already learned how to perform this process with a remote host, so in the following section I’m going to discuss how to create a transport based on a local server.

Please read the segment to come. I’ll be there, waiting for you.

{mospagebreak title=Extending the use of the Swift_SmtpTransport class}

Not surprisingly, Swift Mailer allows you to send email messages by using distinct SMTP servers very easily. This process is reduced to creating a new transport object with the proper arguments, and nothing else.

But this is only dull and boring theory, so let me give you a practical example that shows how to dispatch a basic email by utilizing a local host. Of course, the example assumes that a SMTP server has been previously installed on a local machine. Having clarified that point, its complete source code looks like this:

// 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';

}

As you can see, sending an email message by using a local SMTP server doesn’t differ too much from performing the same task with a remote host. The only difference rests on the first argument passed to the factory method of the “Swift_SmtpTransport” class, which in this case must be the “localhost” string.

The rest of the process of sending the email message remains nearly the same, so I’m not going to spend more time explaining how it works. Instead, I’m going to do something much more useful; in the last section of this article I’ll be demonstrating how to send emails by using different SMTP servers in the same script.

That sounds pretty interesting, right? If you wish to learn how this process will be accomplished, you’ll have to click on the link below and read the last segment.

{mospagebreak title=Swapping out between SMTP servers}

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!

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye