简体   繁体   English

在Zend Framework 2中将SMTP凭据保存并访问到global / local.php中

[英]Save and Access SMTP credential into global/local.php in Zend Framework 2

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. 我想将SMTP凭据保存到global.php和local.php中,以便在生成邮件(如数据库适配器)时调用它们。 Eventually, I'll store smtp1 and smtp2. 最终,我将存储smtp1和smtp2。 I would like to avoid saving my SMTP credentials within a class. 我想避免将SMTP凭据保存在一个类中。

Traditional SMTP options inside my Email class: 我的Email类中的传统SMTP选项:

.............
        $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: 将它们保存到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 用户名和密码保存到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? 如何调用smtp凭证预先保存? inside a my Email class: 在我的Email类中:

....... 
    $transport = new SmtpTransport();
    $options   = new SmtpOptions(**method to call global and local credential**);


     $transport->setOptions($options);
     $transport->send($message);

thanks for the help! 谢谢您的帮助!

  1. Your whole merged config is available via Service-Manager. 您可以通过Service-Manager获得整个合并的配置。
$config = $sm->get('config');
$smtp = $config['smtp'];
  1. You could instantiate your SMTP via a Factory , wich would work like this (not tested): 您可以通过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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM