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.
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.
- <?php if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’);
- | ——————————————————————-
- | EMAIL CONFING
- | ——————————————————————-
- | Configuration of outgoing mail server.
- | */
- $config[‘smtp_user’]=’your gmail email’;
- $config[‘smtp_pass’]=’your gmail password’;
- /* 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->email->from(‘email@example.com’,’Team OnePage’);
- $this->email->subject(‘A test email from CodeIgniter using Gmail’);
- $this->email->message(“I can now email from CodeIgniter using Gmail as my server!”);
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:
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.
Other error that you may find across:
Message: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.googlemail.com:465 (Connection timed out)
–> Possible solution is to open port 465 (You may want to consult the issue to your web hosting provider)