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:
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(‘email@example.com’ => ‘John Doe’))
->setTo(array(‘firstname.lastname@example.org’, ‘email@example.com’ => ‘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”
Nice and easy. I like it. For non-CakePHP projects I will definitely consider it. Thanks.
How is that simple compared to:
$result = mail(
‘firstname.lastname@example.org, A name ‘,
‘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();
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.
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.
Nice post, thanks. Nice colour theme as well. Is it public somewhere?
I foud something strange, the swiftmailer can not work on centos 6.0 but works on ubuntu, anyone knows why?
using sendmail function