Send Email by Email Class in Codeigniter with Gmail

Send email by Email Class in codeigniter with Gmail

this is what worked for me

$email_config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => '465',
'smtp_user' => 'someuser@gmail.com',
'smtp_pass' => 'password',
'mailtype' => 'html',
'starttls' => true,
'newline' => "\r\n"
);

$this->load->library('email', $email_config);

$this->email->from('someuser@gmail.com', 'invoice');
$this->email->to('test@test.com');
$this->email->subject('Invoice');
$this->email->message('Test');

$this->email->send();

sending email using codeigniter email class

Configuration in sendmail.ini

path c:\xampp\sendmail\sendmail.ini

Configurations

[sendmail]

smtp_server=smtp.gmail.com
smtp_port=25
error_logfile=error.log
debug_logfile=debug.log
auth_username=myemail@gmail.com
auth_password=yourgmailpassword
force_sender=myemail@gmail.com

in php.ini

pathc:\xampp\xampp\php\php.ini

[mail function]
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

then

$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxx@gmail.com',// your mail name
'smtp_pass' => '*****',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
'wordwrap' => TRUE
);

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

Mail Settings in XAMPP(Important)

$this->email->from('mygmail@gmail.com', 'myname');//your mail address and name
$this->email->to('target@gmail.com'); //receiver mail

$this->email->subject('testing');
$this->email->message($message);

$this->email->send(); //sending mail

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

Send Email Using CodeIgniter 3.1.10

It's maybe because of the wrong username and password given in SMTP authentication or SMTP port blocked in mail server firewalls.

If you are using Gmail account, please check this link and turn off the "Allow less secure apps".

Here is the URL: https://myaccount.google.com/lesssecureapps

Then try to send email again.

I have updated your code and check now:

private function _sendEmail() {
$config = [
'mailtype' => 'html',
'charset' => 'utf-8',
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com',
'smtp_user' => '****',
'smtp_pass' => '****',
'smtp_port' => '465',
'smtp_timeout' => '20',
'validation' => TRUE,
'newline' => "\r\n"
];
$this->load->library('email', $config);
$this->email->initialize($config);
$this->email->from('vcopadangpariaman@gmail.com', 'VCO Padang Pariaman');
$this->email->to('taufikleon44@gmail.com');
$this->email->subject('Testing');
$this->email->message("hallo");
if ($this->email->send()) {
return true;
} else {
echo $this->email->print_debugger();
}
}

Send email by using codeigniter library via localhost

Please check my working code.

function sendMail()
{
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxx@gmail.com', // change it to yours
'smtp_pass' => 'xxx', // change it to yours
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);

$message = '';
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('xxx@gmail.com'); // change it to yours
$this->email->to('xxx@gmail.com');// change it to yours
$this->email->subject('Resume from JobsBuddy for your Job posting');
$this->email->message($message);
if($this->email->send())
{
echo 'Email sent.';
}
else
{
show_error($this->email->print_debugger());
}

}

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.



Related Topics



Leave a reply



Submit