Sending email Codeigniter Error

2019-04-07 22:26发布

问题:

I'm trying to send email with my codeigniter project from my localhost, but it is always showing error like this:

    220 mx.google.com ESMTP pp9sm11498734pbb.65 - gsmtp 

hello: 250-mx.google.com at your service, [202.43.95.33]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
Failed to authenticate password. Error: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 http://support.google.com/mail/bin/answer.py?answer=14257 pp9sm11498734pbb.65 - gsmtp 
from: 530-5.5.1 Authentication Required. Learn more at
530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 pp9sm11498734pbb.65 - gsmtp
The following SMTP error was encountered: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 pp9sm11498734pbb.65 - gsmtp 
to: 530-5.5.1 Authentication Required. Learn more at
530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 pp9sm11498734pbb.65 - gsmtp
The following SMTP error was encountered: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 pp9sm11498734pbb.65 - gsmtp 
data: 530-5.5.1 Authentication Required. Learn more at
530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 pp9sm11498734pbb.65 - gsmtp
The following SMTP error was encountered: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 pp9sm11498734pbb.65 - gsmtp 
502 5.5.1 Unrecognized command. pp9sm11498734pbb.65 - gsmtp 
The following SMTP error was encountered: 502 5.5.1 Unrecognized command. pp9sm11498734pbb.65 - gsmtp 
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.
User-Agent: CodeIgniter
Date: Mon, 19 Jan 2015 12:49:08 +0100
From: "andhika" <mytest@gmail.com>
Return-Path: <mytest@gmail.com>
To: coba@gmail.com
Subject: =?utf-8?Q?test?=
Reply-To: "mytest@gmail.com" <mytest@gmail.com>
X-Sender: mytest@gmail.com
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <54bcef349adf6@gmail.com>
Mime-Version: 1.0


Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit

world

This is my controller:

$this->load->library('email');
    $config = array(
                'protocol'  =>  'smtp',
                'smtp_host' =>  'ssl://smtp.googlemail.com',
                'smtp_user' =>  'mytest@gmail.com',
                'smtp_pass' =>  'youpassword',
                'smtp_port' =>  '465'
                );
    $this->email->initialize($config);
    $this->email->set_newline('\r\n');

    $this->email->from('mytest@gmail.com', 'andhika');
    $this->email->to('coba@gmail.com');
    $this->email->subject('test');
    $this->email->message('world');


    if ($this->email->send()){
         echo 'Your e-mail has been sent';
    }         
    else{
        show_error($this->email->print_debugger());
    }

This is in my php.ini setting: extension=php_openssl.dll

Any Answer?

Many Thanks...

回答1:

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

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

Forum

stackoverflow

You can have a look at this Mailgun. It looks quit easy to use and secure.



回答2:

If you are using gmail, your gmail account must be changed to allow "access for less secure apps".

Info from gmail

Change account access for less secure apps
To help keep Google Apps users' accounts secure, we may block less secure apps from accessing Google Apps accounts. As a Google Apps user, you will see a "Password incorrect" error when trying to sign in. If this is the case, you have two options:

Option 1: Upgrade to a more secure app that uses the most up to date security measures. All Google products, like Gmail, use the latest security measures.

Option 2: Change your settings to allow less secure apps to access your account. We don't recommend this option because it might make it easier for someone to break into your account. If you want to allow access anyway, follow these steps: Go to the "Less secure apps" section in My Account. Next to "Access for less secure apps," select Turn on.
(Note to Google Apps users: This setting is hidden if your administrator has locked less secure app account access.) If you still can't sign in to your account, the "password incorrect" error might be caused by a different reason.



回答3:

In case anyone is interested, I have found that using single quotes while setting newline, such as

$this->email->set_newline('\r\n');

Throws a password error. It needs double quotes:

$this->email->set_newline("\r\n");

Or it can be initialized in the $config array with double quotes:

$config = array('newline' => "\r\n");


回答4:

Sending email with a local-host with codeigniter:

If you are using wampp / xampp you need to set up your default email and user name in send mail ini file and php ini file

Local host most of the time will not let you send mail if you have not set up the features below even if you set email up in codeingiter config.

Here is how you can set up send mail on local you tube tutorial I found to help me send email on local host https://www.youtube.com/watch?v=TO7MfDcM-Ho

xammp/wamp Windows Only

php folder go to php.ini find on line 1142 uncomment

;sendmail_path = "\"C:\Xampp\sendmail\sendmail.exe\" -t"

to

sendmail_path = "\"C:\Xampp\sendmail\sendmail.exe\" -t"

sendmail folder: sendmail.ini file

Add Your own details

line 14: smtp_server=gmail.com hostserver

line 18: smtp_port=465 or 587

and on line 46 and 47 you need to add username and

passwordauth_username= username@demo.com

auth_password= demo

Once that is all done you should be able to set up your codeigniter mail config and then send mail. So long as the sendmail.ini and php.ini have made changes to xampp/wampp



回答5:

Sending email in Codeigniter is easy you just need two steps:

Step#1: Create the Controller

<?php
class Sendingemail_Controller extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->helper('form');
    }
    public function index() {
        $this->load->helper('form');
        $this->load->view('contact_email_form');
    }
    public function send_mail() {
        $from_email = "email@example.com";
        $to_email = $this->input->post('email');
        //Load email library
        $this->load->library('email');
        $this->email->from($from_email, 'Identification');
        $this->email->to($to_email);
        $this->email->subject('Send Email Codeigniter');
        $this->email->message('The email send using codeigniter library');
        //Send mail
        if($this->email->send())
            $this->session->set_flashdata("email_sent","Congragulation Email Send Successfully.");
        else
            $this->session->set_flashdata("email_sent","You have encountered an error");
        $this->load->view('contact_email_form');
    }
}
?>

Step#2: Create the View

<html>
<head>    
    <title> Send Email Codeigniter </title>
</head>
<body>
<?php
echo $this->session->flashdata('email_sent');
echo form_open('/Sendingemail_Controller/send_mail');
?>
<input type = "email" name = "email" required />
<input type = "submit" value = "SEND MAIL">
<?php
echo form_close();
?>
</body>
</html>

Step#3: Make the changes in the routes.php file in application/config/routes.php and add the following line at the end of the file:

$route['email'] = 'Sendingemail_Controller';

For more details see the source



回答6:

It works for me. I am running on xampp windows.

I changed some part like this

php folder go to php.ini find on line 1142 uncomment

;sendmail_path = "\"C:\Xampp\sendmail\sendmail.exe\" -t"

to

sendmail_path = "\"C:\Xampp\sendmail\sendmail.exe\" -t"
sendmail folder: sendmail.ini file

Add your own details

line 14: smtp_server=gmail.com hostserver

line 18: smtp_port=465 or 587 and on line 46 and 47 you need to add username and

passwordauth_username= ***@***.com //change to yours

auth_password= ****** ////change to yours

And I changed gmail setting because I use gmail.

Change your settings to allow less secure apps to access your account. We don't recommend this option because it might make it easier for someone to break into your account.

If you want to allow access anyway, follow these steps:

  1. Go to the "Less secure apps" section in My Account.
  2. Next to "Access for less secure apps," select Turn on.

(Note to Google Apps users: This setting is hidden if your administrator has locked less secure app account access.)

If you still can't sign in to your account, the "password incorrect" error