How to set up CodeIgniter in order to use Gmail for emails

Now and then and… every time… I see people complaining that the email library of CodeIgniter doesn’t work. Why? Because they are using Gmail in order to send emails. And they are shocked that it doesn’t work. But in order to use Gmail as an email sender from your CodeIgniter, you have to know a couple of things.

1. The correct CI email library settings

Create an email.php file inside your application/config directory and put this in it:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

$config['protocol'] = 'sendmail';
$config['smtp_host'] = 'smtp.googlemail.com';
$config['smtp_user'] = '***@gmail.com';
$config['smtp_pass'] = '********';
$config['smtp_port'] = 465;
$config['smtp_timeout']='30';

Now just test it inside a controller:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

	public function index()
	{
        $this->load->library('email');
        $this->email->from('avenir.ro@gmail.com', 'Your Name');
        $this->email->to('avenir.ro@gmail.com');
        
        $this->email->subject('Email Test');
        $this->email->message('Testing the email class.');

        $this->email->send(false);
        echo $this->email->print_debugger();

		$this->load->view('welcome_message');
	}
}

Did it send? If the answer is “Yes”, then good for you. If the answer is “No”, then you need to follow two more steps:

2. The correct IMAP forwarding

Go to your Gmail, then to settings, and select the “Forwarding and POP/IMAP” tab. From there make sure you have IMAP access enabled.

3. Less security…

Go to https://myaccount.google.com/security and then scroll down to “Allow less secure apps”. Make sure that is set to… ON. If all this didn’t help you, then fine by me…

Leave a Reply

Your email address will not be published. Required fields are marked *

No spam? * Time limit is exhausted. Please reload CAPTCHA.