1

This sounds a bit odd but I have a situation where I have to send email using php mail function. This mail function is in a sub function. Now I want to execute my php script complete without having to worry about success or failure of the mail function. Like I don't care whether the mail function was executed successfully or not because of any error or something but I want my php script to be executed completely. How can I achieve this ?

Thanks in advance.

Anoop
  • 173
  • 1
  • 1
  • 14
  • 1
    And what now - your script stops or what? – u_mulder Jul 30 '15 at 20:05
  • 1
    possible duplicate of [When to use Try Catch blocks](http://stackoverflow.com/questions/5199146/when-to-use-try-catch-blocks) – ficuscr Jul 30 '15 at 20:09
  • 1
    @ficuscr: mail doesn't throw exceptions. it just returns boolean false. – Marc B Jul 30 '15 at 20:09
  • I read it as "mail code sub routine" - but point taken. Is the question about threading then? – ficuscr Jul 30 '15 at 20:10
  • Actually the thing is that mail function is in another function. But before using the mail function I call a service to get some data using curl. This service may fail or succeed. So depending on that mail function will execute or not. Anyways is try catch block sufficient to make sure the script keeps executing in any possible scenario ? – Anoop Jul 30 '15 at 20:28

2 Answers2

0

A long as your use of the mail function is correct, your php script should continue to run after executing the mail function whether it succeeds or not in sending the mail.

What is the error you are getting? You could always wrap it in...

try {

} catch(Exception $e) {

}
Trung Nguyen
  • 519
  • 3
  • 18
  • I'd also recommend outputting the details of the mail you're trying to send to an error log within the `catch()`. That way you have a record of any failures for examination. – Jerbot Jul 30 '15 at 20:17
  • Actually the thing is that mail function is in another function. But before using the mail function I call a service to get some data using curl. This service may fail or succeed. So depending on that mail function will execute or not. Anyways is try catch block sufficient to make sure the script keeps executing in any possible scenario ? – Anoop Jul 30 '15 at 20:35
  • The `Exception` for the `catch` is a top level that will call all errors thrown from code within the `try` block. You can have multiple catches to handle various different errors that might come from the try block. Also, set error reporting to ALL so you can debug your errors and see what is actually breaking your script. – Trung Nguyen Jul 30 '15 at 21:02
  • Actually I can. But this breaking happens only in few cases like 4,5 out of 100. And these are real time hotel bookings. But yeah I think I will try to test this on my local and see if I get any error situation. Thanks. – Anoop Jul 31 '15 at 17:54
  • As mentioned already, it's not good to leave errors in your application. You should log the output of `$e->getMessage()` to a file that you can check from time to time and find out why whatever is failing and fix it. I'd say send yourself an email notification with the error details - but you might not get that! LOL – Trung Nguyen Jul 31 '15 at 17:59
0

I know this was years ago, but I was looking for something similar, and I think this is more what you're after - create a separate file with that function, and call it separately - essentially as a separate action:

exec("php mailuser.php > /dev/null 2>&1 &");

From Prevent PHP From Waiting for mail() function - see the accepted answer there for more details.

(If the mail function delay is not the concern, then a normal try/catch should likely be sufficient.)

Ben in CA
  • 688
  • 8
  • 22