I have written the following bulk email sending script using Swiftmailer (using it to deliver newsletter). Usually everything works fine except couple of users, who have problematic email servers, which constantly reject emails (e.g., due to full inbox, failure to verify reverse DNS entries, ...). The problem is that the Swiftmailer retries sending to those emails infinitely many times (until server is restarted).
Is there a some way to limit amount of retries?
I have read that Swift_FileSpool
class has setRetryLimit
function and that the default value is 10
retries. But I am not sure how do I use that. Also it seems that the default retry limit does not apply for some reason.
<?php
$emails=get_to_emails();//list of emails
require_once(SWIFTMAILER_KELIAS."swift_required.php");
$swMailer=Swift_Mailer::newInstance(Swift_MailTransport::newInstance());
$swMailer->registerPlugin(new Swift_Plugins_ThrottlerPlugin(10,Swift_Plugins_ThrottlerPlugin::MESSAGES_PER_MINUTE));
$message=Swift_Message::newInstance();
$message->setContentType('text/html');
$message->setCharset('UTF-8');
$message->setSender(array(ADMIN=>ADMIN_NAME));
$message->setFrom(array(ADMIN=>ADMIN_NAME));
$message->setSubject("subject");
$message->setBody($text,'text/html');
$message->addPart(strip_tags(str_replace(array("</h1>","</p>","</br>","<br>","<br/>"),array("</h1>
","</p>
","<br/>
","<br/>
","<br/>
"),$text)),'text/plain');
foreach($emails as $email) {
$message->setTo($email);
$swMailer->send($message);
}
?>
Your code shows that you're using SwiftMailers Swift_MailTransport()
as transporter class, which is based on PHP's built-in mail()
function. This is very portable, but has potentially unpredictable results and provides extremely weak feedback.
The mail()
function usually puts the mail into the queue of the local mail delivery agent. Check your smtp configuration of your server to change the behaviour, p.e. retry intervals etc.
In order to have more direct control over your scripts behaviour, you might consider switching to Swift_SmtpTransport()
.