diff --git a/src/mibew/configs/default_config.yml b/src/mibew/configs/default_config.yml index 982a9d52..0a8aacf2 100644 --- a/src/mibew/configs/default_config.yml +++ b/src/mibew/configs/default_config.yml @@ -12,9 +12,33 @@ database: tables_prefix: "" use_persistent_connection: false -# Mailbox +# Mail +## This value will be used as sender address in all e-mails Mibew send. 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 ## Native name will be used in this locale home_locale: en diff --git a/src/mibew/index.php b/src/mibew/index.php index 4bc9ec18..8ca2ddab 100644 --- a/src/mibew/index.php +++ b/src/mibew/index.php @@ -22,6 +22,7 @@ require_once(dirname(__FILE__) . '/libs/init.php'); use Mibew\Application; use Mibew\Authentication\AuthenticationManager; +use Mibew\Mail\MailerFactory; use Mibew\Routing\Router; use Mibew\Routing\Loader\PluginLoader; use Symfony\Component\Config\FileLocator; @@ -38,14 +39,18 @@ $loader_resolver = new LoaderResolver(array( )); $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->setOptions(array('path' => MIBEW_FS_ROOT . '/cache/stash')); $cache = new \Stash\Pool($cache_driver); - -$application = new Application($router, new AuthenticationManager()); $application->setCache($cache); +// Use custom config-dependent mailer factory +$configs = load_system_configs(); +$application->setMailerFactory(new MailerFactory($configs['mailer'])); + // Process request $request = Request::createFromGlobals(); $response = $application->handleRequest($request); diff --git a/src/mibew/libs/classes/Mibew/Mail/MailerFactory.php b/src/mibew/libs/classes/Mibew/Mail/MailerFactory.php index eecb049c..487669d4 100644 --- a/src/mibew/libs/classes/Mibew/Mail/MailerFactory.php +++ b/src/mibew/libs/classes/Mibew/Mail/MailerFactory.php @@ -29,6 +29,87 @@ class MailerFactory implements MailerFactoryInterface */ 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} */ @@ -49,6 +130,18 @@ class MailerFactory implements MailerFactoryInterface */ 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'))); + } } }