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

Review: sending basic email messages with Swift Mailer - 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



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)




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




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.

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