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

Published in #Tutorials