How to send emails in symfony 1.1
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
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
It also supports differents ways of sending mail (smtp, mail(), sendmail) and things like attachments and multi-part messages.
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)
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)
You can read this on http://www.symfony-project.org/cookbook/1_1/email