Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Email using SMTP codeigniter

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.

like image 460
R.Karki Avatar asked Aug 18 '15 06:08

R.Karki


People also ask

How to send email using CodeIgniter?

Sending an Email The subject() and message() function is used to set the subject and message of the email. $this->email->from('[email protected]', 'Your Name'); $this->email->to('[email protected]'); $this->email->subject('Email Test'); $this->email->message('Testing the email class.

How to use Mailtrap in CodeIgniter?

To start, create a Mailtrap account and login. Click on Add Inbox , fill in the inbox name and click on Save . Now, click on the gear icon to see the credentials needed to send email with Mailtrap. On the page where you were redirected, click on Integrations list and choose CodeIgniter to view the configuration code.

What is Smtp_user?

'smtp_user' => '[email protected]', the email address that will be used as the sender when sending emails. This should be a valid email address that exists on the server. 'smtp_pass' => '12345! ', the password to the specified smtp user email.


1 Answers

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:

  1. Visit your App passwords page. You may be asked to sign in to your Google Account.
  2. At the bottom, click Select app and choose the app you’re using.
  3. Click Select device and choose the device you’re using.
  4. Select Generate.
  5. Follow the instructions to enter the App password (the 16 character code in the yellow bar) on your device.
  6. Select Done.

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.

like image 115
Ariful Islam Avatar answered Oct 11 '22 15:10

Ariful Islam