2

今天我发现我的网站联系表大约需要 20 秒才能在我的 Godaddy Centos VPS 服务器中发送一封电子邮件。

我调查了这个问题,发现源是PHPmail()函数引起的。当我在网站的联系页面上输入消息并单击发送按钮时,页面加载完成大约需要 20 秒。

使用以下 PHP 代码创建一个 test.php 文件来测试邮件:

<?php

mail("myemail@mydomain.com", "Test", "Test");

?>

我运行命令:php test.php在服务器上。

我检查了/var/log/maillog邮件,看起来电子邮件已立即发送,没有任何延迟。

只有当我通过我的网站执行我的代码时,才会出现 20 秒的延迟。我认为这与 Apache 用户有关,因为带有 root 用户的命令行可以毫不拖延地发送电子邮件。

这是我使用 root 用户通过控制台发送电子邮件时的邮件日志:

Apr 10 14:57:04 ip-103-1-173-250 sendmail[27681]: r3ALv4i3027681: from=root, size=174, class=0, nrcpts=1, msgid=<201304102157.r3ALv4i3027681@ip-103-1-173-250.ip.secureserver.net>, relay=root@localhost
Apr 10 14:57:05 ip-103-1-173-250 sendmail[27681]: r3ALv4i3027681: to=john23157@gmail.com, ctladdr=root (0/0), delay=00:00:01, xdelay=00:00:01, mailer=relay, pri=30174, relay=myserver, dsn=2.0.0, stat=Sent (Accepted message qp 20459 bytes 684)

出于安全原因,我将这篇文章中的relay=值替换为。myserver

从上面的邮件日志中,很明显邮件是立即发送的,我确实收到了邮件。

这是我通过网站联系表发送时的邮件日志:

Apr 10 14:54:22 myhostname sendmail[27655]: r3ALsMjV027655: from=support@mydomain.com, size=940, class=0, nrcpts=1, msgid=<d946feca6d6640910e2db02d541aa704@mydomain.com>, relay=apache@localhost

Apr 10 14:57:56 myhostname sendmail[27655]: r3ALsMjV027655: to=john23157@gmail.com, ctladdr=support@mydomain.com (48/48), delay=00:03:34, xdelay=00:03:34, mailer=relay, pri=30940, relay=myserver, dsn=2.0.0, stat=Sent (Accepted message qp 21186 bytes 1297)

上面的邮件日志显示延迟了 3 分半钟。

我不知道是什么导致了通过 Apache 发送时的延迟。

您的帮助将不胜感激。

谢谢

4

3 回答 3

1

As @hek2mgl says, unfortunately most such things are caused by network issues–one of the mail servers that is in the chain may be overwhelmed or some part of the network might just be slow at that point. When I was developing my own PHP application, I experienced similar issues.

Since you have your own VPS, I would recommend installing your own SMTP server. I used PostFix, but there are others. You then configure it so that it replaces the built in unix sendmail command. You can find instructions on how to install postfix at the CentOS Wiki

If this is set up correctly, calling mail() from PHP will add the message to the send queue in Postfix, rather than trying to contact an external mail server. It will return immediately (meaning your app will feel much faster), while Postfix will keep trying to send the email in the background for as long as it needs to.

于 2013-04-10T22:23:25.467 回答
0

I found this solution on experts exchange and it fixed this exact issue for me.

--- Pasted Below ---

My problem was that Sendmail looks for a fully qualified domain (FQDN) name, which I did not have in my hosts file. Once I changed it from this:

127.0.0.1 localhost

127.0.1.1 webserver

to this:

127.0.0.1 localhost localhost.localdomain webserver

于 2014-10-15T20:34:16.740 回答
-1

I'll bet you a weeks salary it's not caused by PHP's mail() function.

The mail() function is a simple wrapper around an exec call which invokes the CLI interface to the MTA. Simply exec'ing a program is unlikely to have such a delay - every time I've come across this problem it's a result of the MTA trying to offload the message synchronously and running into problems along the way (badly configured DNS, missing smart hosts, ident lookups....).

Since it's a VPS you have control over how the MTA is (mis)configured. And that's a topic for ServerFault - not Stackoverflow.

(BTW did you check the configs were the same for the CLI and webserver SAPI? Did you try to exec the CLI from the webserver sapi?)

于 2013-04-10T22:56:10.623 回答