【问题标题】:When sending emails from a worker, they are not send untill an exception is thrown从工作人员发送电子邮件时,除非引发异常,否则不会发送
【发布时间】:2019-10-21 08:58:27
【问题描述】:

我将几封邮件放入队列中。当我运行工作人员时,它会成功处理所有邮件,但不会发送。当我添加一个触发异常的邮件时,工作人员被杀死,所有邮件都在发送。

当我与工人处理邮件后运行bin/console swiftmailer:spool:send时,它会显示:

0 emails sent

也试过forceSend(),还是不行。

工人:

class SendQueuedMailWorker
{
    /**
     * @var Mailer
     */
    private $mailer;


    public function __construct(
        Mailer $mailer
    ) {
        $this->mailer = $mailer;
    }


    public function handle(SendQueuedMailCommand $command): void
    {

        $user = $command->getUser();

        $mailContext = [
            'user' => $user
        ];

        $message = $this->mailer->compose(
            'mail_template',
            $mailContext,
            null,
            $user->getEmail()
        );


        if ($this->mailer->send($message) === false) {
            throw QueuedMailNotSentException::withId($command->getReference());
        }
    }
}

Swiftmailer:

swiftmailer:
  transport:          '%mailer_transport%'
  encryption:         '%mailer_encryption%'
  host:               '%mailer_host%'
  port:               '%mailer_port%'
  username:           '%mailer_user%'
  password:           '%mailer_password%'
  delivery_addresses: '%mailer_delivery_addresses%'
  spool:              { type: 'memory' }
  disable_delivery:   '%mailer_disable_delivery%'

队列被消费时没有抛出错误信息。

控制台输出:

Consuming from mailsend queue
[1] Processing payload {"reference":2016320254, "user":"John"}
[1] processed with result: true
[2] Processing payload {"reference":2019990645, "user":"Doe"}
[2] processed with result: true

如何让邮件立即发送?

【问题讨论】:

    标签: symfony queue swiftmailer symfony-3.4


    【解决方案1】:

    固定为:https://stackoverflow.com/a/51730638/11830313

    原来由工作人员处理的假脱机邮件只会在Kernel::terminate 上发送。

    这对我来说似乎是一个激进的解决方案,所以如果有人对手头的问题有不同的解决方案,我会很高兴听到它。

    【讨论】:

      【解决方案2】:

      最终解决方案:

      创建MailerSpoolFlusher。将\Swift_MemorySpool\Swift_Transport 注入其中。

      MailerSpoolFlusher 中添加:

      public function flush()
      {
          $this->spool->flushQueue($this->transport);
      }
      

      然后将MailerSpoolFlusher注入worker,在$mailer->send()之后调用flush()

      像魅力一样工作,无需发起onTerminate

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多