繁体   English   中英

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

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

使用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 )。 请帮助我解决此问题。

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

尝试:

$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