Am I writing a function to send mail to a specific user?
What I'm trying to do is if an error occurs, I want to keep sending 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 an error message, but instead of giving an error, I want to try to send mail until the mail is finally sent.
, ? , , , , .. , ? . . Cron , ( , ). .
/, . , , .
, . , /, , , ( ). , 4 :
" , , - " -Woody Allen
, , mail() ?
mail()
, , ? ? , , , . , , .SMTP-, , , , DDoS'd, . , , , .
, mail() false, , - . , , .
, , , .. mail() , . , / , .
, 30 , , ( 30 ), while?
- , , , , , 782428424 30 , , , .
, - , 5 .
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.")"; $retries = 0; while (!@mail($feedback,$subject,$content,$headers)) { $retries++; if ($retries >= MAX_RETRIES) { break; } sleep(WAITING_PERIOD); } if ($retries < MAX_RETRIES) { echo "Your mail has been sent."; } else { echo "We are sorry for the inconvienience, but we could not send your mail now." ; } }
WAITING_PERIOD MAX_RETRIES -, , MAX_RETRIES , , WAITING_PERIOD .
/ .
, script .
"" .
- :
$remainingTries = 5; while ($remainingTries > 0 && !sendmail()) { $remainingTries--; // we used a try, decrement sleep(5); // wait five seconds before next try }
Try something like this:
function doSomething($arg1, $arg2, $retry = 10) { $result = false; /* do stuff */ if ($result === false) { static $original = null; if (is_null($original)) { set_time_limit(0); $original = $retry; } sleep(pow(2, $original - $retry)); while (--$retry > 0) { return doSomething($arg1, $arg2, $retry); } } return $result; }
If this fails, try $retryagain and wait:
$retry
1 second before the 1st retry 2 seconds before the 2nd retry 4 seconds before the 3rd retry 8 seconds before the 4th retry 16 seconds before the 5th retry 32 seconds before the 6th retry 64 seconds before the 7th retry 128 seconds before the 8th retry 256 seconds before the 9th retry 512 seconds before the 10th retry ... 2 ^ ($retry - 1) seconds before the ($retry)th retry
This way you guarantee that your server will not be overloaded.
Source: https://habr.com/ru/post/1745929/More articles:How to create a UISplitView manually? - objective-cTime zones of different countries - javascriptСтатическое свойство, теряющее свою ценность с перерывами? - propertiesReading XML using XElement in Silverlight - silverlightWhy does changing the StrokeType of a GestureOverlayView object affect the standard behavior of the view? - androidASP.NET Reading External XML from a Website - xmlProblem with Ruby script output to file - linuxC # Figure Oracle Spatial Geometries - c #Как создать ключ с помощью RSA/ECB/PKCS1Padding в python? - pythonChoosing SQL Server - sqlAll Articles