Add an ability to use SMTP mail transport

This commit is contained in:
Dmitriy Simushev 2014-12-10 16:21:59 +00:00
parent 17b7bf6757
commit 6fbee71173
3 changed files with 127 additions and 5 deletions

View File

@ -12,9 +12,33 @@ database:
tables_prefix: "" tables_prefix: ""
use_persistent_connection: false use_persistent_connection: false
# Mailbox # Mail
## This value will be used as sender address in all e-mails Mibew send.
mailbox: mibew@yourdomain.com mailbox: mibew@yourdomain.com
## Mailer parameters
mailer:
# This value determines what kind of mailer will be used. Possible values
# are "mail" and "smtp".
#
# To use standard PHP mail function just set this option to "mail". No
# additional configs are required in this case.
#
# To use SMTP-based solution set this option "smtp". This approach require
# some extra configuration to be done (see below).
transport: mail
# The lines below represents default SMTP mailer configurations. They will
# be used only if the "transport" option is set to "smtp".
host: localhost
port: 25
user: user
pass: ""
# The following option determines what kind of encryption should be used.
# false means no encyption at all. Another values once can use are "ssl" and
# "tls".
encryption: false
# Locales # Locales
## Native name will be used in this locale ## Native name will be used in this locale
home_locale: en home_locale: en

View File

@ -22,6 +22,7 @@ require_once(dirname(__FILE__) . '/libs/init.php');
use Mibew\Application; use Mibew\Application;
use Mibew\Authentication\AuthenticationManager; use Mibew\Authentication\AuthenticationManager;
use Mibew\Mail\MailerFactory;
use Mibew\Routing\Router; use Mibew\Routing\Router;
use Mibew\Routing\Loader\PluginLoader; use Mibew\Routing\Loader\PluginLoader;
use Symfony\Component\Config\FileLocator; use Symfony\Component\Config\FileLocator;
@ -38,14 +39,18 @@ $loader_resolver = new LoaderResolver(array(
)); ));
$router = new Router($route_loader, 'configs/routing.yml'); $router = new Router($route_loader, 'configs/routing.yml');
// Prepare files cache $application = new Application($router, new AuthenticationManager());
// Use custom files cache
$cache_driver = new \Stash\Driver\FileSystem(); $cache_driver = new \Stash\Driver\FileSystem();
$cache_driver->setOptions(array('path' => MIBEW_FS_ROOT . '/cache/stash')); $cache_driver->setOptions(array('path' => MIBEW_FS_ROOT . '/cache/stash'));
$cache = new \Stash\Pool($cache_driver); $cache = new \Stash\Pool($cache_driver);
$application = new Application($router, new AuthenticationManager());
$application->setCache($cache); $application->setCache($cache);
// Use custom config-dependent mailer factory
$configs = load_system_configs();
$application->setMailerFactory(new MailerFactory($configs['mailer']));
// Process request // Process request
$request = Request::createFromGlobals(); $request = Request::createFromGlobals();
$response = $application->handleRequest($request); $response = $application->handleRequest($request);

View File

@ -29,6 +29,87 @@ class MailerFactory implements MailerFactoryInterface
*/ */
protected $mailer = null; protected $mailer = null;
/**
* @var array|null
*/
protected $options = array();
/**
* Class constructor.
*
* @param Array $options Associative array of options that should be used.
*/
public function __construct($options = array())
{
$this->setOptions($options);
}
/**
* Gets factory's option.
*
* @param string $name Name of the option to retrieve.
* @throws \InvalidArgumentException If the option is unknown.
*/
public function getOption($name)
{
if (!isset($this->options[$name])) {
throw new \InvalidArgumentException(sprintf('Unknown option "%s"', $name));
}
return $this->options[$name];
}
/**
* Sets factory's option.
*
* @param string $name Name of the option to set.
* @param string $value New value.
* @throws \InvalidArgumentException If the option is unknown.
*/
public function setOption($name, $value)
{
if (!isset($this->options[$name])) {
throw new \InvalidArgumentException(sprintf('Unknown option "%s"', $name));
}
$this->options[$name] = $value;
}
/**
* Sets factory's options.
*
* @param array $options Associative array of options.
* @throws \InvalidArgumentException If specified array has unknow options.
*/
public function setOptions($options)
{
$defaults = array(
'transport' => 'mail',
'host' => 'localhost',
'port' => 25,
'user' => 'user',
'pass' => '',
'encryption' => false,
);
// Make sure all passed options are known
$unknown_options = array_diff(array_keys($options), array_keys($defaults));
if (count($unknown_options) != 0) {
throw new \InvalidArgumentException(sprintf(
'These options are unknown: %s',
implode(', ', $unknown_options)
));
}
if (empty($this->options)) {
// The options are set for the first time.
$this->options = $options + $defaults;
} else {
// Update only specified options.
$this->options = $options + $this->options;
}
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -49,6 +130,18 @@ class MailerFactory implements MailerFactoryInterface
*/ */
protected function getTransport() protected function getTransport()
{ {
return \Swift_MailTransport::newInstance(); switch ($this->getOption('transport')) {
case 'mail':
return \Swift_MailTransport::newInstance();
case 'smtp':
return \Swift_SmtpTransport::newInstance()
->setHost($this->getOption('host'))
->setPort($this->getOption('port'))
->setUsername($this->getOption('user'))
->setPassword($this->getOption('pass'))
->setEncryption($this->getOption('encryption') ?: null);
default:
throw new \RuntimeException(sprintf('Unknown transport "%s"', $this->getOption('transport')));
}
} }
} }