【问题标题】:Mail not sending with PHPMailer over SSL using SMTP邮件未使用 SMTP 通过 SSL 通过 PHPMailer 发送
【发布时间】:2013-09-03 07:46:47
【问题描述】:

我正在尝试使用 PHPMailer 通过 SMTP 发送电子邮件,但到目前为止还没有成功。我经历了许多 SO 问题、PHPMailer 教程和论坛帖子,但仍然无法使其正常工作。为了节省时间,我将尽可能多地记录我的失败尝试,但首先是我正在使用的代码:

<?php
    session_start();
    error_reporting(E_ALL);
    ini_set('display_errors','On');

    require('includes/class.phpmailer.php');
    include('includes/class.smtp.php');
    $mail = new PHPMailer(); 

    $name = $_POST["name"];
    $guests = $_POST["guests"];
    $time = $_POST["time"];

    $message = "<h1>".$name." has booked a table for ".$guests." at ".$time."</h1>";

    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->Host       = "ssl://smtp.gmail.com"; // SMTP server
    $mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->Port       = 26;                    // set the SMTP port for the GMAIL server
    $mail->Username   = "myEmail@gmail.com"; // SMTP account username
    $mail->Password   = "myPassword";        // SMTP account password
    $mail->SetFrom('myEmail@gmail.com', 'James Cushing');
    $mail->AddReplyTo("myEmail@gmail.com","James Cushing");
    $mail->Subject    = "PHPMailer Test Subject via smtp, basic with authentication";
    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!";
    $mail->MsgHTML($message)
    $address = "myOtherEmail@me.com";
    $mail->AddAddress($address, "James Cushing");

    if(!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message sent!";
    }
?>

首先,当我现在运行这段代码时,我得到了两个不同的错误。在我的本地服务器上,我收到错误:
SMTP -> 错误:无法连接到服务器:操作超时 (60)
以下发件人地址失败:myEmail@gmail.com:在未连接的情况下调用 Mail()
邮件程序错误:以下发件人地址失败:myEmail@gmail.com:在未连接的情况下调用 Mail()

我在我的网络服务器上运行相同的代码时遇到同样的错误,但第一行是:
SMTP -> 错误:无法连接到服务器:网络无法访问 (101)

显然,值得指出的是,我没有使用文字“myEmail@gmail.com”,而是用我自己的电子邮件代替了这篇文章。

我尝试过的事情
- 使用 iCloud SMTP 服务器
- 使用不同的端口
- 在我的 php.ini 文件中启用 OpenSSL 扩展
- 从各种 PHPMailer 示例中复制代码
- 使用 Google 的“DisplayUnlockCaptcha”系统启用连接
- 发送到和从不同的地址 - 从用户名属性中删除“@gmail.com” - 其他一些我不记得的事情

这已经让我发疯了大约一天,所以如果有人能解决它,他们将成为英雄。

谢谢

【问题讨论】:

  • 如果将端口更改为 465 会怎样?
  • @userNOID 这是我尝试过的忘记提及的事情之一。它给了我这个:SMTP -> 错误:服务器不接受 EHLO:SMTP -> 错误:服务器不接受 HELO:SMTP -> 错误:服务器不接受 AUTH:SMTP -> 注意:检查时发现 EOF如果已连接
  • 是否支持您的帐户 SMTP?
  • @userNOID 是的,我也通过 MacOS Mail 运行它,我可以从那里发送电子邮件就好了
  • IIRC Gmail 使用 SSL,因此您需要 $mail-&gt;SMTPSecure = 'tls';$mail-&gt;SMTPSecure = 'ssl';

标签: php email ssl smtp phpmailer


【解决方案1】:
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Username = "myemail@gmail.com";
$mail->Password = "**********";
$mail->Port = "465";

这是一个有效的配置。

尝试替换你所拥有的

【讨论】:

  • 感谢 ybert,这实际上为我指明了正确的方向,现在已修复!我 99.999% 确定我之前有完全相同的配置失败,但也许它是后端更改的组合。我不得不添加$address = "myOtherEmail@me.com"; $mail-&gt;AddAddress($address, "James Cushing"); $message = "Email body"; $mail-&gt;MsgHTML($message); if(!$mail-&gt;Send()) { echo "Mailer Error: " . $mail-&gt;ErrorInfo; } else { echo "Message sent!"; } 但之后它在本地工作。现在在我的网络服务器上测试
  • 好的,在我的网络服务器上运行它仍然会给我 SMTP -> 错误:无法连接到服务器:网络无法访问 (101) 我们已经完成了一半!
  • 这是另一个错误。您的网络服务器上的 SMTP 服务器配置不正确。但是代码很好。你不必改变它!
  • 我认为错误是您的端口“465”在您的服务器上被阻止。你必须修复它。
  • 詹姆斯,我认为 ybert 是对的。尝试在端口 465 上打开从服务器到 smtp.gmail.com 的 telnet 连接。它可以连接吗?
【解决方案2】:

不要在端口 465 上使用 SSL,它自 1998 年以来已被弃用,仅用于未获得备忘录的 Microsoft 产品;在端口 587 上使用 TLS:因此,下面的代码应该非常适合您。

mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "smtp.gmail.com"; // SMTP server

$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "tls";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 587;                   // set the SMTP port for the 

【讨论】:

  • 如果使用特定的网络主机,有些主机默认使用 465。
  • 他们的支持应该能让你四处走走。
【解决方案3】:

首先,为 Google 使用这些设置:

$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls"; //edited from tsl
$mail->Username = "myEmail";
$mail->Password = "myPassword";
$mail->Port = "587";

另外,你设置了什么防火墙?

如果您要过滤掉 TCP 端口 465/995,可能还有 587,您需要配置一些例外或将它们从您的规则列表中删除。

https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

【讨论】:

  • $mail->SMTPSecure = "tsl";应该是 $mail->SMTPSecure = "tls";
【解决方案4】:

每当我的客户端计算机更改网络连接(例如,家庭网络与办公室网络)并且以某种方式重新启动网络服务(或重新启动计算机)为我解决问题时,我都会遇到类似的 SMTP 故障。不确定这是否适用于您的情况,但以防万一。

sudo /etc/init.d/networking restart   # for ubuntu

【讨论】:

    【解决方案5】:

    首先,谷歌创建了“使用不太安全的账户方法”功能:

    https://myaccount.google.com/security

    然后创建另一个权限:

    https://accounts.google.com/b/0/DisplayUnlockCaptcha

    希望对你有帮助。

    【讨论】:

    • 这是大约 4 年前的问题,但希望搜索相同问题的人会发现这很有帮助:)
    • 谢谢。我也这么认为。 :)
    猜你喜欢
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-28
    • 2017-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多