Symfony 5 中实现同步与异步邮件发送的灵活策略

admin 百科 13

Symfony 5 中实现同步与异步邮件发送的灵活策略

本文详细介绍了在 symfony 5 应用程序中如何灵活地实现同步和异步邮件发送。通过创建自定义消息类和消息处理器,并结合 symfony messenger 组件的路由配置,开发者可以精确控制哪些邮件通过消息队列异步发送,而哪些邮件则立即同步发送,从而优化应用性能和用户体验。

在现代 Web 应用中,邮件发送是常见的需求,但有时我们需要根据业务场景选择不同的发送模式:对于非关键或耗时较长的邮件(如通知、批量邮件),异步发送可以避免阻塞主线程,提升用户体验;而对于关键或需要即时反馈的邮件(如密码重置),同步发送则更为合适。Symfony 5 结合其 Mailer 和 Messenger 组件,提供了一套强大的机制来实现这种灵活的邮件发送策略。

1. 理解 Symfony Mailer 与 Messenger

  • Symfony Mailer: 这是 Symfony 官方推荐的邮件发送组件,它提供了一个统一的接口 MailerInterface 来发送邮件,支持多种传输方式(SMTP, Sendgrid, Mailgun 等)。
  • Symfony Messenger: 这是一个消息队列组件,允许应用将耗时任务(如发送邮件、处理图片)异步化。它通过消息总线(Message Bus)、消息(Message)和消息处理器(Message Handler)来工作。

当我们将 Symfony\Component\Mailer\Messenger\SendEmailMessage 路由到异步传输时,所有通过 MailerInterface::send() 方法发送的邮件都会被 Messenger 捕获并推送到队列中。为了实现同步和异步邮件的共存,我们需要更精细的控制。

Symfony 5 中实现同步与异步邮件发送的灵活策略-第2张图片-佛山资讯网

2. 定义异步邮件消息类

为了让 Messenger 能够处理我们的异步邮件,我们首先需要创建一个自定义的消息类。这个类将封装所有发送邮件所需的数据,如收件人、主题、邮件模板路径和上下文变量等。

<?php

namespace App\Message;

class EmailAsync
{
    private string $subject;
    private string $bodyHtmlTemplate;
    private ?string $bodyTextTemplate;
    private string $recipient;
    private array $context = [];
    private string $senderEmail; // 添加发件人邮箱

    public function __construct(
        string $senderEmail,
        string $recipient,
        string $subject,
        string $bodyHtmlTemplate,
        ?string $bodyTextTemplate = null,
        array $context = []
    ) {
        $this->senderEmail = $senderEmail;
        $this->recipient = $recipient;
        $this->subject = $subject;
        $this->bodyHtmlTemplate = $bodyHtmlTemplate;
        $this->bodyTextTemplate = $bodyTextTemplate;
        $this->context = $context;
    }

    // 提供所有属性的公共 getter 方法
    public function getSenderEmail(): string
    {
        return $this->senderEmail;
    }

    public function getRecipient(): string
    {
        return $this->recipient;
    }

    public function getSubject(): string
    {
        return $this->subject;
    }

    public function getBodyHtmlTemplate(): string
    {
        return $this->bodyHtmlTemplate;
    }

    public function getBodyTextTemplate(): ?string
    {
        return $this->bodyTextTemplate;
    }

    public function getContext(): array
    {
        return $this->context;
    }
}

登录后复制

注意:消息类应只包含数据,不应包含业务逻辑。所有属性都应是私有的,并通过 getter 方法暴露。

3. 实现异步邮件消息处理器

消息处理器负责接收并处理特定类型的消息。在这里,EmailAsyncHandler 将接收 EmailAsync 消息,并使用 MailerInterface 实际发送邮件。

<?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 MailerInterface $mailer;

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

    public function __invoke(EmailAsync $emailAsync): void
    {
        $emailToSend = (new TemplatedEmail())
            ->from(new Address($emailAsync->getSenderEmail())) // 使用消息中的发件人
            ->to(new Address($emailAsync->getRecipient()))
            ->subject($emailAsync->getSubject())
            ->htmlTemplate($emailAsync->getBodyHtmlTemplate())
            ->context($emailAsync->getContext());

        if ($emailAsync->getBodyTextTemplate()) {
            $emailToSend->textTemplate($emailAsync->getBodyTextTemplate());
        }

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

登录后复制

注意

标签: php redis html 处理器 编码 app 工具 ai 路由 环境变量 配置文件 邮箱 自动重启 密码重置 re

发布评论 0条评论)

还木有评论哦,快来抢沙发吧~