Code snippets for symfony 1.x


Refine Tags

Snippets tagged "swift"

How to send Emails from the model or from a task

For sf1.3/1.4 only.

It seems that the symfony mailer system is tied to the sfContext resource via the factory handler (correct me if I'm wrong).

It might be a problem when you want to send an email from one of your model methods because sfContext has no instance in cli mode.

It can also be an issue if you want to build a task that send emails. Sample: a nightly cron job that alert users by email when their free trial period is over.

The best way I've found is to provide an access to the mailer system linked to the ProjectConfiguration class.

Add a static protected "$mailer" variable and a static public method "getMailer()" :

class ProjectConfiguration extends sfProjectConfiguration
  static protected
    $mailer  = null # Symfony mailer system
   * Returns the project mailer
  static public function getMailer()
    if (null !== self::$mailer)
      return self::$mailer;
    // If sfContext has instance, returns the classic mailer resource
    if (sfContext::hasInstance() && sfContext::getInstance()->getMailer())
      self::$mailer = sfContext::getInstance()->getMailer();
      // Else, initialization
      if (!self::hasActive())
        throw new sfException('No sfApplicationConfiguration loaded');
      require_once sfConfig::get('sf_symfony_lib_dir').'/vendor/swiftmailer/classes/Swift.php';
      $applicationConfiguration = self::getActive();
      $config = sfFactoryConfigHandler::getConfiguration($applicationConfiguration->getConfigPaths('config/factories.yml'));
      self::$mailer = new $config['mailer']['class']($applicationConfiguration->getEventDispatcher(), $config['mailer']['param']);
    return self::$mailer;

Now if you want to send an email from a task or from a model class:

ProjectConfiguration::getMailer()->composeAndSend(/* your email params*/);

In your unit tests, if you want to test which emails have been sent:

$sentEmails = ProjectConfiguration::getMailer()->getLogger()->getMessages();


by Éric Rogé on 2009-12-11, tagged mailer  swift 

mail with symfony1.1 and swiftMailer

If you looking for an example to send an email from a tell-a-friend-form or other forms. this might help you:

function executeSend()
  [..] // your new sfForm-vodoo [..]
  // after posting form and validation
  $values = $this->form->getValues();
 // header
  $from = new Swift_Address( $values['email'], $values['firstname'] . ' ' . $values['lastname'] );
  $to   = new Swift_address( $values['rcpt_email'], $values['rcpt_name']);
  // subject/message
  $msg  = new Swift_Message( __( '', '', 'tellafriend') ); // at this moment no body-text etc, we want multipart ...
  // content
  $placeholder = array();
  foreach ( $values as $key => $val ) {
    $placeholder['%'.$key.'%'] = $val;
  // stuff, we need in the email
  sfContext::getInstance()->getRequest()->setAttribute( 'placeholder', $placeholder );
  // textversion
  $msg->attach(new Swift_Message_Part( sfContext::getInstance()->getController()->getPresentationFor( 'tellafriend', 'sendEmailText', $viewName = null) ));
  // htmlversion
  $msg->attach(new Swift_Message_Part( sfContext::getInstance()->getController()->getPresentationFor( 'tellafriend', 'sendEmailHTML', $viewName = null), "text/html") );
  $stream = $msg->build();
  //echo $stream->readFull(); die; //Dumps the email contents
  // and send
  $swift = new Swift(new Swift_Connection_NativeMail());
  $swift->send( $msg, $to, $from );
function executeSendEmailText()
  // holds your text-version-template
  $this->placeholder = $this->getRequest()->getAttribute('placeholder');
function executeSendHTMLText()
  // holds your html-version-template
  $this->placeholder = $this->getRequest()->getAttribute('placeholder');

full documentation for swiftMailer you find here:

by Susan Lau on 2008-04-15, tagged sfmail  swift  symfony11