SMTP 配置在生产中不起作用

SMTP configuration not working in production

我正在尝试在提交表单时发送电子邮件。我正在使用 PHPMailer 通过以下配置发送邮件。

$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'mail.example.in'; 
$mail->Port = 25; 
$mail->SMTPAuth = true;
$mail->Username = 'user@example.in'; 
$mail->Password = 'password'; 

$mail->setFrom("user@example.in" , "User");
$mail->addAddress('receiver@example.in', 'Receiver');
$mail->addBCC('anotheruser@somedomain.com', 'Another user');
$mail->AddReplyTo('user@example.in', 'User');

$mail->isHTML(true);

$mail->Subject = $subject;
$mail->Body    = $message;
if($mail->send())
    echo "Your request has been received. We will soon contact you.";
else echo "Unable to send your request. Please try again";

这在本地主机上工作正常。但是,当我将它部署到我的服务器 (example.in) 时,出现以下异常。

SMTP ERROR: Failed to connect to server: Connection timed out (110)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

--编辑--

我尝试使用 telnet 命令连接到 SMTP 服务器,但我无法添加收件人。我收到以下错误?

Last login: Fri Sep 16 11:08:06 on ttys000
admin:~ admin$ telnet mail.example.in 25
Trying 111.91.153.112...
Connected to mail.example.in.
Escape character is '^]'.
220 fbs-ho-mailserver.example.in ESMTP Service (Lotus Domino Release 8.5.3FP3) ready at Fri, 16 Sep 2016 11:36:01 +0530
HELO example.in
250 fbs-ho-mailserver.example.in Hello example.in ([111.91.127.222]), pleased to meet you
MAIL from: marketing@example.in
250 marketing@example.in... Sender OK
RCPT to: john.hh@gmail.com
554 Relay rejected for policy reasons.

-- 编辑--

我能够在 Outlook 中设置此帐户。我真的很困惑发生了什么。

超时错误是丢包的典型现象,因此可能是 Web 服务器和 SMTP 服务器之间的防火墙问题。 如果您可以通过 ssh 访问您的服务器,您可以尝试使用以下方式连接到 smtp:

telnet <smtp server> 25

即使使用 telnet 也出现超时错误,您很确定问题出在 network/firewall 方面。

您需要更改 php.ini 中的某些设置:

upload_max_filesize = 2M 
;or whatever size you want

max_execution_time = 60
; also, higher if you must - sets the maximum time in seconds.

你的 PHP.ini 取决于你的环境,更多信息: reference

将它放在您的 PHP 脚本的顶部,然后放开您的脚本

ini_set('max_execution_time', 600); //600 seconds = 10 minutes or set your time

第一个引用(来自 ibm 社区):

john.hh@gmail.com was a member of a group (Reject) that was listed in gmail.com's "Deny messages intended for the following internet addresses" field (in the destination server's Domino Configuration document's Router/SMTP, Restrictions and Controls, SMTP Inbound Controls tab's "Inbound Intended Recipient's Controls" section).

Removing Mary's hierarchical name (Mary Jones/ABC) from the members list in the Reject (group) document allows Mary to receive messages from the Internet.

第二个引用:

Most mail servers, to prevent them being used as anonymous spam relays, are configured only to relay mail from certain hosts.

使用您自己的 SMTP 不是个好主意。 根据您要处理的内容,您很有可能会以某种方式阻止您的电子邮件或将其标记为垃圾邮件。而且您将不得不花费一些时间来使您的服务器保持最新状态。

使用为每个提供商列入白名单并公开 API 的在线服务来发送您的交易邮件: https://www.mailjet.com/ http://mailchimp.com/ ...

