简体   繁体   中英

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. 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!

  1. Your whole merged config is available via 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):
// 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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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