How to pull your mail configuration from application.ini for Zend Framework 1.8.0+

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 to “How to pull your mail configuration from application.ini for Zend Framework 1.8.0+

  1. thanks for this!

  2. Ahh yes. Help me it did. Thanks for posting this!

  3. 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…

  4. Thanks for useful information

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

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

  7. @Dwamian Mcleish,
    do i have to pass the transport argument at $mail->send($tr)?

  8. @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";
    		}
    
  9. 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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s