使用代码igniter和gmail smtp在localhost上发送来自xampp的电子邮件

时间:2021-01-29 18:12:25

I am trying to create and test email functionality in a code igniter app on localhost using xampp server. I am trying to use the Google SMTP mail server by configuring it in php.ini and sendmail.ini. I am getting lots of errors which I cannot understand. My gmail account uses 2 step authentication. does that matter? Here's my code

我正在尝试使用xampp服务器在localhost上的代码点火器应用程序中创建和测试电子邮件功能。我试图通过在php.ini和sendmail.ini中配置它来使用Google SMTP邮件服务器。我收到很多我无法理解的错误。我的Gmail帐户使用2步验证。那有关系吗?这是我的代码

Controller:

$this->load->library('email');
$from =$this->input->post('email');

$econfig=
    array(
        'protocol'=>'smtp', 
        'smtp_host'=>"smtp.gmail.com",
        'smtp_port'=>465,
        '_smtp_auth'=>TRUE, 
        'smtp_user'=>"deb.pratyush@gmail",
        'smtp_pass'=>"xxxxxxxx",
        'smtp_crypto'=>'ssl',               
        'mailtype'=>'html', 
        'charset'=>'utf-8',
        'validate'=>TRUE);


        $this->email->initialize($econfig);
        $this->email->set_newline("\r\n");
        $msg=$this->input->post('msg');

        $subject = "Contact Request From ".$from ;


        $name=$this->input->post('uname');

        $this->email->to("deb.pratyush@gmail.com");
        $this->email->from($from, stripslashes($name));
        $this->email->subject($subject);
        $this->email->message($msg);

        if($this->email->send())
        {
            $this->session->set_flashdata('msg', "Message Sent");
            $url = base_url().'cms/contact-us';
            redirect($url);
        }
        else{

            echo "fail<br>";
            //echo "m=".mail("deb.pratyush@gmail.com", $subject, $msg)."<br>";
            echo $this->email->print_debugger();
        }

PHP INI:

SMTP = smtp.gmail.com
smtp_port = 465
sendmail_from = deb.pratyush@gmail.com   
sendmail_path = "\"D:\xampp\sendmail\sendmail.exe\" -t"

SENDMAIL INI:

smtp_server=smtp.gmail.com
smtp_port=465
smtp_ssl=auto

error_logfile=error.log
debug_logfile=debug.log

auth_username=deb.pratyush@gmail.com
auth_password=xxxxxxxx
force_sender=deb.pratyush@gmail.com

ERRORS:

fail
220 mx.google.com ESMTP jp10sm8036081pbb.9 - gsmtp

hello: 250-mx.google.com at your service, [223.239.160.146]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN XOAUTH
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 jp10sm8036081pbb.9 - 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     jp10sm8036081pbb.9 - 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 jp10sm8036081pbb.9 - 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 jp10sm8036081pbb.9 - 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 jp10sm8036081pbb.9 - 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 jp10sm8036081pbb.9 - 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 jp10sm8036081pbb.9 - gsmtp
502 5.5.1 Unrecognized command. jp10sm8036081pbb.9 - gsmtp
The following SMTP error was encountered: 502 5.5.1 Unrecognized command. jp10sm8036081pbb.9 - gsmtp
 Unable to send email using PHP SMTP. 
Your server might not be configured to send mail using this method.

User-Agent: CodeIgniter
Date: Sat, 2 May 2015 18:31:32 +0200
To: deb.pratyush@gmail.com
From: "John D\'Costa" <jd@mailinator.com>
Return-Path: <jd@mailinator.com>
Subject: =?utf-8?Q?Contact_Request_From_jd@mailinator.com?=
Reply-To: "jd@mailinator.com" <jd@mailinator.com>
X-Sender: jd@mailinator.com
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <5544fbe4a6133@mailinator.com>
Mime-Version: 1.0


Content-Type: multipart/alternative; boundary="B_ALT_5544fbe4a6133"

This is a multi-part message in MIME format.
Your email application may not support this format.

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

tyftyttrtv jhvyugu


--B_ALT_5544fbe4a6133
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

tyftyttrtv jhvyugu

--B_ALT_5544fbe4a6133--

3 个解决方案

#1


I found the solution. My gmail account uses 2 step authentication. So i needed to create an app specific password and substitute my earlier password with the app-specific password to gain full access to my gmail account through PHP. I found the link given below in my error log for sendmail.exe

我找到了解决方案。我的Gmail帐户使用2步验证。因此,我需要创建一个特定于应用程序的密码,并使用应用程序专用密码替换我之前的密码,以通过PHP获得对我的Gmail帐户的完全访问权限。我在sendmail.exe的错误日志中找到了下面给出的链接

https://support.google.com/accounts/bin/answer.py?answer=185833

It tells you about creating app specific passwords in detail

它会告诉您详细创建应用程序特定密码

Code is below:

代码如下:

$econfig=
    array(
        'protocol'=>'smtp', 
        'smtp_host'=>"smtp.gmail.com",
        'smtp_port'=>465,
        '_smtp_auth'=>TRUE, 
        'smtp_user'=>"deb.pratyush@gmail",
        'smtp_pass'=>"<app-specific password>",
        'smtp_crypto'=>'ssl',               
        'mailtype'=>'html', 
        'charset'=>'utf-8',
        'validate'=>TRUE);

