Skip to content

Instantly share code, notes, and snippets.

@cirpo
Created January 11, 2011 15:54
Show Gist options
  • Save cirpo/774602 to your computer and use it in GitHub Desktop.
Save cirpo/774602 to your computer and use it in GitHub Desktop.
cMailLogger Symfony mail logger
<?php
/**
* ncMAilLogger send mail logs messages.
*
* @package symfony
* @subpackage log
* @author alessandro cinelli a.k.a cirpo <alessandro.cinelli@gmail.com>
* @version SVN: $Id: sfFileLogger.class.php 10964 2008-08-19 18:33:50Z fabien $
*/
class cMailLogger extends sfLogger
{
protected
$mailer,
$dispatcher,
$recipients,
$sender,
$message;
public function initialize(sfEventDispatcher $dispatcher, $options = array())
{
$this->dispatcher = $dispatcher;
if (!isset($options['sender']))
{
$message = 'You must provide a sender parameter for this logger';
throw new sfConfigurationException($message);
}
if (!isset($options['smtp']))
{
$message = 'You must provide a "smtp" parameter for this logger';
throw new sfConfigurationException($message);
}
if (!isset($options['recipients']))
{
$message = 'You must provide a "recipients" parameter for this logger';
throw new sfConfigurationException($message);
}
$mailer = $this->getMailer($dispatcher);
$this->mailer = $mailer;
$this->sender = $options['sender'];
$this->recipients = $options['recipients'];
return parent::initialize($dispatcher, $options);
}
protected function doLog($message, $priority)
{
$this->message = $this->getMailer()->compose($this->sender, $this->recipients, 'log', $message);
}
public function shutdown()
{
$p = $this->mailer->send($this->message);
}
protected function initializeMailer()
{
require_once sfConfig::get('sf_symfony_lib_dir').'/vendor/swiftmailer/classes/Swift.php';
Swift::registerAutoload();
sfMailer::initialize();
//TODO refactor!
$pc = ProjectConfiguration::getApplicationConfiguration('ws', 'dev', true);
$config = sfFactoryConfigHandler::getConfiguration($pc->getConfigPaths('config/factories.yml'));
return new $config['mailer']['class']($this->dispatcher, $config['mailer']['param']);
}
protected function getMailer()
{
if (!$this->mailer)
{
$this->mailer = $this->initializeMailer();
}
return $this->mailer;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment