Tricks and Snippets
Sending multipart email from a Task in Symfony 1.4
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);
}
}
| Stampa l'articolo | Questo articolo è stato pubblicato da admin il 26 agosto 2010 alle 07:02, ed è archiviato come PHP, Programmazione. Puoi seguire i commenti a questo post attraverso RSS 2.0. Puoi pubblicare un commento o segnalare un trackback dal tuo sito. |