zf2:如何将我的zend邮件传输配置存储在mail.local.php文件中?

时间:2012-10-08 14:18:49

标签: php email zend-framework2

我尝试了这个,但对我来说没有用。 https://stackoverflow.com/questions/10537318/how-to-use-the-di-for-zend-mail-transport-smtp

我使用zend framework 2.0.3dev

2 个答案:

答案 0 :(得分:18)

对我而言,这适用于谷歌应用程序邮件地址

config/autoload/mail.local.php

return array(
'mail' => array(
    'transport' => array(
        'options' => array(
            'host'              => 'smtp.gmail.com',
            'connection_class'  => 'plain',
            'connection_config' => array(
                'username' => 'example@example.org',
                'password' => '',
                'ssl' => 'tls'
            ),
        ),  
    ),
),
);

并在Module.php

public function getServiceConfig()
{
    return array(
        'factories' => array(
            'mail.transport' => function (ServiceManager $serviceManager) {
                $config = $serviceManager->get('Config'); 
                $transport = new Smtp();                
                $transport->setOptions(new SmtpOptions($config['mail']['transport']['options']));

                return $transport;
            },
        ),
    );
}

并在控制器中

$transport = $this->getServiceLocator()->get('mail.transport');

我希望这段代码对某人有用:D

答案 1 :(得分:1)

Try:
$objEmail = new \Zend\Mail\Message();
$objEmail->setBody('Message here');
$objEmail->setFrom('from@domain.com', 'From');
$objEmail->addTo('to@domain.com', 'To');
$objEmail->setSubject('Subject here');
// Setup SMTP transport using PLAIN authentication over TLS
$transport = new \Zend\Mail\Transport\Smtp();
$options = new \Zend\Mail\Transport\SmtpOptions(array(
    'name' => 'smtp.gmail.com',
    'host' => 'smtp.gmail.com',
    'port' => 587, // Notice port change for TLS is 587
    'connection_class' => 'plain',
    'connection_config' => array(
        'username' => '',
        'password' => '',
        'ssl' => 'tls',
    ),
));
$transport->setOptions($options);
$transport->send($objEmail);