mail() timeout issue

依然范特西╮ 提交于 2019-12-22 04:01:46

问题


When I execute my email script via browser a timeout fatal error is returned (unless I drastically increase the execution time, then it will run ok, not the solution I'm looking for). The email is sent tho, but it takes forever (5 min. average) to arrive (at my inbox)!
(Considering that via command line it works perfectly I think that SMTP at php.ini is certainly well configured.)

So this is the code executed by browser request:

<?php
mail('amatos@example.com', 'test subject', 'test body', 'From: Andre Matos <amatos@example.com>');
?>

and when I run this same (is it really the same? I'm starting to doubt myself) code via command line:

php -r "mail('amatos@example.com', 'test subject', 'test body', 'From: Andre Matos <amatos@example.com>');"

it works perfectly! The script runs, it stop and the email arrives instantly (2/3 seconds).

So, what can cause this difference and how to fix it? Any ideas?
Thanks in advance.


[edit] some extra info:
- the machine is windows
- the server is localhost
- php.ini is the same for both the browser and the cli instance


[edit2]
Thank you all for trying to guess which was the problem. I placed the question hopping that someone had the problem before and knew of something specific. Given nothing specific showed up and none of the suggestions really worked, I've decided to accept the one that allowed me to reach more conclusions about the problem... +1 For all your helpful knowledge/thoughts (/guesses) :-)


回答1:


I've hypothesised some couses, but I used to linux and on windows I can olny guess:

  1. php_cli and mod_php are 2 different binaries, mod_php can be slightly damaged
  2. php_cli and mod_php use 2 different users, the network profile of apache user can be the problem (dns, firewall, proxy...)
  3. your php script is on "problematic" location or contains some problematic character, but your cli script is by param, try to execute same script: php -f z:\path\to\php\mail.php



回答2:


Given this note from http://php.net/manual/en/function.mail.php, it seems very likely that the issue is with the MTA and not PHP directly:

The Windows implementation of mail() differs in many ways from the Unix implementation. First, it doesn't use a local binary for composing messages but only operates on direct sockets which means a MTA is needed listening on a network socket (which can either on the localhost or a remote machine).

Perhaps it has something to do with the way the MTA is responding to the particular user, or user-specific firewall rules for outgoing mail connections on your machine. Can you run the command-line as the web server user rather than yourself? If so, does that re-create the problem from the command-line?

How about having the web server execute the command-line PHP rather than the parsed PHP file? (For example, perhaps you can run a batch script via CGI.) Does that solve the problem?

(Sorry that these are more guesses than definite answers.)




回答3:


Just to be clear: The php-instance used by the script is the same as the one used by the command line code?

Many web hosts use smtp-relay, which will gather a bunch of emails and send them all at once, so it won't be strange behaviour if your mail is late. However, the long execution time is not normal.




回答4:


Before you send your mail in the script make sure to print out the ini_get() of the variables SMTP, smtp_port, and sendmail_from and be certain that these are working values. PHP running on Windows does not have the benefit of sending mail out via Sendmail, and whatever the PHP devs cobbled together for it is sketchy at best.

I always like to test mail via telnet so I can see if the server is giving an error that is not being passed back properly by the client:

c:\> telnet smtp.domain.com 25

220 smtp.domain.com ESMTP Postfix
helo mailtest
250 smtp.domain.com
mail from: user@local.com
250 2.1.0 Ok
rcpt to: user@remote.com
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
from: user@local.com
to: user@remote.com
subject: test mail

this is a test message
.
250 2.0.0 Ok: queued as 42AD8364FE0E
quit
221 2.0.0 Bye



回答5:


Try to set username and password to "From" mail id. so it can authenticate and sends the mail quickly.

Have you tried PHP mailer?

In my observation its sends mails within seconds. Below example will give you a quick look how to use php mailer class.

include  "class.phpmailer.php";
$msg="Hello! This is a test..."
$mail=new PHPMailer();
$email="someone@friend.com"; //person who receives your mail
$mail->IsSMTP();
$mail->Host = "localhost";
$mail->SMTPAuth = true; 
$mail->Username = "admin@example.com"; //your mail id
$mail->Password = "sdfsd441"; //password for your mail id
$mail->SetFrom('admin@example.com', 'admin'); //from address
$mail->AddAddress($email);
$mail->Subject ="Test Mail";
$mail->Body = $msg;
$mail->IsHTML(true);
$mail->MsgHTML($msg);
$mail->Send();  

EDIT :
In PHP manual they stated like this,

The Windows implementation of mail() differs in many ways from the Unix implementation. First, it doesn't use a local binary for composing messages but only operates on direct sockets which means a MTA is needed listening on a network socket (which can either on the localhost or a remote machine).

so that may cause delay? I think this link might help you.




回答6:


My first guess would be that the browser version of your mailing has already a context or connections ready for the sending. On the contrary, the direct (php -r) execution has to load the mailing context.

To confirm this idea, you can make a loop for sending 10 mails and check wether the mails after the first are much quicker.



来源:https://stackoverflow.com/questions/8216433/mail-timeout-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!