Sending mails is a web developer's everyday task, and symfony 1.1 let you do this easier than ever using Swift Mailer.
Swift Mailer is a well thought, fully featured PHP5 object library that will cover 120% of your mailing needs.
It is available in a tagged SVN repository, so your project won't break just because the library is updated. It will be up to you to switch to a newer version.
Symfony's way to send emails from a project is very simple. You create a partial or a component that will render the e-mail content, and use Swift to send it in a flexible way.
Installing
If your project is already using svn, you can install it using the svn:externals
property:
$ cd /path/to/symfony/project
$ mkdir -p lib/vendor
$ svn propedit svn:externals .
Then add the following line:
swift http://swiftmailer.svn.sourceforge.net/svnroot/swiftmailer/tags/php5/3.3.3/lib/
If your project is not using SVN, you can still get this part as a subversion working copy by checking out the tag.
$ cd /path/to/symfony/project
$ mkdir -p lib/vendor
$ svn checkout http://swiftmailer.svn.sourceforge.net/svnroot/swiftmailer/tags/php5/3.3.3/lib/ swift
Just clear your cache to force class autoloading resolution to be flushed, and you're done with the installation.
Creating a send mail action
As of symfony 1.1 RC2, you can easily get rendered partials and components from an action:
$mailBody = $this->getPartial('mailBody', array('name' => 'John Doe'));
or
$mailBody = $this->getComponent('mail', 'mailBody', array('name' => 'John Doe'));
Then we send the mail with Swift:
try { /* * Create the mailer and message objects */ $mailer = new Swift(new Swift_Connection_NativeMail()); $message = new Swift_Message('Mail\'s subject', $mailBody, 'text/html'); /* * Send */ $mailer->send($message, $mailTo, $mailFrom); $mailer->disconnect(); } catch (Exception $e) { $mailer->disconnect(); // handle errors there }
Sending a multipart email
Some emails are a little more complex than just a partial, and that include multipart emails. The example below sends an email containing both an HTML and a plain text version.
try { /* * Create the mailer and message objects */ $mailer = new Swift(new Swift_Connection_NativeMail()); $message = new Swift_Message('Test mail subject'); /* * Render message parts */ $mailContext = array('name' => 'John Doe'); $message->attach(new Swift_Message_Part($this->getPartial('mail/mailHtmlBody', $mailContext), 'text/html')); $message->attach(new Swift_Message_Part($this->getPartial('mail/mailTextBody', $mailContext), 'text/plain')); /* * Send */ $mailer->send($message, $mailTo, $mailFrom); $mailer->disconnect(); } catch (Exception $e) { $mailer->disconnect(); // handle errors there }
This is a very basic example, but you can do much more with multipart mails, by adding inline images or attaching files. More informations is available in the symfony 1.1 cookbook.
Getting documented
The SwiftMailer website if a gold mine of documentation and tutorials.
And of course, the full API reference is available if you need it.
More information and examples are available in the symfony 1.1 cookbook
Another nice mailer is PHPMailer. It's not as object oriented as Swift is by far, but a bit simpler to use by the looks of it.
It also supports differents ways of sending mail (smtp, mail(), sendmail) and things like attachments and multi-part messages.
Swift of course supports all thoose ways too, and even more complex logic like rotating connections to have fallbacks in case one is not available.
I'll add a chapter on the cookbook about this tomorrow, but everything is of course described in Swift documentation (look at "The connections" chapter on Swift documentation index page)
PHPMailer stinks. There are much better solutions out there, Zend_Mail, ezcMail and Swift Mailer, to name some.
@Alex: PHPMailer was the library used by symfony 1.0 and it's still packaged with symfony 1.1. That said, PHPMailer is unmaintained, not flexible enough, and rather buggy. Swift Mailer is a great library, PHP5 oriented, with great features and a great extensibility. It really fits nicely with symfony.
Thanks for the blog Romain. It is good to see these helpful blogs coming regularly. I'll certainly be needing to use email in my 1.1 project so I look forward to trying Swift Mailer. But saying it will cover 120% of my needs does give the impression you are not completely objective :)
@IsRobot: Swift Mailer does cover 120% of your needs because it has some fancy features, many other PHP mailer libraries doesn't have. Take a deeper look into Swift and you'll see that becomes awesome at some point ;)
Great - just in time. Im using PHPMailer in some old code and needed an alternative to move forward with my project. :-)
Nice blog activity.
quick question though, doesn't the getPartial support the format/mime-type stuff that is described in the iPhone blog post?
$this->getPartial('body', $c) gets _body.php (html partial) $this->getPartial('body', $c, 'txt') gets _body.txt.php (plain text partial)
The cookbook article has been updated to include different connection settings and how to use gmail as sender-agent.
You can read this on http://www.symfony-project.org/cookbook/1_1/email