2009-01-26 - Send Mail with symfony 1.2

Introduction

Symfony 1.0 has introduced a nice feature : a quick and a simple way to generate email. This feature has been deprecated from symfony 1.1 and symfony 1.2, maybe the mailer option was not flexible enough [1].

swToolboxPlugin introduces a new sendEmail method which works almost as the old symfony 1.0 version but with better options :
  - Email decorator
  - Zend_Mail Support
  - Charset and Encoding
  - variables assignement from the controller

This solution is based on Zend_Mail for more information please refer to the Zend Framework documentation available at : http://framework.zend.com/manual/en/zend.mail.html

Installation

1. Download Zend Framework (this part can be funny but Zend Framework is more a set of very good library than a framework.)
 - zipped version at at http://www.zend.com/community/downloads
 - svn version at http://framework.zend.com/svn/framework/standard/tags/release-1.7.2/library/Zend
  
   The lib must be installed into SF_ROOT_DIR/lib/vendor/

2. Install swToolboxPlugin
 - svn version at http://svn.symfony-project.com/plugins/swToolboxPlugin/sf1.2/trunk/
 - there is not package version for now

3. Edit the app.yml file and add the following configuration

  swToolbox:
    mail:
      charset: utf-8                             # charset to use : utf-8, iso-8859-1, ...
      encoding: quoted-printable                 # 7bit, 8bit, quoted-printable, base64 (default : quoted-printable)
      transport:                                 # define which transport class to used
        class: Zend_Mail_Transport_Sendmail      # Zend_Mail_Transport_Sendmail | Zend_Mail_Transport_Smtp
        parameters:
      decorator:                                 # define the layout use in the mail
        enabled: off                             # on | off : set true if all your mail share the same layout
        directory: %SF_APP_TEMPLATE_DIR%         # where the layout is located, ie %SF_APP_TEMPLATE_DIR%
        template: email                          # name of the layout, automatically translate to name.FORMAT.php
      view:                                      # define the view class used to render the template
        class: swMailView

Usage

invoicesActions.class.php

class invoicesActions extends sfActions
{
  public finction executeConfirmPaiement(sfWebRequest $request)
  {
    // [...]
   
    // create object in your controller
    $invoice = Doctrine::getTable('Invoice')->find(..);
   
    // call the email action
    $this->sendMail('yourModule', 'sendInvoice', array('invoice' => $invoice);
  }
}

centralMailsActions.class.php

class centralMailsActions extends sfActions
{
  public function executeSendInvoice(sfWebRequest $request)
  {
    $mail = new swMail;
   
    $user = $this->invoice->getUser();

    $mail->setSubject('Your Invoice #'.$this->invoice->getReference());
    $mail->setFrom('billing@yoursite.com', 'Billing Service');
    $mail->addTo($user->getEmail(), $user->getName());
   
    $this->mail = $mail;
  }
}

You should have a least one template file : sendInvoiceSuccess.txt.php or sendInvoiceSuccess.html.php in your
module/templates folder.

If you want a specific layout around your email (like default header and footer), enable the decorator option
and create a 'email.txt.php' and/or 'email.html.php' inside the decorator directory. That's just work as a standard template.

Conclusion

That's all for now, hope you like this feature, I will explain more about this plugin in the next coming week.

References

Comments

comments powered by Disqus