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

Extending the use of the Swift_SmtpTransport class - 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.

  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



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




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.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: