duanfen1992 2016-05-13 09:04
浏览 20
已采纳

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

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);
}

?>
  • 写回答

1条回答 默认 最新

  • dtu1747 2016-05-16 07:37
    关注

    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().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