首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SMTP错误:无法验证

问SMTP错误:无法验证
EN

Stack Overflow用户
提问于 2015-05-18 13:51:22
回答 1查看 1.8K关注 0票数 2

我的mail()函数有一些问题,所以当我尝试用PHPmailer发送邮件时,我从一个教程中复制的下面的代码会给我带来错误

代码语言:javascript
复制
<?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.';
}
?>

当我运行它时,我会得到这样的错误:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2015-05-18 14:19:21

注意事项:编辑

您必须在上面的代码中包括下面一行,然后再检查一遍。

代码语言:javascript
复制
$mail->SMTPSecure = 'tls';

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

代码语言:javascript
复制
   $mail = new PHPMailer(true);

然后在try块中放置您发送电子邮件的代码,然后您就可以捕获这样的异常

代码语言:javascript
复制
catch (phpmailerException $e) {
  echo $e->errorMessage(); //PHPMailer error messages
} catch (Exception $e) { 
  echo $e->getMessage(); //other error messages
}

从这里获取最新的PHPMailer示例

最新的PHPMailer示例

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

代码语言:javascript
复制
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);
       }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30305038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档