PHPMailer突然无法正常工作

时间:2017-08-10 17:16:39

标签: php email phpmailer

所以我建立了这个使用PHPMailer发送邮件的应用程序。代码在我的本地机器上完美运行,直到昨天它才在服务器上运行良好。但是现在当我在服务器上运行代码时,我得到了:

无法发送消息.Mailer错误:SMTP connect()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

我真的尝试了一切。我无法弄清楚为什么这不起作用。从作为安装站点的用户的服务器,我可以telnet localhost 465我可以telnet localhost 587所以服务器已启动,并且它没有阻止与自身的连接。

我唯一能想到的是,这是一个Cpanel服务器,某些更新必定发生在apache / PHP上,导致某些东西无法正常使用。我知道问题不在于邮件服务器而不是代码(因为当我从本地机器运行这个确切的代码时它可以工作),这只会在服务器上省略PHP / Apache配置...

有没有人对这可能是什么有任何见解?

PS-我必须使用SSL,这是我所知道的服务器支持的唯一内容,至少我从来没有使用过TLS或未加密的运气。

这是我的代码:

require("vendor/autoload.php");
$mail = new \PHPMailer;
// set up mail settings
$mail->isSMTP();
$mail->Host = 'my.host.com';
$mail->SMTPAuth = true;
$mail->Username = 'username';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;

$mail->Debugoutput = function($str, $level) {
    $GLOBALS['debug'][] = $Host . " - " . $str . " - " . $level;
};

$mail->SMTPDebug = 2;
$mail->CharSet = "UTF-8";
$mail->setFrom(.....);
$mail->addAddress(.....);
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body    = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional //EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><body>This is the HTML message body <b>in bold!</b></body></html>';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

编辑:

我将调试级别更改为3(用于连接调试,derp),我得到了这个:

[0] =&GT;字符串(88)&#34; - 连接:打开ssl://host.host.com:465,超时= 300,选项=数组() - 3&#34;

[1] =&GT;字符串(291)&#34; - 连接失败。错误#2:stream_socket_client():SSL操作失败,代码为1. OpenSSL错误消息:错误:14090086:SSL例程:ssl3_get_server_certificate:证书验证失败[/path/to/vendor/phpmailer/phpmailer/class.smtp.php第294行] - 3&#34;

[2] =&GT; string(175)&#34; - 连接失败。错误#2:stream_socket_client():无法启用加密[/path/to/vendor/phpmailer/phpmailer/class.smtp.php第294行] - 3&#34;

[3] =&GT;字符串(215)&#34; - 连接失败。错误#2:stream_socket_client():无法连接到ssl://my.host.com:465(未知错误)[/ path/to/vendor/phpmailer/phpmailer/class.smtp.php第294行] - 3&# 34;

[4] =&GT;字符串(52)&#34; - SMTP错误:无法连接到服务器:(0) - 1&#34;

[5] =&GT;字符串(89)&#34; - SMTP连接()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting - 3&#34; }

所以我是对的,它的SSL相关......所以任何人都知道如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我们的一个现场网站发生了同样的事情。花了几个小时对代码进行故障排除(我们也使用PHPMailer),但它最终成为数据中心的防火墙更新。检查您的cPanel设置和/或与您的托管服务提供商打开一张票。

如果您没有对代码进行任何更改,并且它突然停止工作,则很可能不是您的代码问题。就我而言,服务器防火墙阻止了出站连接。看看那个。