/** * 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')); }
public function __construct(KConfig $config = null) { parent::__construct($config); $this->_message = Swift_Message::newInstance(); switch ($config->method) { case 'sendmail': // WIP required -bs or -t switch $transport = Swift_SendmailTransport::newInstance($config->sendmail); break; case 'smtp': if ($config->smtpauth == 1) { if ($config->smtpsecure != "none") { $transport = Swift_SmtpTransport::newInstance($config->smtphost, $config->smtpport, $config->smtpsecure)->setUsername($config->smtpuser)->setPassword($config->smtppass); } else { $transport = Swift_SmtpTransport::newInstance($config->smtphost, $config->smtpport)->setUsername($config->smtpuser)->setPassword($config->smtppass); } } else { if ($config->smtpsecure != "none") { $transport = Swift_SmtpTransport::newInstance($config->smtphost, $config->smtpport, $config->smtpsecure); } else { $transport = Swift_SmtpTransport::newInstance($config->smtphost, $config->smtpport); } } break; case 'spool': // TODO: Make spool options configurable. $transport = Swift_SpoolTransport::newInstance(new Swift_FileSpool('/var/spool/swift')); break; case 'mail': default: $transport = Swift_MailTransport::newInstance(); break; } $this->_mailer = Swift_Mailer::newInstance($transport); }
/** * @param int $messageLimit * @param int $timeLimit * @return int */ public function sendSpooledMessages($messageLimit = 100, $timeLimit = 60) { $spool = $this->transport_spool->getSpool(); $spool->setMessageLimit($messageLimit); $spool->setTimeLimit($timeLimit); return $spool->flushQueue($this->transport); }
}; $c['log.main'] = function ($c) { $logger = new \Monolog\Logger('main'); $level = $c['config']['debug'] ? \Monolog\Logger::DEBUG : \Monolog\Logger::INFO; $logger->pushHandler(new \Monolog\Handler\StreamHandler($c['log_dir'] . '/main.log', $level)); $level = $c['config']['debug'] ? \Monolog\Logger::DEBUG : \Monolog\Logger::ERROR; // Will mail if error leve is above this setting. $logger->pushHandler(new \Monolog\Handler\SwiftMailerHandler($c['mailer'], new \Swift_Message('Module error need to repair'), $level)); return $logger; }; $c['mailer'] = function ($c) { // Using the native php mail function $transport = \Swift_MailTransport::newInstance(); if ($c['config']['debug']) { // Debug mode will write all sent emails to cache dir $transport = \Swift_SpoolTransport::newInstance(new \Swift_FileSpool($c['res_dir'] . '/cache/mailspool')); } return \Swift_Mailer::newInstance($transport); }; $c['router.routes'] = function ($c) { $routes = (require $c['app_dir'] . '/routing.php'); return $routes; }; $c['router'] = function ($c) { if (isset($c['request'])) { // If the request is set, use it // http://symfony.com/doc/current/components/routing/introduction.html#components-routing-http-foundation $context = new Symfony\Component\Routing\RequestContext(); $context->fromRequest($c['request']); } else { $context = new Symfony\Component\Routing\RequestContext(isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '');
<?php /** * 280-transport-spool-memory.php */ require_once '../vendor/autoload.php'; require_once './config.php'; // POINT of this sample $spool = new Swift_MemorySpool(); $transport = Swift_SpoolTransport::newInstance($spool); $mailer = Swift_Mailer::newInstance($transport); $message = Swift_Message::newInstance(); $message->setFrom(MAIL_FROM)->setTo(MAIL_TO)->setSubject('SpoolTransport (memory) sample')->setBody('This is a mail.'); // No send, but number of send is counted. $result = $mailer->send($message);
/** * Constructor. * * @param array $configuration System mail configuration. */ public function __construct(array $configuration) { $this->configuration = $configuration; $spool = $this->getSpoolFactory()->get($configuration); parent::__construct($spool); }