SMTP连接失败。邮件未发送。发件人错误:SMTP连接失败。

28

我试图发送邮件到 Gmail 地址,但一直出现这个错误:“SMTP -> ERROR:连接服务器失败:连接超时(110)SMTP Connect() 失败。未发送消息。Mailer 错误:SMTP Connect() 失败。”可能是什么问题?

        require 'class.phpmailer.php'; // path to the PHPMailer class
        require 'class.smtp.php';

            $mail = new PHPMailer();


            $mail->IsSMTP();  // telling the class to use SMTP
            $mail->SMTPDebug = 2;
            $mail->Mailer = "smtp";
            $mail->Host = "ssl://smtp.gmail.com";
            $mail->Port = 587;
            $mail->SMTPAuth = true; // turn on SMTP authentication
            $mail->Username = "myemail@gmail.com"; // SMTP username
            $mail->Password = "mypasswword"; // SMTP password 
            $Mail->Priority = 1;

            $mail->AddAddress("myemail@gmail.com","Name");
            $mail->SetFrom($visitor_email, $name);
            $mail->AddReplyTo($visitor_email,$name);

            $mail->Subject  = "Message from  Contact form";
            $mail->Body     = $user_message;
            $mail->WordWrap = 50;  

            if(!$mail->Send()) {
            echo 'Message was not sent.';
            echo 'Mailer error: ' . $mail->ErrorInfo;
            } else {
            echo 'Message has been sent.';
            }

也许是相同的问题 > https://dev59.com/5W025IYBdhLWcg3wLipX ? - Glavić
使用PHP Mailer通过Gmail SMTP服务器发送电子邮件 - Glavić
有些网络主机不允许您连接到外部SMTP服务器。您能够从“localhost”连接吗? - Michael Martin-Smucker
@mactic:你是怎么解决这个错误的?我也遇到了同样的错误。 - Aanshi
15个回答

88

移除或注释掉这一行-

$mail->IsSMTP();

而且它会对你起作用。

我检查过并尝试了许多不同网站的答案,但除了上述解决方案,我没有得到任何解决方法。


不知道为什么,但在某些服务器设置上确实有效。谢谢。 - Andy Holmes
这对我有用 +10。请把它作为答案接受。 - Alive to die - Anant
11
如果我删除那一行,就会出现另一个错误:邮件发送失败:无法实例化邮件函数。 - csandreas1
@csandreas1,如果我删除它,我也会遇到同样的错误。你有答案吗? - Naren Verma
请确保您的脚本正确,如果正确,请联系您的主机。您是否在使用Cloudflare? - csandreas1
显示剩余12条评论

10

如果你使用wampserver,那么你必须安装php_openssl.dll扩展。这很容易,只需搜索并应用PHP的扩展即可。

在示例中更改此内容:

    //Set the hostname of the mail server
    $mail->Host = 'smtp.gmail.com';

    //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission 465 ssl
    $mail->Port = 465;

    //Set the encryption system to use - ssl (deprecated) or tls
    $mail->SMTPSecure = 'ssl';

然后您收到了一封来自Gmail的电子邮件,提到要启用“允许不安全的应用程序访问”选项,请点击此处https://www.google.com/settings/security/lesssecureapps

我建议您经常更改密码并加密它。


谢谢,这正是我所需要的。 - Awais Usmani
非常感谢!你的回答是最有帮助、最不令人困惑和直截了当的!我已经为此烦恼了几个小时了。对我有用的是在这里选择“打开”选项:https://www.google.com/settings/u/1/security/lesssecureapps。我正在使用Mac和Xampp。再次感谢! - Tim Anishere

4
您没有SMTPSecure设置来定义所使用的身份验证类型,并且在Host设置中运行不必要的“ssl://”(PS--ssl位于端口465,如果您需要在ssl上运行它,请参见此处的接受答案。以下是要添加/更改的行:
+ $mail->SMTPSecure = 'tls';

- $mail->Host = "ssl://smtp.gmail.com";
+ $mail->Host = "smtp.gmail.com";

