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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号