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

Mark
about 10 hours after
Awesome work! I really liked that feature in 1.0 and I think this is just an awesome enhancement.

Keep up the good work. :)

Mark.
Jérôme
about 11 hours after
Hi Thomas,

Any reason for not using swiftMailer ?

http://www.symfony-project.org/blog/2008/06/10/how-to-send-emails-in-symfony-1-1

sfSwiftPlugin is available for symfony 1.0.
Thomas
about 12 hours after
@jerome : I just don't want to use too many external library. Zend has a very good support and provide many set of class (other than Zend_Mail) which are very nice. sfSwiftPlugin does not allow global configuration of the transport class and there is not decorator option. Final words from this post : http://prendreuncafe.com/blog/post/2008/09/17/Request-For-Comments%3A-sending-emails-in-symfony-12
Danny
about 21 hours after
How I configure "Zend_Mail_Transport_Smtp"

Thanks
Jérôme
1 day after
@Thomas
You don't want to use too many external library but you actually do with Zend_Mail and swToolBoxPlugin :)

Is decorator option so important ? Using sfActions::getPartial with swiftMailer looks ok to me.

Why global configuration of the transport is so important ?

My concerns about swToolBoxPlugin is that it's a Doctrine-based plugin which provides cool features but outside the specific scope of sending emails.
Thomas
11 days after
@danny : I just update the plugin documentation and update the code to use Zend_Mail_Transport_Smtp

@jerome : the decorator option can be useful to share a common header or footer. Some swToolboxPlugin features are bound to Doctrine, but not the main ones.
Jpatrick
8 months after
This information is incomplete. Don't specified hostname.....
=(

Add comment











A la recherche de projets symfony ?

Tags

Last posts