How to execute a function until it succeeds?

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.

+3
source share
6 answers

Design for error handling:

, ? , , , , .. , ? . . Cron , ( , ). .

/, . , , .

, . , /, , , ( ). , 4 :

" , , - " -Woody Allen

+18

, , mail() ?

  • , .
  • SMTP-, , , - .

, , ? ? , , , . , , .
SMTP-, , , , DDoS'd, . , , , .

, mail() false, , - . , , .

, , , .. mail() , . , / , .

+4

, 30 , , ( 30 ), while?

- , , , , , 782428424 30 , , , .

, - , 5 .

+2
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 .

0

/ .

, script .

"" .

- :

$remainingTries = 5;
while ($remainingTries > 0 && !sendmail())
{
  $remainingTries--; // we used a try, decrement
  sleep(5);          // wait five seconds before next try
}
0

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:

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.

0
source

Source: https://habr.com/ru/post/1745929/


All Articles