# mail() timeout issue

8.9k Views

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?

 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
• The command line and browser are actually using two different php.ini files. You may want to search for php.ini and compare your settings
• That makes no sense to me; So I checked: php --ini returns the same path for the configuration file as the browser.
• I assume sendmail_path is the same as well?
• 2
• djdy, yes the sendmail_path is the same. Ivan Buttinoni, it doesn't work, has the same behavior.

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
• 1
• Hi Ivan, I tested .2 and was surprised to see it take as much as executing mail() directly from the browser. Is it possible then that different users are being used? I mean, it was all installed the same way, at the same time... Still makes no sense... Thanks anyway.
• Hi, I tried executing the file via CLI as you stated and it worked as expected, instant delivery; So the problem should not be the script file.
• 2
• @andrematos to verify .1 you can reinstall mod_php and to verify .2 you can do from apache/html_page system("php -f z:path ophpmail.php")

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

• 2
• Trott, thanks for your effort, however as I explained to Sekar (see comments), I don't think I'm going through MTA. I might have the wrong idea tho..
• @andrematos MTA is a generic term for something that handles mail transfer. Regardless, I'd still check for the potential issue involving users and firewall rules remains.

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.

• 2
• I'm running this from my machine, so yes, it is the same.

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


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

• 2
• I tried both PHPMailer and Swift and the behavior is exactly the same.
• Sekar, I'm connecting to the destination SMTP. My settings are something like: SMTP: mail.example.com; FROM: amatos@example.com; TO: amatos@example.com; So I think MTA has no role here.

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.