php – 无法从自定义Symfony2命令发送电子邮件,但可以从应用程序
发布时间:2020-12-13 16:33:39 所属栏目:PHP教程 来源:网络整理
导读:我写了一个自定义控制台命令来查询我的数据库,生成一个报告并将其发送到一个地址;但是我似乎无法成功发送电子邮件.我可以在我的应用程序其他地方的正常控制器中发送电子邮件,如果我手动创建和配置 Swift_Mailer实例,而不是通过容器获取,我也可以从我的控制台
我写了一个自定义控制台命令来查询我的数据库,生成一个报告并将其发送到一个地址;但是我似乎无法成功发送电子邮件.我可以在我的应用程序其他地方的正常控制器中发送电子邮件,如果我手动创建和配置
Swift_Mailer实例,而不是通过容器获取,我也可以从我的控制台命令中发送电子邮件.
这是我的控制台命令的简化版本: <?php namespace FooReportBundleCommand; use SymfonyBundleFrameworkBundleCommandContainerAwareCommand; use SymfonyComponentConsoleInputInputInterface; use SymfonyComponentConsoleOutputOutputInterface; 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); } } 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 当运行命令时,它将打印1到命令行,我认为这意味着它是成功的.但是我同时监视我的邮件服务器的日志,甚至没有连接. 要确认我的配置正在加载到邮件程序中,我将内存更改为文件,并且在运行命令时将邮件正在假脱机到文件系统,我可以使用php应用程序/控制台swiftmailer在命令行上成功刷新假脱机:阀芯:发送. 有没有人有任何想法,这里发生了什么,或任何建议,我如何可以进一步调试?我的app / logs / dev.log文件中没有任何内容.我正在使用Symfony 2.1.3-DEV.
从挖掘一些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')); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |