SUBSCRIBE VIA RSS


Subscribe to our feed

Status Updates

  • First iPhone app nearly complete! I've been waiting for 3 weeks for Apple to finish "conducting company identity verification."
    3 days ago
  • Rainy day! Staying indoors. Creating computerized players in a simulation game that models cooperation in the work place.
    75 days ago
  • Up at 8AM, with a hot cup of coffee brought back from Cafe Lola in Ann Arbor. Will I succeed in putting in 8 billable hours in one day?
    100 days ago
  • full scale irish band outside my window playing the pipes for the past 20 minutes. pretty fun.
    109 days ago
  • listening to Kristin on Maxim Radio!
    115 days ago
  • back from an amazing meal at ouest with even more amazing company.
    115 days ago
  • wondering how so many online tshirt companies stay in business--ones that sell shirts with funny messages, i never see them in real life
    122 days ago

Topics

TWITTER

The old fashioned way

RECENT TUNES

November 16, 2007 – 4:57pm Sending Email within Symfony

Symfony allows you to dish off the responsibility of sending an email to another module/action when necessary, which can be great because it can utilize the “view” aspect of the framework so you can store your email formatting (plain text or HTML) as a template just like you would for a regular action.

As I don’t use this functionality that often, I usually forget how to pass parameters over to this other mail action, such as the id of the object I am working with or the recipient’s email address.

Sending Email with a Custom Action
In this example we’ll be sending an lease application notice to the manager of a property. After the application is saved, we want to send an email to the property manager containing the property name and applicant name, and include a link for them to login and view the complete application.

In our “save” action of the “leaseApp” module, right after the application is saved we’ll send off the email, and we can do this (as outlined in the symfony cookbook) by delegating to another action.

$application->save();
// set some parameters we want to reference in the email
$this->getRequest()->setParameter('application', array(
  'id'=>$application->getId(),
  'recipient'=>$application->getManagerEmail(),
  'property'=>$application->getProperty()->getAddress()
));
// notify property manager of new application
$raw_email = $this->sendEmail('mail', 'sendApplicationNotice');  
// optionally log the raw email data to our debug log
$this->logMessage($raw_email, 'debug');

It’s really easy to create this new “mail” module that is responsible for all the sending of emails from your web application. Just initialize the module as you would for any other:

symfony init-module frontend mail

Now, create a new action within the module (taken from the symfony cookbook)

  public function executeSendApplicationNotice()
  {
    $this->application = $this->getRequestParameter('application');
 
    // class initialization
    $mail = new sfMail();
    $mail->setCharset('utf-8');      
 
    // definition of the required parameters
    $mail->setSender('webmaster@my-company.com', 'My Company webmaster');
    $mail->setFrom('webmaster@my-company.com', 'My Company webmaster');
    $mail->addReplyTo('webmaster_copy@my-company.com');
 
    $mail->addAddress($this->application['recipient']);
 
    $mail->setSubject('Your password request');
 
    $this->mail = $mail;
  }
}

Now you can create the view, sendApplicationNoticeSuccess.php:

A new lease application was submitted.
 
Property: <?php echo $application['property'] ?>
 
Use this link to view the full application:
 
http://[url here]/application/show/id/<?php echo $application['id'] ?>
Posted in  Web Development   |     |  delicious  Digg

6 Responses to “Sending Email within Symfony”

  1. In the template/sendApplicationNoticeSuccess.php

    you can use the url_for helpers!

    for example:

    Use this link to view the full application:

    < ?php echo link_to('@your_route?id=XX') ?>

    By Dominik on Nov 19, 2007

  2. that’s all nice but how to be with a situation you need to have a special layout for your mail messages?
    Has anybody solved this problem?

    By Mikhus on Apr 29, 2008

4 Trackback(s)

  1. Nov 19, 2007: rpsblog.com » A week of symfony #46 (12-&gt;18 november 2007)
  2. Dec 2, 2007: Sử dụng swiftmailer để gửi email trong symfony - Symfony Vietnam
  3. Dec 3, 2007: Symfony.es » Blog Archive » Una semana con Symfony #19 (12 - 18 noviembre 2007)
  4. Dec 22, 2007: links for 2007-12-22 | xanders blog

Post a Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word