6Answers
  • 14
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

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

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
  • 5
Reply Report
      • 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.)

  • 3
Reply Report
      • 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
Reply Report

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
  • 2
Reply Report

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.

  • 2
Reply Report

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.

  • 2
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions