【问题标题】:Gmail smtp is not working in phpmailer [duplicate]Gmail smtp 在 phpmailer 中不起作用 [重复]
【发布时间】:2017-12-07 21:12:48
【问题描述】:

这是我的代码,

require_once("PHPMailer/class.PHPMailer.php");
$mail = new PHPMailer();
/* receiver details and message - start */
$toid = "example@gmail.com";
$toname = "Example";

$msg = "First name : ".$_POST["fname"] . "<br>";
$msg .= "Last name : ".$_POST["lname"]. "<br>";;
$msg .= "Email : ".$_POST["email"]. "<br>";;
$msg .= "Mobile : ".$_POST["mobile"]. "<br>";;
$msg .= "Message : ".$_POST["msg"];
$subject = "sample subject";
/* receiver details and message - end */

$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1;  // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true;  // authentication enabled
$mail->SMTPKeepAlive = true;  

/* mail smtp configuration - start */
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587; 
$mail->Username = 'test@gmail.com';  
$mail->Password = 'xxxxxx';
/* mail smtp configuration - end */

$mail->From = "test@gmail.com";
$mail->FromName = "from name";


$mail->AddAddress($toid, $toname);

$mail->Subject = $subject;
$mail->Body    = $msg;
$mail->IsHTML(true); 
if($mail->Send()){
echo "success";
}else{
echo "Failure";
}

当运行上面的时候,它会抛出

stream_socket_enable_crypto():SSL 操作失败,代码为 1。OpenSSL 错误消息:错误:14090086:SSL 例程:ssl3_get_server_certificate:certificate verify failed in C:\xampp\htdocs\mail\PHPMailer\class.smtp.php 第 200 行

【问题讨论】:

  • 我不是 PHP 人,但在 C# 中尝试通过 gmail 发送电子邮件时遇到了类似的问题。在我的请求中设置 SSL=true 后解决。
  • 上周出现了一系列类似的问题 - 我怀疑 gmail 更改了许多 PHP 安装中缺少的根证书,因此请尝试更新您的 CA 证书作为故障排除指南和 PHP 文档说。不是这个问题,但是你使用的 PHPMailer 版本很旧,所以获取最新版本。
  • 我检查了最新的也抛出了同样的错误@Synchro
  • 这就是为什么我说“不是这个问题”。修复你的证书,按照指南所说的去做。

标签: php phpmailer


【解决方案1】:

这与您的代码无关 - 您的 OpenSSL CA 证书存储不包含 CA 证书和/或您的系统在伪装 gmail 服务的环境中运行和/或您的时钟非常 em> 错误和/或您使用的是 非常 旧版本的 openSSL。您没有告诉我们有关此平台的任何信息,因此我们无法建议如何解决此问题。

顺便说一句,您还需要在 google 端启用“不安全”连接 - 我认为 PHPmailer 不支持 SMTP 上的 oauth。

【讨论】:

  • PHPMailer 已经支持 SMTP XOAUTH2 好几年了。请参阅this example,但这与验证 Gmail 的证书没有任何关系,这在任何身份验证之前很久就发生了。
  • 我已经在 google 端启用了“不安全”连接...bt 还没有工作。
  • @Renu:您的客户端必须先连接,然后才能进行身份验证。
  • @symcbean :我没明白..?
【解决方案2】:

对于 GMail,将您的设置更改为:

$mail->SMTPSecure = 'ssl';
$mail->Port = 465; 

【讨论】:

  • 它抛出相同的错误,SMTP -> 错误:无法连接到服务器:(0) SMTP 错误:无法连接到 SMTP 主机。失败
  • 不。另外,不要发布无法解释的代码作为答案,说为什么你认为它可以解决问题,尽管在这种情况下它没有。
猜你喜欢
  • 2012-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-31
  • 2021-11-28
  • 2017-10-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多