Php SMTP错误:无法进行身份验证

Php SMTP错误:无法进行身份验证,php,email,phpmailer,Php,Email,Phpmailer,我对mail()函数有一些问题,所以当我尝试使用PHPmailer发送邮件时,下面的代码是我从一个教程中复制的,它给了我错误信息 <?php include("PHPMailer-master/class.phpmailer.php"); include('PHPMailer-master/class.smtp.php'); $mail = new PHPMailer(); $mail->IsSMTP(); // telling the class to use SMTP $mai

我对mail()函数有一些问题,所以当我尝试使用PHPmailer发送邮件时,下面的代码是我从一个教程中复制的,它给了我错误信息

<?php
include("PHPMailer-master/class.phpmailer.php");
include('PHPMailer-master/class.smtp.php');
$mail = new PHPMailer();
$mail->IsSMTP();  // telling the class to use SMTP
$mail->Host     = "smtp.gmail.com"; // SMTP server
$mail->Username   = "nati323@gmail.com";
$mail->Password   = "SOMEPASS";
$mail->SMTPAuth   = true;       
$mail->Port       = 587;
$mail->SMTPDebug  = 2;
$mail->From     = "from@example.com";
$mail->AddAddress("nati323@gmail.com");
$mail->Subject  = "First PHPMailer Message";
$mail->Body     = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>

注意:[编辑]

您必须在上述代码中包含以下行,然后再次检查

$mail->SMTPSecure = 'tls';
始终通过传递
true
参数初始化
PHPMailer
,因为它有助于捕获异常

   $mail = new PHPMailer(true);
然后在
中尝试
阻止输入您发送电子邮件的代码 然后,您可以捕获这样的异常

catch (phpmailerException $e) {
  echo $e->errorMessage(); //PHPMailer error messages
} catch (Exception $e) { 
  echo $e->getMessage(); //other error messages
}
从这里获取最新的PHPMailer示例

编辑: 更改class.smtp.php文件,可能在238行左右

public function connect($host, $port = null, $timeout = 30, $options = array()) {
       if (count($options) == 0) {
           $options['ssl'] = array('verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true);
       }

我敢打赌你的
from
不会是
from@example.com
。它可能必须是
nati323@gmail.com
。Gmail不允许你从其他地址发送邮件。也许你是对的,但问题似乎出在SMTP连接上。检查你的电子邮件帐户。如果你收到一封由谷歌发送的电子邮件,可以通过不太安全的应用程序进行访问…启用该功能,然后重试..Gmail需要tls连接,请尝试添加
$mail->SMTPSecure='tls'public function connect($host, $port = null, $timeout = 30, $options = array()) {
       if (count($options) == 0) {
           $options['ssl'] = array('verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true);
       }