Sending Emails with Zend_Mail using Gmail or Google Apps
June 09, 2011

Introduction

Zend Framework is currently one of the best MVC-based frameworks in the PHP world. Zend_Mail is part of Zend Framework and it provides the ability to easily send email messages. If you’re like me, most web applications you have developed are setup to use Google Apps as their email provider. Here’s how to send email messages via Gmail or Google Apps by using Zend_Mail.

The Code


<?php

public function send() {

 //Initialize needed variables
 $your_name = 'Mario Awad';
 $your_email = 'your_email@your_domain.com'; //Or your_email@gmail.com for Gmail
 $your_password = 'your_password';
 $send_to_name = 'My Friend';
 $send_to_email = 'myfriend@example.com';

 //SMTP server configuration
 $smtpHost = 'smtp.gmail.com';
 $smtpConf = array(
  'auth' => 'login',
  'ssl' => 'ssl',
  'port' => '465',
  'username' => $your_email,
  'password' => $your_password
 );
 $transport = new Zend_Mail_Transport_Smtp($smtpHost, $smtpConf);

 //Create email
 $mail = new Zend_Mail();
 $mail->setFrom($your_email, $your_name);
 $mail->addTo($send_to_email, $send_to_name);
 $mail->setSubject('Hello World');
 $mail->setBodyText('This is the body text of the email.');

 //Send
 $sent = true;
 try {
  $mail->send($transport);
 }
 catch (Exception $e) {
  $sent = false;
 }

 //Return boolean indicating success or failure
 return $sent;

}

Extra Notes

In addition to the above code, please note the following:

  • You must enable the "php_openssl" extension to use the SSL transport protocol (Which is needed for Gmail and Google Apps). All you have to do is open your "php.ini" file and uncomment the line that includes the "php_openssl" extension (Search for "php_openssl" and you’ll find it).
  • I found many resources on the web stating that you should use the TLS transport protocol (They never mention how to use or setup the SSL transfer protocol). This didn’t work in my tests and always resulted in a timeout error.
  • Yes, you must use "smtp.gmail.com" as your SMTP host even if you’re configuring the application for Google Apps. In other words, don’t use "smtp.yourdomain.com".

I hope this small tutorial saves you some headaches.

Cheers!


Email Google Apps Zend Framework

Share this post


Written by
Mario Awad

Founder of SOFTKUBE, lead developer, and getting things done addict. Passionate about open source, user interface design, business development, and the tech world.

More about Mario Awad


About
SOFTKUBE

A small team of experts developing simple, usable, and high-quality web solutions. We blog about business, entrepreneurship, web development, and technology.

More about us


Recent Posts

Gaining Access to a Legacy Google Apps Account When Phone Verification Fails

Custom Theme Migration from Drupal 9 to Drupal 10

How to Change the Most Recent Git Commit Message

How to Make Google Chrome Forget a Permanent HTTP 301 Redirect

View all posts


All Posts Categories

Business Cheat Sheets CLI Design Development Downloads Drupal Email Google Apps HID Keyboards Multilingualism Open Source Philosophy PHP Pointing Devices Productivity Quotes Science Security SEO Technology Thoughts Windows Zend Framework