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



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
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