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

时间:2015-05-18 13:51:22

标签: 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
$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.';
}
?>

当我运行它时,我收到此错误:

2015-05-18 13:46:19 SERVER -> CLIENT: 2015-05-18 13:46:19   SMTP NOTICE: EOF caught while checking if connected 2015-05-18 13:46:19 SMTP Error: Could not authenticate. 2015-05-18 13:46:19 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message was not sent.Mailer error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

1 个答案:

答案 0 :(得分:2)

注意: [编辑]

您必须在上面的代码中包含以下行并再次检查

$mail->SMTPSecure = 'tls';

始终通过传递PHPMailer参数初始化true,因为它可以帮助您捕获异常

   $mail = new PHPMailer(true);

然后在try块中输入您发送电子邮件的代码 然后你可以捕捉这样的例外

catch (phpmailerException $e) {
  echo $e->errorMessage(); //PHPMailer error messages
} catch (Exception $e) { 
  echo $e->getMessage(); //other error messages
}

从这里获取最新的PHPMailer示例

<强> Latest PHPMailer Examples

修改 可能在238左右更改文件class.smtp.php

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);
       }