Sending email with Gmail using the CodeIgniter PHP Framework

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’sEmail 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 theCodeIgniter 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.
  1. <?php  if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’);
  2. /*
  3. | ——————————————————————-
  4. | EMAIL CONFING
  5. | ——————————————————————-
  6. | Configuration of outgoing mail server.
  7. | */
  8. $config[‘protocol’]=’smtp’;
  9. $config[‘smtp_host’]=’ssl://smtp.googlemail.com’;
  10. $config[‘smtp_port’]=’465′;
  11. $config[‘smtp_timeout’]=’30’;
  12. $config[‘smtp_user’]=’your gmail email’;
  13. $config[‘smtp_pass’]=’your gmail password’;
  14. $config[‘charset’]=’utf-8′;
  15. $config[‘newline’]=”\r\n”;
  16. /* End of file email.php */
  17. /* 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:
  1. // send an email
  2. $this->load->library(’email’);
  3. $this->email->from(‘team@myonepage.com’,’Team OnePage’);
  4. $this->email->to(“something@someaddress.com”);
  5. $this->email->subject(‘A test email from CodeIgniter using Gmail’);
  6. $this->email->message(“I can now email from CodeIgniter using Gmail as my server!”);
  7. $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.
Personal notes:
Other error that you may find across:

Severity: Warning

Message: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.googlemail.com:465 (Connection timed out)

Filename: libraries/Email.php

–> Possible solution is to open port 465 (You may want to consult the issue to your web hosting provider)

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