Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mail() timeout issue

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('[email protected]', 'test subject', 'test body', 'From: Andre Matos <[email protected]>');
?>

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

php -r "mail('[email protected]', 'test subject', 'test body', 'From: Andre Matos <[email protected]>');"

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) :-)

like image 455
acm Avatar asked Nov 21 '11 18:11

acm


People also ask

How do I fix SMTP timeout error?

The resolution for this error is quite simple; the user must check the network connectivity to ensure that the SMTP client has unrestricted access to the SMTP server over port 25. If the ISP or administrators intentionally deny port 25, the best alternative is to use port 587, which supports SMTP with encryption.

What is an email timeout?

The timeout error indicates that the server is taking too much time for relaying a request made from the device. This error occurs when you have already defined the length of time and the server is unable to fulfill the request. Once the defined time is over, Outlook displays you a timeout error.


2 Answers

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
like image 96
Ivan Buttinoni Avatar answered Nov 04 '22 16:11

Ivan Buttinoni


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.)

like image 34
Trott Avatar answered Nov 04 '22 17:11

Trott