在 swiftmailer 中限制电子邮件发送重试

Limit email sending retries in swiftmailer

我使用 Swiftmailer 编写了以下批量电子邮件发送脚本(使用它来发送时事通讯)。通常一切正常,除了几个用户,他们的电子邮件服务器有问题,不断拒绝电子邮件(例如,由于收件箱已满,无法验证反向 DNS 条目,...)。问题是 Swiftmailer 会无限次重试发送到这些电子邮件(直到服务器重新启动)。

有什么方法可以限制重试次数吗?

我读到 Swift_FileSpool class 具有 setRetryLimit 功能,默认值为 10 重试。但我不确定如何使用它。此外,由于某种原因,默认重试限制似乎不适用。

<?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>\r\n","</p>\r\n","<br/>\r\n","<br/>\r\n","<br/>\r\n"),$text)),'text/plain');

foreach($emails as $email) {
    $message->setTo($email);
    $swMailer->send($message);
}

?>

您的代码显示您正在使用 SwiftMailers Swift_MailTransport() 作为传输器 class,它基于 PHP 的内置 mail() 函数。这非常便携,但可能会产生不可预测的结果,并提供极其微弱的反馈。

mail()函数通常将邮件放入本地邮件投递代理的队列中。检查服务器的 smtp 配置以更改行为,p.e。重试间隔等

为了更直接地控制您的脚本行为,您可以考虑切换到 Swift_SmtpTransport()