提问人:beta-developper 提问时间:11/17/2023 最后编辑:beta-developper 更新时间:11/19/2023 访问量:19
Symfony Mailer:在 MessageSent 事件上捕获消息时丢失上下文数据
Symfony Mailer: Message lost context data when captured on MessageSent event
问:
我正在尝试使用SentMessageEvent的事件侦听器捕获从Symfony 6.3应用程序发送的电子邮件。我面临的问题是,当我创建电子邮件(使用 TemplatedEmail)时,我在电子邮件上设置的上下文在触发 onMessageSent 方法时似乎是空的。
以下是我发送电子邮件的方式:
<?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 在发送电子邮件后有意清除上下文?
答: 暂无答案
评论