SUBSCRIBE VIA RSS


Subscribe to our feed

Symfony Experts

Symfony Experts
If you have an urgent question for a symfony-related issue, this is the place to ask.

Topics

Stack Overflow


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 by in  Web Development   |  

7 Responses to Sending Email within Symfony

  1. Pingback: rpsblog.com » A week of symfony #46 (12-&gt;18 november 2007)

  2. Dominik says:

    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') ?>

  3. Pingback: Sử dụng swiftmailer để gửi email trong symfony - Symfony Vietnam

  4. Pingback: Symfony.es » Blog Archive » Una semana con Symfony #19 (12 - 18 noviembre 2007)

  5. Pingback: links for 2007-12-22 | xanders blog

  6. Mikhus says:

    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?

  7. Naveed Ahmad says:

    i need this.its very helpfull for newcomer..