Example #1
0
 /**
  * Constructor.
  *
  * Available options:
  *
  *  * charset: The default charset to use for messages
  *  * logging: Whether to enable logging or not
  *  * delivery_strategy: The delivery strategy to use
  *  * spool_class: The spool class (for the spool strategy)
  *  * spool_arguments: The arguments to pass to the spool constructor
  *  * delivery_address: The email address to use for the single_address strategy
  *  * transport: The main transport configuration
  *  *   * class: The main transport class
  *  *   * param: The main transport parameters
  *
  * @param sfEventDispatcher $dispatcher An event dispatcher instance
  * @param array             $options    An array of options
  */
 public function __construct(sfEventDispatcher $dispatcher, $options)
 {
     // options
     $options = array_merge(array('charset' => 'UTF-8', 'logging' => false, 'delivery_strategy' => 'realtime', 'transport' => array('class' => 'Swift_MailTransport', 'param' => array())), $options);
     $constantName = 'sfMailer::' . strtoupper($options['delivery_strategy']);
     $this->strategy = defined($constantName) ? constant($constantName) : false;
     if (!$this->strategy) {
         throw new InvalidArgumentException(sprintf('Unknown mail delivery strategy "%s" (should be one of realtime, spool, single_address, or none)', $options['delivery_strategy']));
     }
     // transport
     $class = $options['transport']['class'];
     $transport = new $class();
     if (isset($options['transport']['param'])) {
         foreach ($options['transport']['param'] as $key => $value) {
             $method = 'set' . ucfirst($key);
             if (method_exists($transport, $method)) {
                 $transport->{$method}($value);
             } elseif (method_exists($transport, 'getExtensionHandlers')) {
                 foreach ($transport->getExtensionHandlers() as $handler) {
                     if (in_array(strtolower($method), array_map('strtolower', (array) $handler->exposeMixinMethods()))) {
                         $transport->{$method}($value);
                     }
                 }
             }
         }
     }
     $this->realtimeTransport = $transport;
     if (sfMailer::SPOOL == $this->strategy) {
         if (!isset($options['spool_class'])) {
             throw new InvalidArgumentException('For the spool mail delivery strategy, you must also define a spool_class option');
         }
         $arguments = isset($options['spool_arguments']) ? $options['spool_arguments'] : array();
         if ($arguments) {
             $r = new ReflectionClass($options['spool_class']);
             $this->spool = $r->newInstanceArgs($arguments);
         } else {
             $this->spool = new $options['spool_class']();
         }
         $transport = new Swift_SpoolTransport($this->spool);
     } elseif (sfMailer::SINGLE_ADDRESS == $this->strategy) {
         if (!isset($options['delivery_address'])) {
             throw new InvalidArgumentException('For the single_address mail delivery strategy, you must also define a delivery_address option');
         }
         $this->address = $options['delivery_address'];
         $transport->registerPlugin($this->redirectingPlugin = new Swift_Plugins_RedirectingPlugin($this->address));
     }
     parent::__construct($transport);
     // logger
     if ($options['logging']) {
         $this->logger = new sfMailerMessageLoggerPlugin($dispatcher);
         $transport->registerPlugin($this->logger);
     }
     if (sfMailer::NONE == $this->strategy) {
         // must be registered after logging
         $transport->registerPlugin(new Swift_Plugins_BlackholePlugin());
     }
     // preferences
     Swift_Preferences::getInstance()->setCharset($options['charset']);
     $dispatcher->notify(new sfEvent($this, 'mailer.configure'));
 }