1

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.

6
  • 2
    Are you sure the send() is the reason for exiting? It could also be a script timeout or a mistake in the condition of your while loop. Commented Feb 8, 2010 at 9:48
  • My tests show that the script exits only when it encounters the failure portion of the loop. Commented Feb 8, 2010 at 9:52
  • Did you run the script with error_reporting=E_ALL and display_errors=true (or keeping an eye on the error.log)? Commented Feb 8, 2010 at 9:53
  • Yes, I am keeping track of the error.log, but nothing significant is present there. So my question is whether the command-line script will exit if it encounters a false. This is kind of weird because we all use boolean results almost everywhere, right? Commented Feb 8, 2010 at 9:58
  • 1
    No, php-cli will not exit just because some arbitrary function/method returns false. Commented Feb 8, 2010 at 10:11

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

5 Comments

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.
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.
PHPMailer may be trying to silence the failures using the @ suppressor, but if it's a fatal error, it will exit regardless.
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.
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.
1

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');
        }
     [...]

2 Comments

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.
You could use a debugger like xdebug and netbeans as frontend to step through the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.