【问题标题】:PHPMailer to use Gmail as SMTP server.Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP hostPHPMailer 使用 Gmail 作为 SMTP 服务器。无法连接到 SMTP 主机。邮件程序错误:SMTP 错误:无法连接到 SMTP 主机
【发布时间】:2011-09-04 08:41:46
【问题描述】:

我正在尝试使用 phpMailer 通过电子邮件向用户发送确认消息。我的代码是这样的:

<?php
include("class.phpmailer.php");
include("class.smtp.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "ssl://smtp.gmail.com"; // specify main and backup server
$mail->Port = 465; // set the port to use
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "sender@gmail.com"; // your SMTP username or your gmail username
$mail->Password = "mypasswrord"; // your SMTP password or your gmail password
$from = "webmaster@example.com"; // Reply to this email
$to="receiver@yahoo.com"; // Recipients email ID
$name="Jersey Name"; // Recipient's name
$mail->From = $from;
$mail->FromName = "Webmaster"; // Name to indicate where the email came from when the recepient received
$mail->AddAddress($to,$name);
$mail->AddReplyTo($from,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Sending Email From Php Using Gmail";
$mail->Body = "This Email Send through phpmailer, This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>

我已经在 php.ini 中启用了 ssl。

PS> sender@gmail.com 是保护隐私的掩码电子邮件。但我确实在那部分放了一个真实的电子邮件地址

【问题讨论】:

  • 您考虑过使用swiftmailer.org 吗?它可以开箱即用地通过 GMail 发送电子邮件。很棒的图书馆! :-)
  • 另外,将主机从“ssl://smtp.gmail.com”更改为“smtp.gmail.com”
  • 什么也没发生……甚至没有回声
  • jamie 你能给我一个使用 swiftmailer 的简单示例代码吗?

标签: php ssl smtp gmail phpmailer


【解决方案1】:

来自here

2)在class.phpmailer.php中注释掉以下几行代码

/*
if(strstr($hosts[$index], ":"))
list($host, $port) = explode(":",
$hosts[$index]); else 
*/

如果你还没有,试试这个。

【讨论】:

  • 该行在我的 class.phpmailer.php 中不存在
【解决方案2】:

This page 有作者声称“工作完美”的代码。

我看到他和你之间唯一真正的区别是他有:

$mail->Mailer = "smtp";

我想我会从他的代码开始,看看它是否有效,然后从那里调试。

【讨论】:

  • 所以他的代码对他有效,但对你无效?然后环境中有一些不同的东西......尝试telnet smtp.gmail.com 465以确保它不是防火墙问题。下一个最可能的原因是无法验证服务器的 SSL 证书。
【解决方案3】:

在您的 php.ini 中,确保您已取消注释该行

extension=php_openssl.dll

【讨论】:

  • 这仅适用于windows...哪里适用于linux
  • 对于 Linux:extension=php_openssl.so
【解决方案4】:

错误:

$mail->Host = "ssl://smtp.gmail.com"; // specify main and backup server

好:

$mail->Host = "smtp.gmail.com"; // specify main and backup server

【讨论】:

  • 这对我不起作用。它仅适用于包含的“ssl://”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多