编辑:这个问题出现在试图在同一个应用程序中同时拥有同步和同步电子邮件的过程中。没有说清楚。在撰写本文时,这是不可能的,至少不像这里尝试的那样简单。请参阅下面@msg 的评论。
配置为异步发送电子邮件的电子邮件服务,而是立即发送电子邮件。这发生在doctrine
或amqp
选择为MESSENGER_TRANSPORT_DSN
。doctrine
传输成功创建messenger_messages
表,但没有内容。这告诉我MESSENGER_TRANSPORT_DSN
观察到了。使用 RabbitMQ 'Hello World' 教程的简单测试amqp
表明它配置正确。
我在下面的代码中遗漏了什么?
如下所示的序列摘要:添加机会 ->OppEmailService
创建电子邮件内容 ->TemplatedEmail()
从EmailerService
(未显示)获取对象 -> 将TemplatedEmail()
对象提交到LaterEmailService
,配置为异步。
信使.yaml:
framework:
messenger:
transports:
async: '%env(MESSENGER_TRANSPORT_DSN)%'
sync: 'sync://'
routing:
'App\Services\NowEmailService': sync
'App\Services\LaterEmailService': async
OpportunityController
:
class OpportunityController extends AbstractController
{
private $newOpp;
private $templateSvc;
public function __construct(OppEmailService $newOpp, TemplateService $templateSvc)
{
$this->newOpp = $newOpp;
$this->templateSvc = $templateSvc;
}
...
public function addOpp(Request $request): Response
{
...
if ($form->isSubmitted() && $form->isValid()) {
...
$volunteers = $em->getRepository(Person::class)->opportunityEmails($opportunity);
$this->newOpp->oppEmail($volunteers, $opportunity);
...
}
OppEmailService
:
class OppEmailService
{
private $em;
private $makeMail;
private $laterMail;
public function __construct(
EmailerService $makeMail,
EntityManagerInterface $em,
LaterEmailService $laterMail
)
{
$this->makeMail = $makeMail;
$this->em = $em;
$this->laterMail = $laterMail;
}
...
public function oppEmail($volunteers, $opp): array
{
...
$mailParams = [
'template' => 'Email/volunteer_opportunities.html.twig',
'context' => ['fname' => $person->getFname(), 'opportunity' => $opp,],
'recipient' => $person->getEmail(),
'subject' => 'New volunteer opportunity',
];
$toBeSent = $this->makeMail->assembleEmail($mailParams);
$this->laterMail->send($toBeSent);
...
}
}
LaterEmailService
:
namespace App\Services;
use Symfony\Component\Mailer\MailerInterface;
class LaterEmailService
{
private $mailer;
public function __construct(MailerInterface $mailer)
{
$this->mailer = $mailer;
}
public function send($email)
{
$this->mailer->send($email);
}
}