Created
July 8, 2013 13:27
-
-
Save mudge/5948769 to your computer and use it in GitHub Desktop.
A retry function for PHP.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once dirname(__FILE__) . '/../lib/simpletest/autorun.php'; | |
function retry($f, $delay = 10, $retries = 3) | |
{ | |
try { | |
return $f(); | |
} catch (Exception $e) { | |
if ($retries > 0) { | |
sleep($delay); | |
return retry($f, $delay, $retries - 1); | |
} else { | |
throw $e; | |
} | |
} | |
} | |
class RetryTest extends UnitTestCase | |
{ | |
function testRetryWithNoExceptions() | |
{ | |
$name = "Bob"; | |
$result = retry( | |
function () use ($name) { | |
return $name . "!"; | |
}, | |
0.1 | |
); | |
$this->assertEqual("Bob!", $result); | |
} | |
function testRetryWithException() | |
{ | |
$fail = true; | |
$result = retry( | |
function () use (&$fail) { | |
if ($fail) { | |
$fail = false; | |
throw new Exception("Boom!"); | |
} else { | |
return "Woo!"; | |
} | |
}, | |
0.1 | |
); | |
$this->assertEqual("Woo!", $result); | |
} | |
function testRetryTriesTheExpectedNumberOfTimes() | |
{ | |
$count = 0; | |
try { | |
retry( | |
function () use (&$count) { | |
$count += 1; | |
throw new Exception("Boom!"); | |
}, | |
0.1 | |
); | |
} catch (Exception $e) { | |
} | |
$this->assertEqual(4, $count); | |
} | |
function testRetryWithConstantFailure() | |
{ | |
$this->expectException(new PatternExpectation("/I will never work./")); | |
$result = retry( | |
function () { | |
throw new Exception("I will never work."); | |
}, | |
0.1 | |
); | |
} | |
} |
Hello, I am using a phpmailer.
if a user uploads a material, it sends notices to other users email and i used the phpmailer.
it is working. but when there is an error occurs, it shows an error, i don't want the user to see the error but the code to retrying sending the message.
i need help on how i should integrate your code in it.
or should i post my code here.
`
foreach($result as $row){
$email = $row['email'];
$full_name = $row['full_name'];
$stu_email = $row['stu_email'];
$stu_name = $row['stu_name'];
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
require '../vendor/autoload.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
// $mail->isSMTP(); // Set mailer to use SMTP
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `tls` also accepted
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->Port = 587; // TCP port to connect to 465 or 587 Use ssl / 465 or tls/ 587;
$mail->Mailer = "smtp";
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = 'ucnote@2018'; // SMTP password
//Recipients
$mail->setFrom("[email protected]", "UcNOTE");
$mail->addAddress($stu_email, $stu_name); // Add a recipient
// $mail->addAddress('[email protected]'); // Name is optional
$mail->addReplyTo($email, $full_name);
// $mail->addCC('[email protected]');
// $mail->addBCC('[email protected]');
//Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $full_name." uploaded an assignment.";
$mail->Body = $message;
// $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
// echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error';
}
}
`
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
+1 for including test