Shaked Klein Orbach echo 'Just smile';

21Jan/120

Zend Framework, Gmail, SMTP and SSL

Topic says everything, isn't it?

Description

I had a problem sending emails while using Zend Framework with Gmail account. So I had to handle different types of errors:

  • "Unable to connect via TLS" - being created by Zend Framework Exception.
  • "Unable to find the socket transport "https" - did you forget to enable it when you configured PHP?" - PHP built in error message

Suggestions

Before starting to code, you have to configure your PHP to work with php-openssl.

Linux:

Install openssl

Install php5_openssl. I`m not sure about all of the distributions I did it only on OpenSuse:

zypper install php5_openssl

I`m guessing that on CentOS it would be something like

yum install php5_openssl

And Ubuntu

apt-get install php5_openssl

Windows:

Add extension=php_openssl.dll to your php.ini

 After finishing it, please restart your apache.

 

Now check if the SSL module is installed:

php -m | grep ssl

will output:

openssl

Or try to check your phpinfo.

 

Now you can use my code as an example:

application.ini

email.host 			= 'smtp.gmail.com'
email.from		 	= 'info@yourdomain.com'
email.name 			= 'Display Name'
email.params.auth		= 'login'
email.params.username 		= 'info@yourdomain.com'
email.params.password 		= 'Password'
email.params.ssl 		= 'ssl'
email.params.port 		= 465

helper.perfix[] = 'My_Helper';

Bootstrap.php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected  function _initConfig(){
    	 $config = new Zend_Config_Ini(
            APPLICATION_PATH . '/configs/application.ini',
            APPLICATION_ENV
        );
        Zend_Registry::set('appConfig',$config);
    }
    protected function _initHelpers(){
    	 $perfix = Zend_Registry::get('appConfig')->helper->perfix->toArray();
    	 Zend_Controller_Action_HelperBroker::addPrefix($perfix[0]);
    }
}

IndexController.php

class IndexController {
 	public function indexAction(){
		$helper = $this->_helper->mail(array(
				 			'email'		=> 'myemail@gmail.com',
				 			'subject' 	=> 'test subject',
				 			'message'	=> 'just a test message'
				 		));
		var_dump($helper);die;
	}
}

Mailer.php (helper)

class My_Helper_Mail extends Zend_Controller_Action_Helper_Abstract {
	/**
	 * @var array email has to contain those values
	 */
	private static $_keys = array('email','subject','message');
	/**
	 * @var Zend_Mail
	 */
	private $_mail;

	const MAIL_BODY_CHARSET 	= 'utf8';
	const VALIDATION_DATA_ERROR = 'Email error, key %s is required and cannot be empty.';

	/**
	 * Execute everything, enables our class to work as helper ($this->_helper->mail(array()))
	 * @params array $data (Should contain self::$_keys)
	 * @return Zend_Mail
	 */
	public function direct(array $data){
		//validate keys and values
		self::_checkData($data);

		//prepare
		$this->_prepare($data);

		return $this->_send(self::_createTransportSmtp());
	}

	/**
	 * Validate data before send
	 * @param array $data
	 * @throws My_Helper_Mail_Exception
	 */
	private static function _checkData(array $data){
		foreach(self::$_keys as $value){
			if (empty($data[$value])){
				throw new My_Helper_Mail_Exception(sprintf(
					self::VALIDATION_DATA_ERROR,
					$value
				));
			}
		}
	}

	/**
	 * Prepre mail data
	 * @param array $data
	 */
	private function _prepare(array $data){
		$oAppConfig = Zend_Registry::get('appConfig')->email;

		$this->_mail = new Zend_Mail();
		$this->_mail->addTo($data['email']);
		$this->_mail->setSubject($data['subject']);
		$this->_mail->setBodyHtml($data['message'],self::MAIL_BODY_CHARSET);
		$this->_mail->setFrom($oAppConfig->from,$oAppConfig->name);
	}

	/**
	 * Setup mail settings
	 * @return Zend_Mail_Transport_Smtp
	 */
	private static function _createTransportSmtp(){
		$config = Zend_Registry::get('appConfig'); 

		$host  	= $config->email->host;
		$params	= $config->email->params;

		return new Zend_Mail_Transport_Smtp($host,$params->toArray());
	}

	/**
	 * Send mail
	 * @param Zend_Mail_Transport_Smtp $transport
	 * @return Zend_Mail
	 */
	private function _send(Zend_Mail_Transport_Smtp $transport){
		return $this->_mail->send($transport);
	}
}
class My_Helper_Mail_Exception extends Zend_Exception {}
Or you may download the files Here

Summary

Using Zend Framework is pretty straight-forward when you have all of your server settings enabled.

You are more then welcome to update me if you would found any other solution or a problem.

 

-Shak

Posted by Shaked

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

(required)

No trackbacks yet.