php - Messenger __invoke 方法中的 Swiftmailer 不发送邮件
问题描述
我目前在将 \Swift_Mailer 用于 Symfony MessageHandlerInterface 时遇到问题。在此类的 __invoke 方法中,我想使用 Swift_Mailer 发送电子邮件。
/**
* @param MailMessage $mailMessage
*/
public function __invoke(MailMessage $mailMessage)
{
try {
$message = (new \Swift_Message('You Got Mail!'))
->setFrom('no-reply@example.be' , 'No reply')
->setTo('development@example.be')
->setBody(
'hello world',
'text/plain'
)
;
$successfulRecipientsCount = $this->mailer->send($message, $failedRecipients);
if ($successfulRecipientsCount < 1 || count($failedRecipients) > 0) {
throw new \Exception('error sending mail');
}
} catch (\Exception $e) {
$this->logger->error( $e->getMessage());
}
}
但是当我想使用这些消息时,邮件程序永远不会将邮件发送到目的地。当我将此片段用于控制器时,它就可以工作。所以我的配置中的邮件参数没有问题。
我知道 Symfony 的控制器调度 kernel.terminate 事件,Swift_Mailer 利用这个事件发送电子邮件。
如何在 Symfony 的 MessengerHandlerInterface 的魔术方法 __invoke 中调度 kernel.terminate 事件?
解决方案
推荐阅读
- configuration - 使用 Snakemake CLI 将可变长度数组声明为配置值
- r - R - 改变一条线的起点
- python-3.x - 使用pandas groupby功能,如何查询数据子集并将其放入列表
- python-3.x - 在引用和循环另一个df时在Pandas中使用apply函数?
- python - numpy.random.rand(n) 中重复的几率是多少(假设完全随机)?
- ios - Cordova 插件媒体不会在 ios 11.0 和 12.0 上正确触发 onStatus 回调
- c# - JSON.NET 反序列化似乎认为 1000 是 1000.0 从而导致整数转换错误,为什么?
- spring - Hibernate Jpa - 主键(序列)上的约束违规异常
- python - 从 2 列切换值
- r - data.table - 按多个组列选择行