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 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码