Shopware6 中的MailBeforeValidateEvent

问题描述 投票:0回答:1

我目前正在 Shopware 中使用

MailBeforeValidateEvent
,我想知道在电子邮件发送到队列之前是否触发此事件。我在文档中找不到详细信息,希望有这方面经验的人能够澄清。

events shopware6 email-templates
1个回答
0
投票

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

v6.5.6.1
):

MailBeforeValidateEvent
Shopware\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\MessageHandler
处理,调用配置的邮件传输(
sendmail
等)。

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

MailBeforeValidateEvent

© www.soinside.com 2019 - 2024. All rights reserved.