Sending Email With Gmail Smtp With Codeigniter Email Library

Sending email with gmail smtp with codeigniter email library

$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxx',
'smtp_pass' => 'xxx',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");

// Set to, from, message, etc.

$result = $this->email->send();

From the CodeIgniter Forums

Unable to send email using SMTP gmail config in codeigniter 3

Are you try this?

$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxx',
'smtp_pass' => 'xxx',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");

// Set to, from, message, etc.

$result = $this->email->send();

And make sure you had setting your email account with
Allowing less secure apps to access your account

Codeigniter Email PHP SMPT Error, cannot send mail through Gmail

You don't need to change php.ini or sendmail.ini to send SMTP. Please make it default.

After I look at your SMTP configuration, You missed a little thing but seems important: crypto protocol.

You can set crypto protocol inside $config['smtp_host'] like so:

$config['smtp_host'] = 'ssl://smtp.googlemail.com'

Or you can also try this config:

$config['protocol'] = "smtp";
$config['smtp_host'] = "smtp.googlemail.com";
$config['smtp_port'] = "465"; // if you decided to use 465, then set smtp crypto to ssl, else for example 587, you need to set smtp crypto to tls.
$config['smtp_crypto'] = 'ssl'; // this is based on your smtp port -> ssl/tls
$config['smtp_timeout'] = "400";
$config['smtp_user'] = "youruser@gmail.com";
$config['smtp_pass'] = "yourpassword%&^$&$";
$config['validate'] = true;
$config['charset'] = 'utf-8';
$config['mailtype'] = "html";
$config['crlf'] = "\r\n";
$config['newline'] = "\r\n";

Then assign $config to email library: $this->load->library('email', $config);

Please do check less security (in gmail setting) was: enabled.

Otherwise, you can create email.php as an email config library inside application/config/email.php

just put it there:

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

$config['protocol'] = "smtp";
$config['smtp_host'] = "smtp.googlemail.com";
$config['smtp_port'] = "465"; // if you decided to use 465, then set smtp crypto to ssl, else for example 587, you need to set smtp crypto to tls.
$config['smtp_crypto'] = 'ssl'; // this is based on your smtp port -> ssl/tls
$config['smtp_timeout'] = "400";
$config['smtp_user'] = "youruser@gmail.com";
$config['smtp_pass'] = "yourpassword%&^$&$";
$config['validate'] = true;
$config['charset'] = 'utf-8';
$config['mailtype'] = "html";
$config['crlf'] = "\r\n";
$config['newline'] = "\r\n";

?>

Then inside your controller you don't need to set up your $config anymore :D, but all you have to do just call $this->load->library('email'); in controller.

SMTP Mail not sending - Codeigniter Email Library

Solution:
The problem was fixed! The culprit was Valid Hostname & Reverse DNS.

Detailed info:
Due to misconfiguration, SMTP was responding around 7 - 10 secs. According to docs, if we don't specify the smtp_timeout, it will take default which is 5 sec.
So I changed the smtp_timeout from default 5 sec to 10 sec and it works.

After figuring out what was the problem, found that the SMTP responding slow. There was no valid hostname, Reverse DNS added. So added that. Now it's working as expected. Now I removed the smtp_timeout field. It's working now.

codeigniter smtp mail not working properly

The issue with your code is that you are not initializing the email setting. Your controller code on line $this->load->library('email', $config); is wrong. You have to initialize the email setting separately after the email library is loaded.

Take a look at this code.

$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.gmail.com';
$config['smtp_port'] = 587, //if 80 dosenot work use 24 or 21
$config['smtp_user'] = '**';
$config['smtp_pass'] = '**';
$config['_smtp_auth'] = true,
$config['smtp_crypto'] = 'tls';
$config['protocol'] = 'smtp';
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE

$this->load->library('email');
$this->email->initialize($config);

$this->email->set_newline("\r\n");
$this->email->from('users@gmail.com'); //same email u use for smtp_user
$this->email->to($this->input->post('user_email'));
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();

For more information look at here



Related Topics



Leave a reply



Submit