SwiftMailer – Sending Mail in PHP With Ease

The other day someone was looking for help with sending emails in PHP. I’ll be honest, the methodology for sending mail in PHP is haphazard to say the least. At least with the built in functionality. The documentation is lacking and there are a lot of undocumented pitfalls.

This is where SwiftMailer comes in.

SwiftMailer is an OOP based PHP5 library for sending email. It supports multiple SMTP servers, batch sending mail, etc. It doesn’t require any php.ini configurations, and its very straight forward. If there are errors, it throws exceptions instead of returning weird functionality. I highly recommend it for sending email in PHP. There are other mailer libraries, however I really like SwiftMailer the best. It has great documentation, and every PHP developer should check it out.

Here is an example on sending an email:


setUsername(‘your username’)
->setPassword(‘your password’)

You could alternatively use a different transport such as Sendmail or Mail:

$transport = Swift_SendmailTransport::newInstance(‘/usr/sbin/sendmail -bs’);

$transport = Swift_MailTransport::newInstance();

//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

//Create a message
$message = Swift_Message::newInstance(‘Wonderful Subject’)
->setFrom(array(‘john@doe.com’ => ‘John Doe’))
->setTo(array(‘receiver@domain.org’, ‘other@domain.org’ => ‘A name’))
->setBody(‘Here is the message itself’)

//Send the message
$result = $mailer->send($message);

You can alternatively use batchSend() to send the message

$result = $mailer->batchSend($message);



There you go! Its pretty darn slick. Hope it helps anyone out there.

7 thoughts on “SwiftMailer – Sending Mail in PHP With Ease

  1. Nice and easy. I like it. For non-CakePHP projects I will definitely consider it. Thanks.


  2. How is that simple compared to:
    $result = mail(
    ‘receiver@domain.org, A name ‘,
    ‘Wonderful Subject’,
    ‘Here is the message itself’,
    ‘From: John Doe ‘);

    Unless you need the advanced features like batch mailing using a heavy duty library with atrocious syntax, stick to mail();


  3. I’m not a PHP developer (anymore. Stopped in 1997 and never looked back.), but I’m a sysadmin and I have to maintain a few PHP sites. Why are there so many blasted PHP mail alternatives?

    Our developers were fighting with mail() and I found PEAR-Mail which installed easily on a CentOS system because it was available as a package in a standard yum repository.

    Isn’t PEAR supposed to be home of “community standard” packages? Why are so many devs still going out and installing third-party libraries like phpMailer and SwiftMailer? Better yet, why are they doing it on a per-project basis and not site-wide? Grrr.

    Sorry. A little venting here.


  4. @Lonnie,

    While I can kinda agree, for the very basic mail() works well, its when you start to leave the “basic” needs that I’ve fought mail(). I guess I’ve had enough battle wounds that I just prefer Swift now.


    I can understand your frustrations, and that is one of the “double edge sword” things about PHP, you have a LOT of choices, but it also can result in inconsistencies.

    When I do use a library like Swift, I tend to have a copy for each project, instead of having a dozen projects pointing to the same library. That can make upgrading a pain, but it saves me from upgrading Swift for one project, only to massively break the code on another project.


  5. Nice post, thanks. Nice colour theme as well. Is it public somewhere?


  6. I foud something strange, the swiftmailer can not work on centos 6.0 but works on ubuntu, anyone knows why?


  7. using sendmail function


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close