編輯:這個問題是在嘗試在同一個應用程式中同時使用同步和同步電子郵件時出現的。那沒有說清楚。在撰寫本文時,這是不可能的,至少不像這里嘗試的那么簡單。請參閱下面@msg 的評論。
配置為異步發送電子郵件的電子郵件服務會立即發送電子郵件。這發生在doctrine或amqp選擇為 時MESSENGER_TRANSPORT_DSN。doctrine傳輸成功創建messenger_messages表,但沒有內容。這告訴我MESSENGER_TRANSPORT_DSN觀察到的。amqp使用 RabbitMQ 'Hello World' 教程的簡單測驗表明它已正確配置。
我在下面的代碼中遺漏了什么?
下面顯示的序列摘要:添加機會 ->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);
}
}
uj5u.com熱心網友回復:
我最終創建了作為日常cron作業運行的控制臺命令。每個命令都會呼叫一個創建和發送電子郵件的服務。用例是每天向注冊用戶發送少量電子郵件,通知他們影響他們的操作。一個例子如下:
控制臺命令:
class NewOppsEmailCommand extends Command
{
private $mailer;
private $oppEmail;
private $twig;
public function __construct(OppEmailService $oppEmail, EmailerService $mailer, Environment $twig)
{
$this->mailer = $mailer;
$this->oppEmail = $oppEmail;
$this->twig = $twig;
parent::__construct();
}
protected static $defaultName = 'app:send:newoppsemaiils';
protected function configure()
{
$this->setDescription('Sends email re: new opps to registered');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$emails = $this->oppEmail->oppEmail();
$output->writeln($emails . ' email(s) were sent');
return COMMAND::SUCCESS;
}
}
Opp電子郵件服務:
class OppEmailService
{
private $em;
private $mailer;
public function __construct(EmailerService $mailer, EntityManagerInterface $em)
{
$this->mailer = $mailer;
$this->em = $em;
}
/**
* Send new opportunity email to registered volunteers
*/
public function oppEmail()
{
$unsentEmail = $this->em->getRepository(OppEmail::class)->findAll(['sent' => false], ['volunteer' => 'ASC']);
if (empty($unsentEmail)) {
return 0;
}
$email = 0;
foreach ($unsentEmail as $recipient) {
$mailParams = [
'template' => 'Email/volunteer_opportunities.html.twig',
'context' => [
'fname' => $recipient->getVolunteer()->getFname(),
'opps' => $recipient->getOpportunities(),
],
'recipient' => $recipient->getVolunteer()->getEmail(),
'subject' => 'New opportunities',
];
$this->mailer->assembleEmail($mailParams);
$recipient->setSent(true);
$this->em->persist($recipient);
$email ;
}
$this->em->flush();
return $email;
}
}
電子郵件服務:
class EmailerService
{
private $em;
private $mailer;
public function __construct(EntityManagerInterface $em, MailerInterface $mailer)
{
$this->em = $em;
$this->mailer = $mailer;
}
public function assembleEmail($mailParams)
{
$sender = $this->em->getRepository(Person::class)->findOneBy(['mailer' => true]);
$email = (new TemplatedEmail())
->to($mailParams['recipient'])
->from($sender->getEmail())
->subject($mailParams['subject'])
->htmlTemplate($mailParams['template'])
->context($mailParams['context'])
;
$this->mailer->send($email);
return $email;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/395899.html
