我正在尝试使用 SentMessageEvent 的事件侦听器捕获从 Symfony 6.3 应用程序发送的电子邮件。我面临的问题是,当触发 onMessageSent 方法时,我在创建电子邮件时(使用 TemplatedEmail)在电子邮件上设置的上下文似乎为空。
这是我发送电子邮件的方式:
<?php
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\MailerInterface;
readonly class EmailService
{
public function __construct(
private Mailer $mailer,
) {
}
public function sendEmail(): void
{
$email = (new TemplatedEmail())
->to('[email protected]')
->from('[email protected]')
->subject('My subject')
->htmlTemplate('email/email.html.twig')
->context([
'data' => [
'key' => 'value'
]
]);
$this->mailer->send($email);
}
}
这是我的事件监听器:
<?php
namespace App\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mailer\Event\MessageEvent;
use Symfony\Component\Mailer\Event\SentMessageEvent;
class MailEventListener implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
SentMessageEvent::class => 'onMessageSent',
MessageEvent::class => 'onMessage',
];
}
public function onMessage(MessageEvent $event): void
{
// Here I can access the context
dump($event->getMessage());
// Here is the output, the context contains the values setted when the TemplatedEmail Object
/*
Symfony\Bridge\Twig\Mime\TemplatedEmail {#1975
-message: null
-headers: Symfony\Component\Mime\Header\Headers {#1978
-headers: array:3 [
"to" => array:1 [
0 => Symfony\Component\Mime\Header\MailboxListHeader {#1971
-name: "To"
-lineLength: 76
-lang: null
-charset: "utf-8"
-addresses: array:1 [
0 => Symfony\Component\Mime\Address {#1958
-address: "[email protected]"
-name: ""
}
]
}
]
"from" => array:1 [
0 => Symfony\Component\Mime\Header\MailboxListHeader {#1973
-name: "From"
-lineLength: 76
-lang: null
-charset: "utf-8"
-addresses: array:1 [
0 => Symfony\Component\Mime\Address {#1970
-address: "[email protected]"
-name: ""
}
]
}
]
"subject" => array:1 [
0 => Symfony\Component\Mime\Header\UnstructuredHeader {#1981
-name: "Subject"
-lineLength: 76
-lang: null
-charset: "utf-8"
-value: "My subject"
}
]
]
-lineLength: 76
}
-body: null
-text: null
-textCharset: null
-html: null
-htmlCharset: null
-attachments: []
-cachedBody: null
-htmlTemplate: "email/email.html.twig"
-textTemplate: null
-context: array:1 [
"data" => array:1 [
"key" => "value"
]
]
}
*/
}
public function onMessageSent(SentMessageEvent $event): void
{
// Here, the context seems to be empty
dump($event->getMessage()->getOriginalMessage() );
// Here is the output, we can see clearly that the context is empty
// Also the context is empty if I inspect the getEnvelope(), getOriginalMessage() and getMessage() methods
/*
MailEventListener.php on line 27:
Symfony\Bridge\Twig\Mime\TemplatedEmail {#2318
-message: null
-headers: Symfony\Component\Mime\Header\Headers {#2319
-headers: array:3 [
"to" => array:1 [
0 => Symfony\Component\Mime\Header\MailboxListHeader {#2321
-name: "To"
-lineLength: 76
-lang: null
-charset: "utf-8"
-addresses: array:1 [
0 => Symfony\Component\Mime\Address {#1958
-address: "[email protected]"
-name: ""
}
]
}
]
"from" => array:1 [
0 => Symfony\Component\Mime\Header\MailboxListHeader {#2320
-name: "From"
-lineLength: 76
-lang: null
-charset: "utf-8"
-addresses: array:1 [
0 => Symfony\Component\Mime\Address {#1970
-address: "[email protected]"
-name: ""
}
]
}
]
"subject" => array:1 [
0 => Symfony\Component\Mime\Header\UnstructuredHeader {#2323
-name: "Subject"
-lineLength: 76
-lang: null
-charset: "utf-8"
-value: "My subject"
}
]
]
-lineLength: 76
}
-body: null
-text: """
Email content: value
"""
-textCharset: "utf-8"
-html: """
<p>
Email content: <b>value</b>
</p>
"""
-htmlCharset: "utf-8"
-attachments: []
-cachedBody: null
-htmlTemplate: null
-textTemplate: null
-context: []
}
*/
}
}
有没有办法确保在捕获发送的电子邮件时保留上下文,或者 Symfony 是否在发送电子邮件后故意清除上下文?
渲染电子邮件模板时,上下文以及其他参数都会重置。
所以我们无法在电子邮件发送后获取上下文。