简体   繁体   English

使用PHP使用PHPmailer发送邮件时遇到身份验证错误

[英]Getting authentication error while sending mail with PHPmailer using PHP

I am getting the following error while sending the email using PHPMailer . 使用PHPMailer发送电子邮件时出现以下错误。

Error: 错误:

SMTP Error: Could not authenticate. SMTP错误:无法验证。

I am explaining my code below. 我在下面解释我的代码。

<?php
require_once('/var/www/oditek.in/subhra/phpmailer/class.phpmailer.php');
function SentMail($to,$from,$subject,$msg_body,$reply_to='',$cc='',$files=''){      
    $mail = new PHPMailer();
    $body = $msg_body;
    $mail->IsSMTP();
    $mail->Host       = "smtp.sendgrid.net";
    $mail->SMTPAuth   = true;                    
    $mail->SMTPSecure = 'tls';   
    $mail->Port       = 587;
    $mail->Username   = "tuurbus@gmail.com"; 
    $mail->Password   = "abcd_bs@123";
    $mail->SetFrom($from,'tuurbus');
    if($reply_to!=''){
        $mail->AddReplyTo($reply_to,'tuurbus');
    }
    $mail->Subject    = $subject;
    $mail->MsgHTML($body);
    $address = $to;
    $mail->AddAddress($address);
    if(count($files) > 0 && $files!=''){
        for($i=0;$i<=count($files);$i++){
            if(is_file($files[$i])){
                $mail->AddAttachment($files[$i]);
            }
        }
    }
    if($cc!=''){
        $addrcc = explode(',',$cc);
        foreach ($addrcc as $addresscc) {
            $mail->AddCC(trim($addresscc));
        }
    }
    if($mail->Send()){
        return 1;
    }else{
        return 0;
    }
}
$to="tuurbus@gmail.com";
$from="subhrajyotipradhan@gmail.com";
$subject="Test email";
$msg_body="Hi, This is customize request";
$ret = SentMail($to,$from,$subject,$msg_body);
echo $ret;exit;
?>

I have also turned on the the less secured app option in gmail but still same error is coming. 我还打开了gmail中less secured app选项,但仍会出现相同的错误。 This is the implementation like contact us form in website. 这是类似于网站中contact us表格的实现。 User will send the email request to admin( here tuurbus ). 用户会将电子邮件请求发送给admin( here tuurbus )。 Please help me to resolve this issue. 请帮助我解决此问题。

Download Phpmailer package from https://github.com/PHPMailer/PHPMailer/ link & copy into your project and extract zip folder into your project and change the your code as following also check your password ,email id. https://github.com/PHPMailer/PHPMailer/下载Phpmailer软件包,链接并复制到您的项目中,然后将zip文件夹解压缩到您的项目中,然后按照以下步骤更改代码,同时检查密码,电子邮件ID。 Its works by my side. 它在我身边工作。

<?php
include_once('PHPMailerAutoload.php');
function SentMail($to,$from,$subject,$msg_body,$reply_to='',$cc='',$files=''){      
    $mail = new PHPMailer();
    $body = $msg_body;
    $mail->IsSMTP();
    $mail->Host       = "smtp.gmail.com";
    $mail->SMTPAuth   = true;                    
    $mail->SMTPSecure = 'tls';   
    $mail->Port       = 587;
    $mail->Username   = "tuurbus@gmail.com"; 
    $mail->Password   = "abcd_bs@123";
    $mail->SetFrom($from,'tuurbus');
    if($reply_to!=''){
        $mail->AddReplyTo($reply_to,'tuurbus');
    }
    $mail->Subject    = $subject;
    $mail->MsgHTML($body);
    $address = $to;
    $mail->AddAddress($address);
    if(count($files) > 0 && $files!=''){
        for($i=0;$i<=count($files);$i++){
            if(is_file($files[$i])){
                $mail->AddAttachment($files[$i]);
            }
        }
    }
    if($cc!=''){
        $addrcc = explode(',',$cc);
        foreach ($addrcc as $addresscc) {
            $mail->AddCC(trim($addresscc));
        }
    }
    if($mail->Send()){
        return 1;
    }else{
        return 0;
    }
}
$to="tuurbus@gmail.com";
$from="subhrajyotipradhan@gmail.com";
$subject="Test email";
$msg_body="Hi, This is customize request";
$ret = SentMail($to,$from,$subject,$msg_body);
echo $ret;exit;
?>

Try: 尝试:

$mail->Host = gethostbyname('smtp.gmail.com'); 
// if your network does not support SMTP over IPv6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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