简体   繁体   中英

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' => 'myvalidemail@gmail.com', 
                    '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('myvalidemail@gmail.com'); 
    $this->email->to('validreceiptent@gmail.com');
    $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: <55d2ce42747f5@gmail.com>
Mime-Version: 1.0

Thank you in advance.

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.

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'] = 'user@gmail.com'; //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('user@gmail.com', 'sender name');
    $this->email->to('test1@gmail.com');
    $this->email->cc('test2@gmail.com'); 
    $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!";
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM