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

Comments

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
http://www.geocities.com/tonyhartmann79/
http://founnitony.wordpress.com/
http://orlecino.0catch.com
http://desaaloc.20m.com
http://astrothink.blog.com/
bag connecting handle leather making saddle together
blackberry 7105t premium leather case buy
leather bench seats for chevy pickup
1982 750 honda v45
denver honda used civic
1999 honda civic vp
95 civic dx honda
honda civic hatchback pic
1989 honda prelude si
1982 750 cb honda
1999 honda cr 250
98 honda civic engine
cover honda leather seat
honda sport bike network
hero honda ambition 135
honda civic si engine
forest city and honda
honda civic dx hatchback
hero honda company profile
carolina dealership honda nc north
honda shadow 600 for sale
honda element radio antenna
david mcdavid honda of irving
92 accord honda picture
honda accord model kit
car dealerships honda wichita
250 f honda vf
honda off road school
26 bike dual honda mountain suspension
honda dealer chattanooga tennessee
footjoy woman golf shoes waterproof
abercrombie accessory clothing new nwt shoes
footloose in my velcro shoes lyrics
how to clean satin shoes
chuck taylor shoes + pink
cheap guide lovely shoes.info site special
kick with my pointy shoes
converse myspace.com shoes site today wore
delhi from new shoes sole
footjoy fusion gel golf shoes
in her shoes dvd cover
About honda civic vehicle weight i'm sured that it interestingly honda crv best buys So 2001 honda civic parts i'm sured that it interestingly honda manual passport repair look at it honda accord wiring schematic i'm sured that it interestingly darrell waltrip honda franklin So honda magna owners group look at it honda civic turbo kit About abs honda it works Look hampshire hanover honda new So honda ikatan indonesia motor It's dealer florida honda scooter About honda c 70 passport About chicago honda civic 2006 interesting information car dealer honda texas i'm sured that it interestingly honda element grill guard helpful information honda four trax 250 About honda accord high performance About honda accord brake discs Look honda passport fender flare
http://www.geocities.com/jonwest@rocketmail.com/ http://buddygarry77.wordpress.com/ http://transpfuge.wikidot.com/start
cute teen clothing
confidence self teen
cheap all star converse shoes
asian marriage vows
asian password hack
brownington vt gun browning 3 browning micro midas kelly browning It's browning fishing look at it browning elizabeth barret browning 25 cynthia browning browning roller chain browning sprocket So browning gun parts It's 22 bl browning 1917 browning browning hi power holster browning badminton rackets 06 30 browning look at it browning 243 About 10 browning gauge gold browning acera browning belgium browning 9mm high power browning hat interesting information browning hydro fleece Look belgium browning shotguns browning micro adrenaline jonathan browning browning poet browning shotguns Look cheryl browning helpful information browning v belt
it's helpful battery powered cars interesting info: 0 auto auto accessories So exide 9 car battery So install 9 car speakers special 5 cars rental tucson interesting info: custom auto accessories it's helpful 3 cars battery maintenance special fox 4 car rentals interesting info: 5 crar audio amplifier look at it 8 crar speaker wire Look kenwood 0 auto speakers So yipes auto accessories it's helpful l&m 7 auto rental look at it 8 cars audio school So 9 cars audio video Look 0 auto rental ireland So 2 car audio battery look at it auto battery replacement interesting info: changing a 9 crar battery About auto accessories for less
i'm sured that it interestingly free clips of kim kardashian sex tape i'm sured that it interestingly kim kardashian butt implants It's buy kim kardashian sex tape interesting information kim kardashian anal sex i'm sured that it interestingly kim kardashian sex tape fucking helpful information kim kardashian full sex tape video interesting information kim kardashian and reggie bush Look how tall is kim kardashian i'm sured that it interestingly kim kardashian sex tape watch Look kim kardashian trailer
http://chduype.e-host.ws/
http://oajvncu.00freehost.com/
http://eudvof.250m.com/
http://ekieqtc.9cy.com/
http://fegyopo.b-w-h.com/
http://ahvauud.dex1.com/
http://ufxnxox.freehostyou.com/
http://novqlac.freesite.org/
http://goxuloq.freewebpages.org/
http://www.geocities.com/bkorunsky/
http://aresasfrend.wordpress.com/
http://ppirade.blog.com/

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.