使用SMTP通过SSL发送邮件时,PHPMailer无法发送邮件

14

我正在尝试使用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 -> ERROR: Failed to connect to server: Operation timed out (60)
The following From address failed: myEmail@gmail.com : Called Mail() without being connected
Mailer Error: The following From address failed: myEmail@gmail.com : Called Mail() without being connected

在我的Web服务器上运行相同的代码时,我也更或多少会得到相似的错误,但是第一行是:
SMTP -> ERROR: Failed to connect to server: Network is unreachable (101)

显然值得指出的是,我没有使用文字“myEmail@gmail.com”,而是替换为了本帖中的自己的电子邮件地址。

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

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

谢谢


2
如果您将端口更改为465会发生什么? - Lkopo
@userNOID 这是我忘记提到尝试过的事情之一。它给了我这个:SMTP -> 错误:服务器不接受 EHLO:
SMTP -> 错误:服务器不接受 HELO:
SMTP -> 错误:服务器不接受 AUTH:
SMTP -> 注意:在检查连接时捕获到 EOF
- James Cushing
您的账户支持SMTP吗? - Lkopo
@userNOID 是的,我也在通过MacOS Mail运行它,我可以从那里轻松地发送电子邮件。 - James Cushing
2
如果我没记错的话,Gmail 使用 SSL,因此您需要 $mail->SMTPSecure = 'tls';$mail->SMTPSecure = 'ssl'; - symcbean
那就是我在答案中建议的...他也必须像我建议的那样更改主机。 - ybert
5个回答

43
$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->AddAddress($address, "James Cushing"); $message = "Email body"; $mail->MsgHTML($message); if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent!"; }但之后它在本地工作了。现在正在我的Web服务器上进行测试。 - James Cushing
好的,但在我的Web服务器上运行它仍然会出现SMTP -> ERROR: Failed to connect to server: Network is unreachable (101)不过我们已经完成了一半! - James Cushing
这是另一个错误。您的Web服务器上的SMTP服务器配置不正确。但代码是好的。您不需要更改它! - ybert
1
我认为错误在于您的服务器上端口'465'被阻止了。您需要修复它。 - ybert
詹姆斯,我认为ybert是正确的。尝试从您的服务器打开到smtp.gmail.com端口465的telnet连接。它是否连接? - mti2935
显示剩余2条评论

3
不要在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 

2
然而,如果使用特定的Web主机,则其中一些将465作为默认端口。 - OzzyTheGiant
他们的支持应该能够为您提供解决方案。 - Asuquo12

2

首先,使用以下设置来进行谷歌搜索:

$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"; - MicBehrens

1

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

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

0

这是大约4年前的问题,但希望有人搜索相同问题时会发现这很有帮助 :) - James Cushing
谢谢。我也这么认为。 :) - Andre Mesquita

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接