I am writing a function to send a mail to specific user?
What I am trying to do is, if error occurs I want to keep on sending the mail until the mail is sent.
function sendmail() {
$name = mysql_escape_string($name);
$email = mysql_escape_string($email);
$phone = mysql_escape_string($phone);
$content = nl2br(mysql_escape_string($content));
$subject = "mail from ".$name." (".$email.", Ph: ".$phone.")";
$mail = @mail($feedback,$subject,$content,$headers);
if($mail) { echo "Your mailis send"; }
else { echo "We are sorry for the inconvienience, but we could not send your mailnow."; }
}
the above function displays error message but instead of giving the error, I want to try to send the mail until the mail is finally sent.
Design for Error Handling:
What if the page cannot send? The Daemon stopped, the user entered malformed data, the connection was lost, could write disk, database is down for mainenance, etc. What do you want the server to do with the user request? I advise setting a queue up on failure to retry later. There are a lot of good queue systems out there. Cron the retry if you have to (that way it is not hanging on a client request). Log tries and if certain error conditions occur then notify the administrator.
This is what an operations/infrastructure team is built for, but you can start with some good tooling and a well designed strategy for error and exception handling. The people who inherit your code will thank you.
I have been reading the Pragmatic Programmer and I have to highly recommend it for dealing specifically with this question. It sets a standard in the way you think about error/exception handling and for me definitely cleared the fog regarding what an error is, vs an exception, vs an assertion condition (or impossible condition). In particular Chapter 4 Pragmatic Paranoia:
“When everyone actually is out to get you, paranoia is just good thinking” -Woody Allen