PHPMailer - Is it possible to resend an e-mail?

3

Is there any implementation that can be done in PHPMailer so that emails that could not be sent (due to the momentary drop in the site server that uses PHPMailer , for example) can be sent later?

It can be some kind of monitoring of the email server or record of emails to be sent (check if they were sent or not).

    
asked by anonymous 22.07.2015 / 14:39

2 answers

3

What you can do here is to create a table in the database with the emails that were not sent and by a cron to run from hour to hour for example, or the interval you want to try to resend the emails that were pending.

    
22.07.2015 / 16:19
0

The Send() method of the class returns a Boolean that will be false if the message is not sent for any reason.

<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp1.example.com;smtp2.example.com";
$mail->SMTPAuth = true;
$mail->Username = 'smtpusername';
$mail->Password = 'smtppassword';

$mail->AddAddress("[email protected]");
$mail->Subject = "assunto";
$mail->Body = "PHPMailer.";

if(!$mail->Send())
{
   echo "Erro: " . $mail->ErrorInfo;
}
else
{
   echo "E-mail enviado";
}
?>

You can use an infinite Loop to try to send, but this is not a good practice because the application can get stuck in it trying to send unsuccessfully, so I used a for limiting the number of attempts to 10, each attempt is spaced in 30 seconds.

for($tentativas = 0; $tentativas < 10; $tentativas++){
    if($mail->Send())
    {
       break;
    }
    sleep(30);
}
    
22.07.2015 / 15:07