Carlos Developer Blog Just another developer weblog

15Jul/093

CodeIgniter + Email + SMTP Custom Config to Gmail Working

I was trying to make use of the email library on codeigniter, and found some issues on the Gmail config and authentication errors, then after making my own I have made a custom configuration that works fine.

1. Create this CONSTANTS on config/constanst.php

define('MAIL_PROT', 'smtp');
define('SITE_ADMIN', 'Webmaster');
define('SITE_EMAIL', 'gmail.user@gmail.com');
define('SMTP_HOST', 'ssl://smtp.googlemail.com');
define('SMTP_USER', 'gmail.user@gmail.com');
define('SMTP_PASS', 'somepassword');
define('SMTP_PORT', 465);
define('MAIL_TYPE', 'html');
define('MAIL_CHARSET', 'utf-8');

2. Then create a smtp config function on a email_model for example:

	function smtpconfig()
	{
			$config = array();
			//$config = $this->config->item('email');
			$config = Array(
			    'protocol' => MAIL_PROT,
			    'smtp_host' => SMTP_HOST,
			    'smtp_port' => SMTP_PORT,
			    'smtp_user' => SMTP_USER,
			    'smtp_pass' => SMTP_PASS,
			    'smtp_timeout' => 30,
			    'mailtype'  => MAIL_TYPE
			);
			//var_dump($config);

			$this->load->library('email', $config);
			$this->email->clear();
			$this->email->set_newline("\r\n");
	}

3. Finally we can use BOTH in a test function like:

function emailtest()
{
		$this->smtpconfig();
                $this->email->from(SITE_EMAIL, SITE_ADMIN);
		$this->email->to('receiver.user@gmail.com');

		$this->email->subject('CodeIgniter & Gmail Rules !! ');
		$this->email->message('Hello Gmail World');

		if (!$this->email->send())
		    show_error($this->email->print_debugger());
		else
			echo 'Your e-mail has been sent!';
}

I hope this would be useful.

Thanks for your feedback,

Carlos

Comments (3) Trackbacks (0)
  1. Hi, Where are you from? Is it a secret? :)
    Jinny

  2. Hello there…

    There is no secret…. I’m just a developer from BO (if you know the two letters country), so I’m judging you are from RUSIA ?

    Thanks for your kind comment.

    Carlos

  3. I wanted to ask, is there any chance for a modified version of the directory listing script, with an iphone-stylish like design?


Leave a comment


No trackbacks yet.