SENDMAIL INI:

smtp_server=smtp.gmail.com
smtp_port=465
smtp_ssl=auto

error_logfile=error.log
debug_logfile=debug.log

auth_username=deb.pratyush@gmail.com
auth_password=<app-specific password>
force_sender=deb.pratyush@gmail.com

#2


This lead me on the right track. However,

这引导我走上正轨。然而,

All I had to do was switch the "Allow less secure apps to access your account" to on. I guess this is an alternative to creating an app specific password.

我所要做的就是将“允许安全性较低的应用访问您的帐户”切换为开启状态。我想这是创建应用专用密码的替代方法。

https://support.google.com/accounts/answer/6010255 for info

https://support.google.com/accounts/answer/6010255获取信息

https://www.google.com/settings/security/lesssecureapps for the switch

为交换机https://www.google.com/settings/security/lesssecureapps

Might be worth switching it back off after you're done for security purposes.

为安全起见,完成后可能需要将其关闭。

#3


        $config = array();
        $config['protocol']    = 'smtp';
        $config['smtp_host']    = 'xyz.com';
        $config['smtp_port']    = '25';
        $config['smtp_timeout'] = '100';
        $config['smtp_user']    = 'aaa@xyz.com';
        $config['smtp_pass']    = '*******';
        $config['charset']    = 'utf-8';
        $config['newline']    = "\r\n";
        $config['mailtype'] = 'html'; // or text
        $config['validation'] = TRUE;



       $this->email->initialize($config);
        //$this->email->set_newline("\r\n");
        $this->email->from('aaa@xyz.com', 'TESTING');
        $this->email->to('abc@gmail.com'); 
        $this->email->subject('Email Test');
        $body = read_file(base_url().'resources/img/index.html');
        $this->email->message($body);  

        if($this->email->send())
        {
           echo $this->email->print_debugger(); echo 'mail send'.$body;
        }
        else
        echo $this->email->print_debugger();

       but not recive any mail

#1


I found the solution. My gmail account uses 2 step authentication. So i needed to create an app specific password and substitute my earlier password with the app-specific password to gain full access to my gmail account through PHP. I found the link given below in my error log for sendmail.exe

我找到了解决方案。我的Gmail帐户使用2步验证。因此,我需要创建一个特定于应用程序的密码,并使用应用程序专用密码替换我之前的密码,以通过PHP获得对我的Gmail帐户的完全访问权限。我在sendmail.exe的错误日志中找到了下面给出的链接

https://support.google.com/accounts/bin/answer.py?answer=185833

It tells you about creating app specific passwords in detail

它会告诉您详细创建应用程序特定密码

Code is below:

代码如下:

$econfig=
    array(
        'protocol'=>'smtp', 
        'smtp_host'=>"smtp.gmail.com",
        'smtp_port'=>465,
        '_smtp_auth'=>TRUE, 
        'smtp_user'=>"deb.pratyush@gmail",
        'smtp_pass'=>"<app-specific password>",
        'smtp_crypto'=>'ssl',               
        'mailtype'=>'html', 
        'charset'=>'utf-8',
        'validate'=>TRUE);

SENDMAIL INI:

smtp_server=smtp.gmail.com
smtp_port=465
smtp_ssl=auto

error_logfile=error.log
debug_logfile=debug.log

auth_username=deb.pratyush@gmail.com
auth_password=<app-specific password>
force_sender=deb.pratyush@gmail.com

#2


This lead me on the right track. However,

这引导我走上正轨。然而,

All I had to do was switch the "Allow less secure apps to access your account" to on. I guess this is an alternative to creating an app specific password.

我所要做的就是将“允许安全性较低的应用访问您的帐户”切换为开启状态。我想这是创建应用专用密码的替代方法。

https://support.google.com/accounts/answer/6010255 for info

https://support.google.com/accounts/answer/6010255获取信息

https://www.google.com/settings/security/lesssecureapps for the switch

为交换机https://www.google.com/settings/security/lesssecureapps

Might be worth switching it back off after you're done for security purposes.

为安全起见,完成后可能需要将其关闭。

#3


        $config = array();
        $config['protocol']    = 'smtp';
        $config['smtp_host']    = 'xyz.com';
        $config['smtp_port']    = '25';
        $config['smtp_timeout'] = '100';
        $config['smtp_user']    = 'aaa@xyz.com';
        $config['smtp_pass']    = '*******';
        $config['charset']    = 'utf-8';
        $config['newline']    = "\r\n";
        $config['mailtype'] = 'html'; // or text
        $config['validation'] = TRUE;



       $this->email->initialize($config);
        //$this->email->set_newline("\r\n");
        $this->email->from('aaa@xyz.com', 'TESTING');
        $this->email->to('abc@gmail.com'); 
        $this->email->subject('Email Test');
        $body = read_file(base_url().'resources/img/index.html');
        $this->email->message($body);  

        if($this->email->send())
        {
           echo $this->email->print_debugger(); echo 'mail send'.$body;
        }
        else
        echo $this->email->print_debugger();

       but not recive any mail