chrvadala
chrvadala

Reputation: 592

zf2: How can I store my zend mail transport config in a mail.local.php file?

I tried this, but for me doesn't worked. https://stackoverflow.com/questions/10537318/how-to-use-the-di-for-zend-mail-transport-smtp

I use zend framework 2.0.3dev

Upvotes: 1

Views: 4538

Answers (2)

Indrajeet Singh
Indrajeet Singh

Reputation: 2989

Try:
$objEmail = new \Zend\Mail\Message();
$objEmail->setBody('Message here');
$objEmail->setFrom('[email protected]', 'From');
$objEmail->addTo('[email protected]', '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); 

Upvotes: 1

chrvadala
chrvadala

Reputation: 592

For me this worked with a google app mail address

config/autoload/mail.local.php

return array(
'mail' => array(
    'transport' => array(
        'options' => array(
            'host'              => 'smtp.gmail.com',
            'connection_class'  => 'plain',
            'connection_config' => array(
                'username' => '[email protected]',
                'password' => '',
                'ssl' => 'tls'
            ),
        ),  
    ),
),
);

and in 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;
            },
        ),
    );
}

and in the controller

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

I hope that this code will be usefull for somebody :D

Upvotes: 18

Related Questions