指尖上的记忆指尖上的记忆
首页
  • 基础
  • Laravel框架
  • Symfony框架
  • 基础
  • Gin框架
  • 基础
  • Spring框架
  • 命令
  • Nginx
  • Ai
  • Deploy
  • Docker
  • K8s
  • Micro
  • RabbitMQ
  • Mysql
  • PostgreSsql
  • Redis
  • MongoDb
  • Html
  • Js
  • 前端
  • 后端
  • Git
  • 知识扫盲
  • Golang
🌟 gitHub
首页
  • 基础
  • Laravel框架
  • Symfony框架
  • 基础
  • Gin框架
  • 基础
  • Spring框架
  • 命令
  • Nginx
  • Ai
  • Deploy
  • Docker
  • K8s
  • Micro
  • RabbitMQ
  • Mysql
  • PostgreSsql
  • Redis
  • MongoDb
  • Html
  • Js
  • 前端
  • 后端
  • Git
  • 知识扫盲
  • Golang
🌟 gitHub

symfony配置异步邮件发送,主要有两种方式

  • 使用默认的messageHandler
framework:
    messenger:
        failure_transport: failed

        transports:
            # https://symfony.com/doc/current/messenger.html#transport-configuration
            async:
                dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
                options:
                    use_notify: true
                    check_delayed_interval: 60000
                retry_strategy:
                    max_retries: 3
                    multiplier: 2
            failed: 'doctrine://default?queue_name=failed'
            sync: 'sync://'

        routing:
            Symfony\Component\Mailer\Messenger\SendEmailMessage: async

            # Route your messages to the transports
            # 'App\Message\YourMessage': async
  • 使用自定义的messageHandler

1.先创建一个消息类

<?php

namespace App\Message;

class EmailAsync
{
    private $subject;

    private $body;

    private $bodyText;

    private $recipient;

    private $context = [];

    public function __construct($subject, $body, $bodyText, $recipient, $context)
    {
        $this->subject = $subject;
        $this->body = $body;
        $this->bodyText = $bodyText;
        $this->recipient = $recipient;
        $this->context = $context;
    }
}

2.创建一个消息处理类

<?php

namespace App\MessageHandler;

use App\Message\EmailAsync;
use Symfony\Component\Mime\Address;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

class EmailAsyncHandler  implements MessageHandlerInterface 
{

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

    public function __invoke(EmailAsync $email)
    {
        $subject = $email->getSubject();
        $body = $email->getBody();
        $bodyText = $email->getBodyText();
        $recipient = $email->getRecipient();
        $context = $email->getContext();

        $emailToSend = (new TemplatedEmail())
            ->from("my-address@hello.com")
            ->to(new Address("your-address@hello.com"))
            ->subject($subject)

            // path of the Twig template to render
            ->htmlTemplate($body)
            ->textTemplate($bodyText)

            // pass variables (name => value) to the template
            ->context($context)
        ;

        $this->mailer->send($emailToSend);
    }
}

3.配置messageHandler

framework:
    messenger:
        transports:
            async: '%env(MESSENGER_TRANSPORT_DSN)%'

        routing:
            # Route your messages to the transports
            App\Message\EmailAsync:  async

4.触发邮件发送服务

<?php

namespace App\Service;

use App\Message\EmailAsync;
use Symfony\Component\Messenger\MessageBusInterface;

class MailManagerAsync
{
    protected $bus;

    public function __construct(MessageBusInterface $bus)
    {
        $this->bus = $bus;
    }

    protected function sendMessage($subject, $body, $bodyText, $to, $context = [])
    {
        $emailAsync = new EmailAsync($subject, $body, $bodyText, $to, $context);
        $this->bus->dispatch($emailAsync);
    }
}

整个流程和普通的消息队列是一样的

注意点:

之前我这里有个问题,在config/packages/dev/mailer.yaml 里配置了:
framework:
    mailer:
        dsn: '%env(MAILER_DSN)%'
        message_bus: false

根据官网的介绍(https://symfony.com/doc/5.4/mailer.html#sending-messages-async)可以知道,这里配置
为false以后 asymc 就会失效,尤其是这个 dev 环境,直接覆盖了我的配置,所以导致 async 一直不生效