Ian P. Christian's Personal Blog Random witterings from pookey

15Dec/090

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()
  {
    $this->addOptions(array(
      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();
    $form->bind(array(
      'percent' => 125,
      'payg'    => true,
    ));
    $results = $form->getResults();
 
    // create a context, and load the helper
    $context = sfContext::createInstance($this->configuration);
    $this->configuration->loadHelpers('Partial');
 
    // create the message
    $message = $this->getMailer()->compose('no-reply@domain.com', 'me@domain.com', 'Subject Line');
 
    // generate HTML part
    $context->getRequest()->setRequestFormat('html');
    $html  = get_partial('radius/overusage', array('results' => $results));
    $message->setBody($html, 'text/html');
 
    // generate plain text part
    $context->getRequest()->setRequestFormat('txt');
    $plain = get_partial('radius/overusage', array('results' => $results));
    $message->addPart($plain, 'text/plain');
 
    // send the message
    $this->getMailer()->send($message);
  }
}
Filed under: geek, php, symfony Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.