I would like to save my SMTP credential into global.php and local.php for invoking them when I generate a mail, like db adapter. Eventually, I'll store smtp1 and smtp2. I would like to avoid saving my SMTP credentials within a class.
Traditional SMTP options inside my Email class:
.............
$transport = new SmtpTransport();
$options = new SmtpOptions(array(
'name' => 'smtp.gmail.com',
'host' => 'smtp.gmail.com',
'connection_class' => 'login',
'connection_config' => array(
'username' => 'secretusr',
'password' => 'secretpsw',
'ssl' => 'tls'
),
));
save them into global.php:
<?php
return array(
'db' => array(
'driver' => 'Pdo_Mysql',
'charset' => 'utf-8',
'dsn' => 'mysql:dbname=zftutorial;host=localhost',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
'smtp'=> array(
'name' => 'smtp.gmail.com',
'host' => 'smtp.gmail.com',
'connection_class' => 'login'
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
'session' => array(
'config' => array(
'class' => 'Zend\Session\Config\SessionConfig',
'options' => array(
'name' => 'zf-tutorial',
),
),
'storage' => 'Zend\Session\Storage\SessionArrayStorage',
'validators' => array(
'Zend\Session\Validator\RemoteAddr',
'Zend\Session\Validator\HttpUserAgent',
),
),
);
?>
username and password saved into local.php
return array(
'db' => array(
'username' => 'secretusr',
'password' => 'secretpsw',
),
'smtp' => array (
'connection_config' => array(
'username' => 'secretusr',
'password' => 'secretpsw',
'ssl' => 'tls'
),
),
);
how to call smtp credential previosly saved? inside a my Email class:
.......
$transport = new SmtpTransport();
$options = new SmtpOptions(**method to call global and local credential**);
$transport->setOptions($options);
$transport->send($message);
thanks for the help!
$config = $sm->get('config');
$smtp = $config['smtp'];
Factory
, wich would work like this (not tested):// new file
class SmtpFactory implements FactoryInterface
{
public function createService($sm)
{
$config = $sm->get('config');
$smtpConf = $config['smtp'];
//instantiate some SMTP
//here you will define $smtpInstance
//may $smtpInstance is instance of SmtpTransport
// - with populated Options from config
$smtpInstance;
return $smtpInstance;
}
}
// in module.config.php
'service_manager' => array(
'factories' => array(
'Smtp' => 'Your\Namespace\SmtpFactory'
)
)
// in controller - you are also able to inject via SM
/**
* @var SmtpInstance $smtp
*/
$smtp = $this->getServiceLocator()->get('Smtp');