SMTP on CodeIgniter shows success, but e-mail is not delivered to Gmail account

  • Added:
  • |
  • In: Basic PHP

I am trying to setup SMTP on CodeIgniter. Everything is working fine and I recieve success message on page, that email is sent without errors. But, email is not delivered.

Here is the code, that I use:

$config = Array( 'protocol' => 'smtp', 'smtp_host' => 'ssl://', 'smtp_port' => 465, 'smtp_user' => '', 'smtp_pass' => '***', 'mailtype' => 'html', 'charset' => 'iso-8859-1', 'wordwrap' => TRUE ); $this->load->library('email', $config); $this->email->from('', 'Explendid Videos'); $this->email->to(''); $this->email->reply_to('', 'Explendid Videos'); $this->email->subject('Explendid Video - Contact form'); $message = "Contact form\n\n"; $message .= "Name: ". $_POST['name'] . "\n"; $message .= "Phone: ". $_POST['phone'] . "\n"; $message .= "Email: ". $_POST['email'] . "\n"; $this->email->message($message); $this->email->send(); 

What can be the reason, that e-mail is not actually delivered.

This Question Has 7 Answeres | Orginal Question | Wasif Khalil

here is work for me on apache2 server, ci 2.1.4: its very simple: first create a file called email.php under your application/config directory then type the following code inside them~>

<?php $config['protocol'] = 'smtp'; $config['smtp_host'] = 'ssl://'; $config['smtp_port'] = '465'; $config['smtp_user'] = 'u'r gmail account'; $config['smtp_pass'] = 'password of u'r account'; $config['charset'] = 'utf-8'; $config['newline'] = "\r\n"; ?> 

then create a file called email.php under your application/controllers directory then type this code~>

 <?php class Email extends CI_Controller { function send() { // Loads the email library $this->load->library('email'); // FCPATH refers to the CodeIgniter install directory // Specifying a file to be attached with the email // if u wish attach a file uncomment the script bellow: //$file = FCPATH . 'yourfilename.txt'; // Defines the email details $this->email->from('some@of.mailaddress', 'ur Name'); $this->email->to('email@detiny.your'); $this->email->subject('Email Test'); $this->email->message('Testing the email class.'); //also this script //$this->email->attach($file); // The email->send() statement will return a true or false // If true, the email will be sent if ($this->email->send()) { echo "you are luck!"; } else { echo $this->email->print_debugger(); } } } ?> 

you can change this script, for debug your problem,



if($this->email->send()) { echo 'Your email was sent.'; } else { show_error($this->email->print_debugger()); } 

Have you checked your php.ini file? Try it. If not then perhaps you could also try SPF. SPF or Sender Policy Framework is a new technology that allows easy detection of spam. Gmail honours SPF unless you manually mark those emails as not spam. Regardless of this, if you have received emails on another address then they must have reached Gmail too. Check your spam thoroughly, as Gmail does not discard emails even on very high spam suspicion rather they end up in the Spam folder.

You can set up a SPF that allows your webserver to send emails which will result in Gmail accepting emails sent by your webserver as authentic. See and a wizard from Microsoft.


$config['protocol'] = 'smtp'; 


$config['protocol'] = 'sendmail'; 

Use the following code

And dont froget to unable following two security settings in google.

1) << turn it on

2) << Click continue

** Turn off 2 step verification if you have it enabled.

$config = Array( 'protocol' => 'smtp', 'smtp_host' => 'ssl://', 'smtp_port' => 465, 'smtp_user' => '', //email id 'smtp_pass' => 'xxxxxxxxxxx', // password 'mailtype' => 'html', 'charset' => 'iso-8859-1' ); $this->load->library('email', $config); $this->email->set_newline("\r\n"); $this->email->from('','my name'); $this->email->to(""); // email array $this->email->subject('email subject'); $this->email->message("my mail body"); $result = $this->email->send(); show_error($this->email->print_debugger()); // for debugging purpose :: remove this once it works... 

Change it to the following:

$ci = get_instance(); $ci->load->library('email'); $config['protocol'] = "smtp"; $config['smtp_host'] = "ssl://"; $config['smtp_port'] = "465"; $config['smtp_user'] = ""; $config['smtp_pass'] = "yourpassword"; $config['charset'] = "utf-8"; $config['mailtype'] = "html"; $config['newline'] = "\r\n"; $ci->email->initialize($config); $ci->email->from('', 'Blabla'); $list = array(''); $ci->email->to($list); $this->email->reply_to('', 'Explendid Videos'); $ci->email->subject('This is an email test'); $ci->email->message('It is working. Great!'); $ci->email->send(); 

I just modified the code from RobinCominotto to make it work in office365.

PS: I got it working when placing it in a controller and calling this function exactly like this. When I place this configs on email.php (config file) does not work anymore :(

 $ci = get_instance(); $ci->load->library('email'); $config['protocol'] = "smtp"; $config['smtp_host'] = ""; $config['smtp_port'] = "587"; $config['smtp_user'] = "<HERE COMES YOUR EMAIL>"; $config['smtp_pass'] = "<HERE COMES THE PASSWORD OF EMAIL>"; $config['charset'] = "utf-8"; $config['mailtype'] = "html"; $config['newline'] = "\r\n"; $ci->email->initialize($config); $ci->email->from('<HERE COMES YOUR EMAIL>', 'Blabla'); $list = array('<HERE COMES TO EMAIL>', '<HERE COMES TO EMAIL>'); $ci->email->to($list); $this->email->reply_to('<HERE COMES YOUR EMAIL>', 'Explendid Videos'); $ci->email->subject('This is an email test'); $ci->email->message('It is working. Great!'); $ci->email->send(); print_r($ci->email->print_debugger()); 

I am...

Sajjad Hossain

I have five years of experience in web development sector. I love to do amazing projects and share my knowledge with all.

Connect Social With PHPAns