Symfony Swiftmailer未立即发送
我已成功将我的symfony webapp配置为使用SMTP发送电子邮件。但是我所有发送的电子邮件都被放在Symfony Swiftmailer未立即发送,symfony,configuration,ubuntu-14.04,swiftmailer,Symfony,Configuration,Ubuntu 14.04,Swiftmailer,我已成功将我的symfony webapp配置为使用SMTP发送电子邮件。但是我所有发送的电子邮件都被放在spool目录中 只有在发送过程中出现错误时才会发生这种情况。对吗 但是如果我执行命令swiftmailer:spool:send--env=prod,我所有的电子邮件都会正确发送 为什么我的服务器没有立即发送电子邮件? 那是因为我修正了一个错误吗?有办法解决这个问题吗 速递员: spool: type: file path: %kernel.root_dir%/spool
spool
目录中
只有在发送过程中出现错误时才会发生这种情况。对吗
但是如果我执行命令swiftmailer:spool:send--env=prod,我所有的电子邮件都会正确发送
为什么我的服务器没有立即发送电子邮件?
那是因为我修正了一个错误吗?有办法解决这个问题吗
速递员:
spool:
type: file
path: %kernel.root_dir%/spool
您可以强制冲洗滑阀。 例如:
$mailer = $this->container->get('mailer');
$mailer->send($message);
$spool = $mailer->getTransport()->getSpool();
$transport = $this->container->get('swiftmailer.transport.real');
if ($spool and $transport) $spool->flushQueue($transport);
还要检查config.yml中的假脱机配置
如果您有:
swiftmailer:
....
spool: { type: memory }
邮件在内核终止事件中发送(因此在页面末尾)A只需将命令
swiftmailer:spool:send
添加到crontab
中即可。此步骤不清楚。如果有人通过消息队列(symfony/messenger)处理电子邮件,最好使用内存假脱机。但是,仅在Kernel::terminate
事件上处理内存假脱机。此事件永远不会在长时间运行的console worker上发生
此内核事件正在调用Symfony\Bundle\SwiftmailerBundle\EventListener\EmailSenderListener::onTerminate()
方法。您可以通过调度自己的事件并向其订阅上述方法来手动调用此方法
src/App/Email/Events.php
<?php
namespace App\Email;
class Events
{
public const UNSPOOL = 'unspool';
}
services:
App\Email\AmqpHandler:
tags: [messenger.message_handler]
Symfony\Bundle\SwiftmailerBundle\EventListener\EmailSenderListener:
tags:
- name: kernel.event_listener
event: !php/const App\Email\Events::UNSPOOL
method: onTerminate
<?php
namespace App\Email;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class AmqpHandler
{
/** @var EventDispatcherInterface */
private $eventDispatcher;
/** @var Swift_Mailer */
private $mailer;
public function __construct(EventDispatcherInterface $eventDispatcher, Swift_Mailer $mailer)
{
$this->eventDispatcher = $eventDispatcher;
$this->mailer = $mailer;
}
public function __invoke($emailMessage): void
{
//...
$message = (new Swift_Message($subject))
->setFrom($emailMessage->from)
->setTo($emailMessage->to)
->setBody($emailMessage->body, 'text/html');
$successfulRecipientsCount = $this->mailer->send($message, $failedRecipients);
if ($successfulRecipientsCount < 1 || count($failedRecipients) > 0) {
throw new DeliveryFailureException($message);
}
$this->eventDispatcher->dispatch(Events::UNSPOOL);
}
}
您的消息队列工作程序
src/App/Email/amqpmhandler.php
<?php
namespace App\Email;
class Events
{
public const UNSPOOL = 'unspool';
}
services:
App\Email\AmqpHandler:
tags: [messenger.message_handler]
Symfony\Bundle\SwiftmailerBundle\EventListener\EmailSenderListener:
tags:
- name: kernel.event_listener
event: !php/const App\Email\Events::UNSPOOL
method: onTerminate
<?php
namespace App\Email;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class AmqpHandler
{
/** @var EventDispatcherInterface */
private $eventDispatcher;
/** @var Swift_Mailer */
private $mailer;
public function __construct(EventDispatcherInterface $eventDispatcher, Swift_Mailer $mailer)
{
$this->eventDispatcher = $eventDispatcher;
$this->mailer = $mailer;
}
public function __invoke($emailMessage): void
{
//...
$message = (new Swift_Message($subject))
->setFrom($emailMessage->from)
->setTo($emailMessage->to)
->setBody($emailMessage->body, 'text/html');
$successfulRecipientsCount = $this->mailer->send($message, $failedRecipients);
if ($successfulRecipientsCount < 1 || count($failedRecipients) > 0) {
throw new DeliveryFailureException($message);
}
$this->eventDispatcher->dispatch(Events::UNSPOOL);
}
}
是的,我在某些情况下会这样做。但是当FOSUserBundle
发送电子邮件时,队列不会刷新。我将spool typy用作文件
。因此,只有在我手动刷新时才会发送电子邮件。在我的旧机器中,这是自动完成的。你不想使用内存吗?(至少对于FOSUserBundle)为什么要使用文件
和内存
?有关文件假脱机的优点,请参见的开头(但必须刷新…)是。现在我不明白了。我正在从其他人配置的机器进行迁移,所以这是我第一次从无到有地配置Symfony。不清楚是否应该将该命令添加到cron选项卡以发送电子邮件。