views:

253

answers:

2

I am running a loop script in PHP command line, which send out emails to customers using PHPMailer. The problem I am receiving is that the command line script exits when the PHPMailer returns a false.

Here is the script pseudocode:

while(the loop is valid){
    if(mail ID exists){
        set_time_limit(30);
        ..compose mail..
        if($mail->Send()){
            ..Mark as success in database..
            usleep(10000);
        } else {
            ..Mark as failure in database..
            usleep(10000);
        }
    }
    ..continue loop..
}

If the $mail->Send() returns a false, the script stops and exits. Is this an expected behaviour of PHP in command line? If that's the case, is there any way to tell PHP not to stop when it receives a false?

Thank you for any help.

+1  A: 

I would guess that the $mail->Send() function (or something else completely) is throwing an error which is halting the execution of your script.

I take it the values are not getting updated in the database either? This will be the case if so. Run the script with error reporting on to determine this.

error_reporting(E_ALL);
ini_set('display_errors', true);
Stephen Melrose
Yes! The values are not getting updated in the database. I am running with the error reporting turned on, but no hint is thrown back.
Nirmal
The PHPMailer's manual says: `Send()` creates message and assigns Mailer. If the message is not sent successfully then it returns false. Use the ErrorInfo variable to view description of the error. Returns true on success, false on failure.
Nirmal
PHPMailer may be trying to silence the failures using the `@` suppressor, but if it's a fatal error, it will exit regardless.
Stephen Melrose
I use PHP 5.3 and read this in a forum: `PHPMailer makes use of features that are deprecated in PHP version 5.3. This results in E_DEPRECATED messages (a new type of warning) if those are enabled in the php.ini configuration file.` Could this be triggering the exit? Does by any chance the deprecated message stop a script? Thank you for your help.
Nirmal
A warning is not a fatal error.
VolkerK
As @VolkerK said, the E_DEPRECATED messages are warnings and don't halt the execution of the script. There will be an error elsewhere, maybe not even in PHPMailer, that is stopping the execution.
Stephen Melrose
+1  A: 

Most likely the error handling routine (in the else branch) calls exit or causes a fatal error that lets php bail out.
Add two lines of debug output to check whether the script enters and leaves the else branch (successfully).

while(the loop is valid){
     [...]
        } else {
          error_log('Debug: Send() failed. Start error handling');
          ..Mark as failure in database..
          usleep(10000);
          error_log('Debug: Send() failed. End error handling');
        }
     [...]
VolkerK
Nope. The script does not even enter the failure routine. It simply exits. I tried running the same script from the browser and it worked! On false, it processed the failure routine and moved on with the loop. The problem I have with invoking the page within a browser is that IIS stops any fast-CGI after a specified timeout, no matter what we specify in php.ini. So, no way the whole list of emails get sent.
Nirmal
You could use a debugger like xdebug and netbeans as frontend to step through the code.
VolkerK