在 Zend Framework 2 中将 SMTP 凭据保存并访问到 global/local.php
Save and Access SMTP credential into global/local.php in Zend Framework 2
我想将我的 SMTP 凭据保存到 global.php 和 local.php 中,以便在我生成邮件时调用它们,例如数据库适配器。最后,我将存储 smtp1 和 smtp2。我想避免将我的 SMTP 凭据保存在 class 中。
我的电子邮件中的传统 SMTP 选项 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'
),
));
将它们保存到 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',
),
),
);
?>
用户名和密码保存到local.php
return array(
'db' => array(
'username' => 'secretusr',
'password' => 'secretpsw',
),
'smtp' => array (
'connection_config' => array(
'username' => 'secretusr',
'password' => 'secretpsw',
'ssl' => 'tls'
),
),
);
如何调用之前保存的smtp凭据?在我的电子邮件中 class:
.......
$transport = new SmtpTransport();
$options = new SmtpOptions(**method to call global and local credential**);
$transport->setOptions($options);
$transport->send($message);
感谢您的帮助!
- 您的整个合并配置可通过服务管理器获得。
$config = $sm->get('config');
$smtp = $config['smtp'];
- 您可以通过
Factory
实例化您的 SMTP,它会像这样工作(未测试):
// 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');
我想将我的 SMTP 凭据保存到 global.php 和 local.php 中,以便在我生成邮件时调用它们,例如数据库适配器。最后,我将存储 smtp1 和 smtp2。我想避免将我的 SMTP 凭据保存在 class 中。
我的电子邮件中的传统 SMTP 选项 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'
),
));
将它们保存到 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',
),
),
);
?>
用户名和密码保存到local.php
return array(
'db' => array(
'username' => 'secretusr',
'password' => 'secretpsw',
),
'smtp' => array (
'connection_config' => array(
'username' => 'secretusr',
'password' => 'secretpsw',
'ssl' => 'tls'
),
),
);
如何调用之前保存的smtp凭据?在我的电子邮件中 class:
.......
$transport = new SmtpTransport();
$options = new SmtpOptions(**method to call global and local credential**);
$transport->setOptions($options);
$transport->send($message);
感谢您的帮助!
- 您的整个合并配置可通过服务管理器获得。
$config = $sm->get('config');
$smtp = $config['smtp'];
- 您可以通过
Factory
实例化您的 SMTP,它会像这样工作(未测试):
// 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');