I’m working on Zend Framework 1.8.0 on a dev box and needed to configure Zend_Mail_Transport_Smtp for testing purposes to hit a gmail account. The manual wants you to do this:
$config = array('auth' => 'login',
'username' => 'myusername',
'password' => 'password');
$transport = new Zend_Mail_Transport_Smtp('mail.server.com', $config);
but who wants to put passwords and config data in code? Naturally it belongs in the application.ini. Some users may find it tricky from the manual to figure out how to get it from application.ini in version 1.8.0 into their controller, so I thought I’d offer this tip out there.
This is how I do it, assuming you are connecting to gmail. In your application.ini, put these lines in the correct section (I put in the dev section), replacing your username and password in the appropriate fields of course:
email.server = smtp.gmail.com email.username = yrusername@gmail.com email.password = yrpass email.ssl = ssl email.port = 465
Then in your controller, after defining the controller class:
private $_aMailConfig; private $_strSmtp;
then in your preDispatch function (if you don’t have one, create one and) add this:
public function preDispatch()
{
$bootstrap = $this->getInvokeArg('bootstrap');
$aConfig = $bootstrap->getOptions();
$this->_aMailConfig = array(
'auth' => 'login'
,'username' => $aConfig['email']['username']
,'password' => $aConfig['email']['password']
,'ssl' => $aConfig['email']['ssl']
,'port' => $aConfig['email']['port']);
//echo '<pre>' . print_r($this->_aMailConfig,1);exit;
$this->_strSmtp = $aConfig['email']['server'];
parent::preDispatch();
}
then whenever you need it in your controller, type this:
$mailTransport = new Zend_Mail_Transport_Smtp($this->_strSmtp,$this->_aMailConfig); Zend_Mail::setDefaultTransport($mailTransport);
I haven’t tested this outside my own environment, which does some funky stuff, so I can’t guarantee it will work on yours, but I believe it should.
Hope this helps someone!
9 responses so far ↓
MacSebi // July 9, 2009 at 2:44 am
thanks for this!
Tim Fletcher // July 12, 2009 at 4:04 pm
Ahh yes. Help me it did. Thanks for posting this!
jithu // July 23, 2009 at 2:44 pm
Thanx a lot for your information…It really helped me and saved a hell lot of time…
Once again thanking u …Keep up the good work…
om // August 25, 2009 at 10:24 am
Thanks for useful information
Dwamian Mcleish // September 7, 2009 at 5:05 am
Here is an alternate way of doing this. Add the config entries as the posts suggests. Then go to your main Bootstrap.php file and add the following
protected function _initEmailTransport(){ $aConfig = $this->getOptions(); $emailConfig = array( 'auth'=>'login', 'username'=>$aConfig['email']['username'], 'password'=>$aConfig['email']['password'], 'ssl'=>$aConfig['email']['ssl'], 'port'=>$aConfig['email']['port'], ); $server = $aConfig['email']['server']; $transport = new Zend_Mail_Transport_Smtp($server, $emailConfig); Zend_Mail::setDefaultTransport($transport); //echo "We can send email from anywhere now"; }Your Bootstrap.php file will automatically execute any code prefixed with “_init”. That way..the transport will be available to the entire application instead of per controller
Sudoku // September 10, 2009 at 9:28 am
I have used it in a different way see my post. I also put it in the registry for use all over the site.
have a look
http://roninio.blogspot.com/2009/09/zend-framework-pull-information-from.html
Anindya // October 7, 2009 at 12:51 pm
@Dwamian Mcleish,
do i have to pass the transport argument at $mail->send($tr)?
dmcleish // October 8, 2009 at 5:04 am
@Anindya
No. Once you set it in config and you initialize it in your bootstrap, it will be available to you in the application. Here is an example of how you would use it:
try { $mail = new Zend_Mail ( ); $mail->setFrom ( 'from@domain.com' ); $mail->addTo ( 'to@domain.com' ); $mail->setSubject ( 'Your Subject' ); $email = ""; //not ideal but works for demonstration purposes include "emails/_email-confirm-registration.phtml"; $mail->setBodyText ( $email ); $mail->send (); } catch ( Exception $e ) { $this->view->message = "Unable to send email"; }Fabio // November 19, 2009 at 1:21 am
I wrote an application resource for this, you can find it here.
The post is in italian, but the code is commented in English, you should be able to use it.
Bye