Sending multipart email from a Task in symfony 1.4

I recently needed to send the result of an action in symfony via email daily. E-mail has changed slightly in symfony 1.3/1.4 – here’s how I did it. I really don’t like my solution at all, the use of get_partial() from within a Task seems very wrong to me – but I’m in a rush so it will do for now, I’d love to hear feedback

The mail has a plain text, and HTML part to it. It generates the contents of the mail by rendering a partial, which requires a context to be set up. I modify sf_format by changing the request format on the request.

class nospOverusagereportTask extends sfBaseTask
  protected function configure()
      new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name', 'frontend'),
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
      new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'radius'),

    $this->namespace        = 'nosp';
    $this->name             = 'over-usage-report';
    $this->briefDescription = '';
    $this->detailedDescription = 'moooo :)';

  protected function execute($arguments = array(), $options = array())
    // initialize the database connection
    $databaseManager = new sfDatabaseManager($this->configuration);
    $connection = $databaseManager->getDatabase($options['connection'] ? $options['connection'] : null)->getConnection();

    $form = new RadiusOverUsageForm();
      'percent' => 125,
      'payg'    => true,
    $results = $form->getResults();

    // create a context, and load the helper
    $context = sfContext::createInstance($this->configuration);

    // create the message
    $message = $this->getMailer()->compose('[email protected]', '[email protected]', 'Subject Line');

    // generate HTML part
    $html  = get_partial('radius/overusage', array('results' => $results));
    $message->setBody($html, 'text/html');

    // generate plain text part
    $plain = get_partial('radius/overusage', array('results' => $results));
    $message->addPart($plain, 'text/plain');

    // send the message

4 thoughts on “Sending multipart email from a Task in symfony 1.4”

  1. Just to make it clear. You MUST set the application name in the options as this isn’t by default and will cause $this->configuration to be an instance of ProjectConfiguration not ApplicationConfiguration.

  2. Thanks for your piece of code, it helped me.

    By the way, I got warnings when instanciating the context object. It said that the headers were already sent. The problem is that the context instanciation makes a call to session_start, which sends headers to the client. So if you have any output before context instanciation, like log messages (my case), you’ll get these warnings.

    The solution is to instanciate the context object at the beginning of the task, before any output.

    Thanks again.

  3. @ian: thanks for the post, very helpful and well referenced

    @Harry: funny to find you back here. Your comment was key to understand the problem I was just having, not loading the correct parameters. Thanks 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.