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

时间:2018-10-13 07:35:13

标签: php phpmailer

使用PHPMailer发送电子邮件时出现以下错误。

  

错误:

SMTP错误:无法验证。

我在下面解释我的代码。

<?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;
?>

我还打开了gmail中的less secured app选项,但是仍然出现相同的错误。这是网站中contact us形式的实现。用户会将电子邮件请求发送给admin(here tuurbus)。请帮助我解决此问题。

2 个答案:

答案 0 :(得分:-1)

https://github.com/PHPMailer/PHPMailer/下载Phpmailer程序包,链接并复制到您的项目中,然后将zip文件夹解压缩到您的项目中,然后按照以下步骤更改代码,并检查密码,电子邮件ID。它在我的身边。

<?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;
?>

答案 1 :(得分:-1)

尝试:

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