Posted on

Using Gmail or Google Apps email with Magento

I was asked this week by a reader about using Google Apps email with Magento. There are solutions available that will allow you to configure the Magento SMTP server settings, or even your linux server SMTP settings so that you can use Gmail or Google Apps email to send outbound emails with Magento. In this post I will quickly cover a simple little open source Magento extension I made that makes setting up Gmail and Google Apps Email child’s play.

The Google Apps and Gmail Magento extension

You might be wondering firstly why you would want to use Google Apps or Gmail for your outbound emails from Magento.

  • Easier to set up a stable, secure and robust solution than if you try to run your own SMTP server.
  • Easy to administer with either the Gmail or Google Apps interface. You can set up auto replies, and auto forwards.
  • Excellent search capability for finding messages that have been sent to customers.
  • Acts as a log or archive of all emails sent by Magento, which means you can make sure it’s not sending any you do not want sent, and easily track any that are sent.

So hopefully everyone knows how sweet it is using a hosted ‘in the cloud’ email service, now on to how to do it!

Installation

You can install the module from Magento Connect by getting the extension key. Then using your store backend Magento Connect manager, you’ll need to change your settings to accept alpha modules (on the settings tab) and then just paste in the extension key and click install. Too easy. You can change the alpha back to stable afterwards, by the way.

Configuration

The Module is configured in the System -> Configuration -> System section. There are only three things to choose, so it’s very easy to setup.

Using Google Apps or Gmail with Magento is made easy with my purpose built Magento extension.
Using Google Apps or Gmail with Magento is made easy with my purpose built Magento extension.

It’s hopefully redundant, but for completeness here is some documentation:

The Enable Google Apps Email option allows you to turn the extension on and off easily. When enabled the extension will use your supplied Google Apps or Gmail credentials to send email from the Magento store.

The Email Address field is where you type the full address of you Gmail account or Google Apps account. It is important you type the whole address, even for Gmail where you might be more familiar with just entering a username.

Finally the Password field is where you type you Email account password. There is currently no way to test the connection (i.e. to make sure you have the right password) but that is planned functionality for a later release.

The Technical Details

The module is very simple, it’s just two Model files that overwrite the email sending functionality with Magento.

The key is in the setup of the Zend transport object as shown below:

public function getGoogleAppsEmailTransport() {
 
	$email = Mage::getStoreConfig('system/googleappsemail/email');
	$password = Mage::getStoreConfig('system/googleappsemail/password');
 
	$config = array('ssl' => 'tls', 'port' => 587, 'auth' => 'login', 'username' => $email, 'password' => $password);
	$transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);
 
	return $transport;
 
}

It’s annoying that for all the flexibility Magento allows, the way that the email sending has been coded really doesn’t allow much scope for extending the functionality without re-writing a sizable amount of Magento code. This means that maintenance becomes more difficult and the value of the inheritance structure is lost.

Setting up your Google Apps account

Sign up for a Google Apps account and then you’ll be able to create a ‘user’ to do your Magento email sending. In my current configurations I use email addresses like ‘mailer@xyzdomain.com’ or ‘no-reply@xyzdomain.com’. This means that to the end user the from email address will appear like a sophisticated CRM mailer, when in fact it’s just a free Google Apps account!

I also recommend setting the account up to forward a copy of all email to your own personal Gmail or Google Apps account. This way if any users do actually try to reply to the mailer address, the message will still make it to someone. If you wanted to you could even set up an auto responder that informed users to contact you in a preferred manner.

Feedback

I’d appreciate some feedback on this little module – I know the functionality exists elsewhere, but sometimes just making a good solution really easy for everyone, can be beneficial, hopefully you agree. If you spot any bugs or would like to suggest some new functionality, I’m all ears. Keen readers who check out the source code (which is totally open by the way) may notice I have some code in there for sending test emails, I just haven’t wired it up to the admin interface (backend) yet.