Kri*_*ris 9 php swiftmailer symfony
我编写了一个自定义控制台命令来查询我的数据库,生成报告并通过电子邮件发送到一个地址; 但是我似乎无法成功发送电子邮件.我可以从我的应用程序中的其他地方的普通控制器中发送电子邮件,如果我手动创建和配置Swift_Mailer实例而不是通过容器获取它,我也可以从我的控制台命令中发送它.
这是我的控制台命令的精简版:
<?php
namespace Foo\ReportBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class ExpiryCommand extends ContainerAwareCommand
{
protected function configure()
{
$this->setName('report:expiry')
->setDescription('Compile and send e-mail listing imminent expiries');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
/* ... */
$message = \Swift_Message::newInstance()
->setSubject('Expiry report')
->setFrom('DoNotReply@domain.com')
->setTo('recipient@domain.com')
->setBody($body);
$mailer = $this->getContainer()->get('mailer');
/* This works...
$transport = \Swift_SmtpTransport::newInstance('smtp.domain.com', 25)
->setUsername('username')
->setPassword('password');
$mailer = \Swift_Mailer::newInstance($transport);
*/
$result = $mailer->send($message);
$output->writeln($result);
}
}
Run Code Online (Sandbox Code Playgroud)
Swiftmailer配置为通过我的app/config/config.yml
文件中的SMTP发送(delivery_address: dev@domain.com
也设置为app/config/config_dev.yml
):
swiftmailer:
transport: smtp
host: smtp.domain.com
username: username
password: password
spool:
type: memory
Run Code Online (Sandbox Code Playgroud)
在运行命令时,它会打印1
到命令行,我认为这意味着它成功了.但是,我正在同时监视我的邮件服务器的日志,它甚至没有连接.
为了确认我的配置被加载到邮件程序中,我将假脱机从更改为memory
,file
并且在运行命令时将消息假脱机到文件系统,并且我可以使用命令行成功刷新假脱机php app/console swiftmailer:spool:send
.
有没有人对这里发生的事情有任何想法,或者有关如何进一步调试它的任何建议?我的app/logs/dev.log
文件中没有出现任何内容.我正在使用Symfony 2.1.3-DEV.
Eln*_*mov 27
通过挖掘一些Symfony和SwiftMailer代码,我可以看到kernel.terminate
在发送响应后发生的事件上刷新了内存假脱机.我不确定它是否适用于命令,但我可能错了.
尝试在命令末尾添加此代码,看看它是否有帮助:
$transport = $this->container->get('mailer')->getTransport();
if (!$transport instanceof \Swift_Transport_SpoolTransport) {
return;
}
$spool = $transport->getSpool();
if (!$spool instanceof \Swift_MemorySpool) {
return;
}
$spool->flushQueue($this->container->get('swiftmailer.transport.real'));
Run Code Online (Sandbox Code Playgroud)