Reputation: 73
I am trying to send email using smtp codeigniter. The code I am using is as below:
public function notify_marketing(){
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => '[email protected]',
'smtp_pass' => '*******',//my valid email password
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->email->initialize($config);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('[email protected]');
$this->email->to('[email protected]');
$this->email->subject('My Subject');
$this->email->message('Hello there');
if($this->email->send())
{
$this->session->set_flashdata("success","Email sent.");
}
else
{
show_error($this->email->print_debugger());
}
}
However I am getting the following error in the response. I tried other solutions from this site but didn't work.
<div id="exception_error">
<h1><span class="type">An Error Was Encountered [ 500 ]</span></h1>
<div class="content">
<p><p>220 smtp.googlemail.com ESMTP bv4sm16669443pbb.86 - gsmtp
<br /><pre>hello: 250-smtp.googlemail.com at your service, [110.44.127.179]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN XOAUTH
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
</pre>lang:email_smtp_auth_pw<br />lang:email_send_failure_smtp<br /><pre>User-Agent: CodeIgniter
Date: Tue, 18 Aug 2015 12:03:42 +0545
From: <********@gmail.com>
Return-Path: <********@gmail.com>
To: *******@gmail.com
Subject: =?iso-8859-1?Q?My_Subject?=
Reply-To: "********@gmail.com" <*********@gmail.com>
X-Sender: *******@gmail.com
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <[email protected]>
Mime-Version: 1.0
Thank you in advance.
Upvotes: 5
Views: 7972
Reputation: 7675
The code is perfect.
You need to use Application password of gmail not the gmail password. Please set a application password from gmail settings and give the password here.
It will work perfectly then. See More from HERE
Process to setting up a app password in gmail:
Once you are finished, you’ll won’t see that App password code again. However, you will see a list of apps and devices you’ve created App passwords for.
Upvotes: 6
Reputation: 5444
Try this..
Create a file named 'email.php' inside 'application/config' folder and add the below settings to it.
<?php
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com'; //change this
$config['smtp_port'] = '465';
$config['smtp_user'] = '[email protected]'; //change this
$config['smtp_pass'] = 'password'; //change this
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n";
?>
In Controller:
<?php
//send mail
function sendmail()
{
$this->load->library('email'); // load email library
$this->email->from('[email protected]', 'sender name');
$this->email->to('[email protected]');
$this->email->cc('[email protected]');
$this->email->subject('Your Subject');
$this->email->message('Your Message');
$this->email->attach('/path/to/file1.png'); // attach file
$this->email->attach('/path/to/file2.pdf');
if ($this->email->send())
echo "Mail Sent!";
else
echo "There is error in sending mail!";
}
?>
Upvotes: 0