September 25th, 2009 | Joel | Comments#comments">Comments
A great guy called Ricardo has kindly translated this post into Portuguese, see it here if that’s preferable.
I have just implemented this functionality for OnePage and I’d like to share it in case people find it useful.

Why send email with Gmail rather than the server’s SMTP configuration?

There are a number of advantages I see for doing this:
  • Ability to develop locally and test email sending functionality without going to lengths to setup a local mail server.
  • Ability to utilise Google Apps emails to send email from emails which are on your own domain.
  • Ability to have a reference of the mail you send using this method in the “sent” folder on your Gmail account.

Right on!

So we’ve decided that’s the route we’re going to take, so lets get going! It’s rather simple actually, especially with the fantastic CodeIgniter framework and it’s Email Class. It should work nicely for regular PHP or with other frameworks if you just take the concepts outlined here. I must also give credit to wrs from the CodeIgniter Forums, since it is his post which I have based my solution upon.

A new config file – email.php

Place the following code inside a new config file called “email.php” and put it in your application/config directory. Config files named with the same name as a library are included automatically, and this is what we’re doing here.
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------
| EMAIL CONFING
| -------------------------------------------------------------------
| Configuration of outgoing mail server.
| */
$config['protocol']='smtp';
$config['smtp_host']='ssl:/smtp.googlemail.com';
$config['smtp_port']='465';
$config['smtp_timeout']='30';
$config['smtp_user']='your gmail email';
$config['smtp_pass']='your gmail password';
$config['charset']='utf-8';
$config['newline']="\r\n";

/* End of file email.php */
/* Location: ./system/application/config/email.php */

Make sure you change ‘your gmail email’ and ‘your gmail password’ appropriately.

Sending email, now sent through Gmail

Now any emails you send using the CodeIgniter Email Class will actually be sent from your Gmail account of choice:

// send an email
$this->load->library('email');
$this->email->from('team@myonepage.com','Team OnePage');
$this->email->to("something@someaddress.com");
$this->email->subject('A test email from CodeIgniter using Gmail');
$this->email->message("I can now email from CodeIgniter using Gmail as my server!");
$this->email->send();

UPDATE: It seems Posterous thinks it’s clever by turning my email’s into links, even inside code. Please adjust your code accordingly :)

One drawback of this method

There is one drawback of this method, which is that you cannot specify the “from” email to be anything other than your Gmail email address or an email address you have associated with the Gmail account. All email will be sent from the email account you have setup as the default in your Gmail account.

Possible error – “Unable to find the socket transport “ssl” – did you forget to enable it when you configured PHP?”

If you got this error, like I did, then you need to enable SSL in your PHP config. Load up php.ini and find a line with the following:
;extension=php_openssl.dll
Simply remove the “;” and then restart Apache and you’re good to go. The “;” denotes that the line is a comment, and so removing it enables the SSL extension for Apache. Credit - http://www.boringguys.com/2007/07/20/unable-to-find-the-socket-transport-ssl-did-you-forget-to-enable-it-when-you-configured-php/

That’s all!

I hope that is useful for some people :) I use it mainly for local development purposes – it’s nice to be able to test everything.
If you find my posts useful, you should check out my OnePage and follow me on Twitter.

  • johanidstam
    Gmail only allows you to send 2000 messages per day per account even if you are using Google apps.
    I think I will create several accounts to send email from my CodeIgninter application and rotate between them.
    On the other hand, when my application needs more than 2000 messages, I can probably afford some other solution.
  • I think I agree that if you're sending more than 2000 messages a day you can probably afford a better solution.

    I use this method almost exclusively for local development rather than for in a live production environment.

    Right now, we're testing out Postmark for OnePage which is a nice solution - http://postmarkapp.com/
  • Thank you so much. Very helpful your tip. Can I make a translation of the article to portuguese in my blog? Offcourse I'll give the due credits. Anyway, once more, thank you.
  • Hi Ricardo! I'm glad you found it useful.

    I'd love for you to translate it to Portuguese, please go ahead :)
  • It' done. Here is the link to the tranlation
    http://hipercodigo.blogspot.com/2010/02/traduca...
    Thank you.
  • Thank you! I've added a link at the top of the article :)
  • Just a correction, in the top of the article you mentioned that I've translated into Spanish, actually I've translated into Portuguese.
  • Don't know how I managed that, sleep deprivation is not a good thing! :) Thanks again Ricardo.
blog comments powered by Disqus