他们经常建议为小容量(每天 2000 封电子邮件以下)提供免费帐户。 使用 API 非常简单,可以在几分钟内完成(例如:https://dev.mailjet.com/

您遇到的问题 - 而不是 两个不相关的问题 - 非常简单:

SMTP ERROR: Failed to connect to server: Connection timed out (110) SMTP connect() failed.

并且您已验证服务器确实在接受连接:

I tried connecting to the SMTP server using telnet command

Last login: Fri Sep 16 11:08:06 on ttys000
admin:~ admin$ telnet mail.example.in 25
Trying 111.91.153.112...
Connected to mail.example.in.

当 运行 从其生产服务器 .

时,您的脚本无法连接到 SMTP 服务器

可能的原因是生产服务器有一个防火墙,为了避免滥用,它阻止了与外部的任何连接。服务器可以服务 Web 请求,但不能更多。

如果您的测试已验证 端口 25 没有响应,那么(在检查主机地址是否正确后)您可以尝试 telnet mail.example.in 587 代替。如果可行,则可能意味着服务器不接受不安全的连接(端口 25),而是接受安全连接。使用 PHPMailer,您可以尝试 activating secure connection:

$mail->SMTPSecure = 'tls';

$mail->SMTPSecure = 'ssl';

如果这不起作用,您可能仍有防火墙问题;或者您可能需要查看 phpinfo() 并验证您是否在 PHP.

中提供了 OpenSSL 支持

你需要做什么

  • 请维护生产服务器的IT人员打开防火墙;
  • 更有希望的是,询问他们如何从该服务器发送电子邮件。您可能需要使用 mail() 功能,或使用 localhost127.0.0.1 作为 SMTP 服务器。然后电子邮件将通过您的生产服务器的服务网络发送出去。

他们可能会告诉您不允许使用端口 25,但可以使用(比如说)465 或 567 端口。您将必须更新您的配置 and/or 相应地添加 TLS/SSL(见上文)。

  • 或者您可能被允许连接到您必须提供其 IP 地址的第三方 SMTP 服务器,以允许 IT 人员打开合适的防火墙 window。然后邮件会通过第三方服务器发出。

第二个问题(可能不是问题)

250 marketing@example.in... Sender OK RCPT to: john.hh@gmail.com 554 Relay rejected for policy reasons

同样为了避免滥用,SMTP 服务器 不会让每个人连接和发送电子邮件,但只允许他们自己的客户。我看到您在 PHPMailer 配置中指定了用户和密码。在 telnet 会话中你没有。所以很可能 PHPmailer 可以发送,但不能连接,而你的 telnet 可以连接,但不能发送。

一旦您解决了连接问题,您的身份验证问题就会得到解决或消失(因为您将使用 IT 人员提供给您的不同服务器,例如 localhost) .

第三个问题(可能永远不会出现)

滥用服务的第三种方式是过度使用——向太多人发送太多电子邮件。与 IT 人员确认发送电子邮件的可接受策略是什么。

问题,问题

其他需要检查的是来源的可信度(您可能想代表某个域发送电子邮件,该域没有将您选择的 SMTP 服务器指定为允许的发件人),以及数据的 机密性 (即使有 TLS/SSL 个连接,如果您被指定为 localhost 作为 SMTP 服务器,您的 IT 人员将拥有完整、不受约束的,无法检测到对您发送的任何电子邮件的访问。您可能会,也可能不会,对此表示满意)。

询问您的托管服务提供商是否启用了 smtp,而不是 server.I 之前对于 smtp 和 curl both.Contact 托管服务提供商有同样的问题。

phpmailer 版本是多少?

您可能忘记添加此行

  $mail->IsSMTP(); //  use SMTP

在您启动 $mail 之后。

试一试。

我明白你的意思了。同样的问题发生在我身上,我只是添加了一行并且它对我有用。请使用 ,

$mail->SMTPSecure = 'ssl';

并根据 ssl 配置您的设置。还要正确检查服务器端的邮件配置。

所以你有一个 GoDaddy 服务器,你想通过你新获得的 SMTP 中继发送电子邮件。好吧 “你做不到” 可能是你四处寻找 Google.

的结果

如果你多看看,你可能会发现 GoDaddy 的官方解决方案是使用他们的内部邮件中继。这并不理想,因为他们设置了任意障碍,例如这个:

Our dedicated servers have an outbound email limit of 1000 per day. If you need to send more than 1000 emails per day, please call Customer Support or open a support ticket to request a higher limit.

快速有效的解决方案:获取支持使用 HTTP 发送电子邮件的 SMTP 中继 API。

我的建议:(一个简单有效的解决方案,我个人使用)

Sendgrid 中注册一个帐户。 Sendgrid 每月提供 12k 封免费电子邮件,我认为这应该足以满足任何基本需求。

第一步: Sendgrid 会要求您添加一些 DNS 记录以验证您是否代表该主机发送电子邮件。就这样吧。

第二步步骤:配置白标https://sendgrid.com/docs/User_Guide/Settings/Whitelabel/index.html(这并不重要,但它会让您发送的电子邮件看起来更专业)

第三步:生成API个密钥

你可以manage your API Keys from the SendGrid Customer Portal. Additionally, you can manage your API keys via the API itself.

第四步:继续写我们的代码。

使用 github 上可用的 library 在 sendgrid 上使用 PHP 发送邮件很容易:

// using SendGrid's PHP Library
// https://github.com/sendgrid/sendgrid-php
require 'vendor/autoload.php';
$sendgrid = new SendGrid("SENDGRID_APIKEY");
$email    = new SendGrid\Email();

$email->addTo("test@sendgrid.com")
      ->setFrom("you@youremail.com")
      ->setSubject("Sending with SendGrid is Fun")
      ->setHtml("and easy to do anywhere, even with PHP");

$sendgrid->send($email);

这是如何工作的

GoDaddy 阻止端口 25、2525、587 和 485,这些端口是 SMTP 的理想端口。所以我们使用端口 80/443 并请求 sengrid 的 API 代表我们使用端口 80 发送电子邮件,是的,我们愚弄了 GoDaddy。

您的错误显示:

SMTP ERROR: Failed to connect to server: Connection timed out (110)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

所以让我们看看https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting :

"SMTP Error: Could not connect to SMTP host."

This may also appear as SMTP connect() failed or Called Mail() without being connected in debug output. This is often reported as a PHPMailer problem, but it's almost always down to local DNS failure, firewall blocking (for example as GoDaddy does) or other issue on your local network. It means that PHPMailer is unable to contact the SMTP server you have specified in the Host property, but doesn't say exactly why. It can also be caused by not having the openssl extension loaded (See encryption notes below).

Some techniques to diagnose the source of this error are discussed below.

GoDaddy

Popular US hosting provider GoDaddy imposes very strict (to the point of becoming almost useless) constraints on sending email. They block outbound SMTP to ports 25, 465 and 587 to all servers except their own. This problem is the subject of many frustrating questions on Stack Overflow. If you find your script works on your local machine, but not when you upload it to GoDaddy, this will be what's happening to you. The solution is extremely poorly documented by GoDaddy: you must send through their servers, and also disable all security features, username and password (great, huh?!), giving you this config for PHPMailer:

$mail->isSMTP();
$mail->Host = 'relay-hosting.secureserver.net';
$mail->Port = 25;
$mail->SMTPAuth = false;
$mail->SMTPSecure = false;

GoDaddy also refuses to send with a From address belonging to any aol, gmail, yahoo, hotmail, live, aim, or msn domain (see their docs). This is because all those domains deploy SPF and DKIM anti-forgery measures, and faking your from address is forgery.

You may find it easier to switch to a more enlightened hosting provider.

我想我们可以在几步之内让它正常工作。

  1. 在您的 Godaddy/Hostgator Cpanel > Select MX 记录 > Select 有问题的域 > Select "Remote Mail Exchanger"

    此时您可以测试邮件发送,问题可能会得到解决。

  2. 我确信它设置正确,但相信我,当涉及到好的 ole Cpanel、PhpMailer 和 Godaddy/Hostgator 时。这是一个常识毫无意义的地方。

替换为:

$mail->Host = 'mail.example.in';

与:

$mail->Host = 'localhost';

我相信这应该可以解决任何问题,前提是登录凭据等内容有效。

我遇到了类似的超时问题,原因是我的托管服务提供商 (Linode)。 TLDR;对于 2019 年 11 月 5 日之后创建的帐户,默认情况下对 Linode 实施 SMTP 限制。您需要为您的实例配置 rDNS 并打开支持票证以确认 CAN-SPAM 合规性,支持人员应该会很快解除限制。我猜很多人因为 smtp 限制而遇到这个问题