仍然无法工作... 生成的错误是:“错误:无法连接到服务器:连接超时(110)SMTP Connect() 失败。消息未发送。邮件程序错误:SMTP Connect() 失败。” - andy
尝试编辑您的原始帖子,并更改至此为止所尝试的代码,以便我们可以对其当前的外观有一个完全清晰的想法。仍然似乎存在可以修复的问题... - Dmitri DB
我曾经遇到过类似的问题。您需要指定SMTPSecure设置。如果您正在使用tls,则需要使用587端口。如果您正在使用ssl,则组合是465。而且,是的,请删除smtp.gmail.com前面的ssl://。 - chap
1
Host设置为tls://smtp.gmail.com与将Host设置为smtp.gmail.com并将SMTPSecure设置为tls实现的效果完全相同;这只是相同事物的另一种语法。您也可以附加端口,例如:587来设置Port属性。 - Synchro

4
你是否在本地主机上运行?并且你编辑了php.ini文件吗?
如果还没有,请尝试以下操作:
1. 打开xampp->php->php.ini
2. 搜索extension=php_openssl.dll
3. 初始设置为;extension=php_openssl.dll
4. 删除“;”,这样它就变成了extension=php_openssl.dll
5. 如果找不到extension=php_openssl.dll,请添加此行extension=php_openssl.dll
6. 然后重新启动Xampp。
祝你好运 ;)

1
兄弟,我在我的 php.init 文件中只找到了 extension=openssl 而没有找到 extension=php_openssl.dll - Goyo

3

我知道这个问题已经有一段时间了,但我曾经遇到过同样的问题,并通过在 csf.conf 中禁用 SMTP_BLOCK(我们使用 CSF 防火墙)来解决它。

只需编辑 csf.conf 并像这样禁用 SMTP_BLOCK:

###############################################################################
# SECTION:SMTP Settings
###############################################################################
# Block outgoing SMTP except for root, exim and mailman (forces scripts/users
# to use the exim/sendmail binary instead of sockets access). This replaces the
# protection as WHM > Tweak Settings > SMTP Tweaks
#
# This option uses the iptables ipt_owner/xt_owner module and must be loaded
# for it to work. It may not be available on some VPS platforms
#
# Note: Run /etc/csf/csftest.pl to check whether this option will function on
# this server
# SMTP_BLOCK = "1" --> this will cause phpmailer Connection timed out (110)
SMTP_BLOCK = "0"

谢谢。结果证明,这是我 cPanel 安装过程中真正的罪魁祸首,而不是在 WHM 的 "SMTP 限制" 配置页面上的开关。 - Garrett W.

3

myaccount.google.com/security上登录你的Google账号,然后进入"登录"并点击"安全性",向下滚动至页面底部,启用"允许不安全应用程序"选项。


3

直到我接到谷歌发送的一封电子邮件,告诉我有人试图登录我的帐户,询问是否为自己,我回答是,然后它开始正常工作。所以如果您也遇到此问题,请查看您的电子邮件并允许服务器。


抱歉我的英语不好,我想说的是我一直在尝试使用phpmailer发送电子邮件,但是我一直收到相同的错误信息“SMTP -> ERROR: Failed to connect to server: Connection timed out (110)...”,1分钟后谷歌向我发送了一封电子邮件(与我在phpmailer中使用的相同电子邮件),他们说有人正在尝试从未知设备访问我的电子邮件,并询问是否是我,我回答是,问题得到解决。另外一个可能有用的信息是在您的gmail中使用像Pa$wword这样的强密码。 - user5778000
你可以编辑你的原始帖子。你的英语很好,但标点符号有些问题 :-) - Supersharp

2

下面是在处理PHPMailer时需要注意的事项列表:

  1. 通过取消注释PHP.ini中的extension=php_openssl.dll,启用openSSL。
  2. 使用$mail->SMTPSecure = 'tls';$mail->Port = 587;
  3. 如果出现错误(例如用户名和密码不正确),请启用调试功能。

1
你做得很好。只需要检查不同的SMTP端口,比如465和其他在你的系统上可以工作的端口。 另一件事情要记住是允许谷歌帐户访问不太安全的应用程序,否则会出现相同的错误。
我花了整整一天时间研究它,唯一做错的事情就是端口号,我只改变了端口号,然后它就可以工作了。

1

Mailjet

SMTP设置

端口:25或587(一些提供商会阻止25号端口)

我通过在将应用程序部署到服务器后更改端口来工作。

  • 在调试中,它对我有用:$mail->Port = 25;
  • 在发布中,它对我有用:$mail->Port = 587;

祝好运


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