The method Send()
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 keep trying to send, but this is not a good practice because the application can get stuck in it trying to send without success, 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);
}
Icardo this is not a very good idea... I would get the application stuck in it until he could send it. The best would be to have a cron running x in x time trying to send pending emails.
– Jorge B.
@Jorgeb. he can limit the number of attempts, I’ll add that now
– Ricardo
Ricardo but this does not solve anything. If the server is low the probability of all fail is huge.
– Jorge B.
@Jorgeb. What would be "the server is down"?
– Ricardo
@This loop of attempts I’ve seen in other sources and helps a lot. The bigger question would be in relation to when the server goes down (that’s what Jorgeb. must mean by 'server below'. There would be pending emails whose sending attempts failed. Still, thank you for the replies.
– Gustavo Hoppe Levin