Shopware6 中的 MailBeforeValidateEvent

MailBeforeValidateEvent in Shopware6

提问人:Moss 提问时间:11/14/2023 更新时间:11/14/2023 访问量:9

问:

我目前正在使用 Shopware,我想知道在将电子邮件发送到队列之前是否触发了此事件。我在文档中找不到详细信息,我希望在这方面有经验的人可以澄清一下。MailBeforeValidateEvent

活动 shopware6 电子邮件模板

评论


答:

1赞 Marcus 11/20/2023 #1

假设您在默认配置中使用最新的 Shopware 6 版本(当前:):v6.5.6.1

在 的开头调度:MailBeforeValidateEventShopware\Core\Content\Mail\Service\MailService::send()

/**
 * @param mixed[] $data
 * @param mixed[] $templateData
 */
public function send(array $data, Context $context, array $templateData = []): ?Email
{
    $event = new MailBeforeValidateEvent($data, $context, $templateData);
    $this->eventDispatcher->dispatch($event);
    /* ... */

然后,在同一函数的末尾,邮件被发送:

    /* ... */
    $this->mailSender->send($mail);

    $event = new MailSentEvent($data['subject'], $recipients, $contents, $context, $templateData['eventName'] ?? null);
    $this->eventDispatcher->dispatch($event);

    return $mail;
}

这导致哪些调用,在内部被调度:Shopware\Core\Content\Mail\Service\MailSender::send()Symfony\Component\Mailer\Mailer::send()Symfony\Component\Mailer\Messenger\SendEmailMessage

public function send(RawMessage $message, Envelope $envelope = null): void
{
    if (null === $this->bus) {
        $this->transport->send($message, $envelope);

        return;
    }

    $stamps = [];
    if (null !== $this->dispatcher) {
        // The dispatched event here has `queued` set to `true`; the goal is NOT to render the message, but to let
        // listeners do something before a message is sent to the queue.
        // We are using a cloned message as we still want to dispatch the **original** message, not the one modified by listeners.
        // That's because the listeners will run again when the email is sent via Messenger by the transport (see `AbstractTransport`).
        // Listeners should act depending on the `$queued` argument of the `MessageEvent` instance.
        $clonedMessage = clone $message;
        $clonedEnvelope = null !== $envelope ? clone $envelope : Envelope::create($clonedMessage);
        $event = new MessageEvent($clonedMessage, $clonedEnvelope, (string) $this->transport, true);
        $this->dispatcher->dispatch($event);
        $stamps = $event->getStamps();

        if ($event->isRejected()) {
            return;
        }
    }

    try {
        $this->bus->dispatch(new SendEmailMessage($message, $envelope), $stamps);
    } catch (HandlerFailedException $e) {
        foreach ($e->getNestedExceptions() as $nested) {
            if ($nested instanceof TransportExceptionInterface) {
                throw $nested;
            }
        }
        throw $e;
    }
}

它由 ,调用配置的邮件传输(等)进行处理。Symfony\Component\Mailer\Messenger\MessageHandlersendmail

因此,在将电子邮件发送到队列之前会触发 MailBeforeValidateEvent