I have written a custom console command to query my database, produce a report and e-mail it to an address; however I can not seem to successfully send the e-mail. I can send e-mail fine from within normal controllers elsewhere within my application, and I can also send it from within my console command if I manually create and configure a Swift_Mailer instance and not get it via the container.
我编写了一个自定义控制台命令来查询我的数据库,生成报告并通过电子邮件发送到一个地址;但我似乎无法成功发送电子邮件。我可以从我的应用程序中的其他地方的普通控制器中发送电子邮件,如果我手动创建和配置Swift_Mailer实例而不是通过容器获取它,我也可以从我的控制台命令中发送它。
Here is a stripped down version of my console command:
这是我的控制台命令的精简版:
<?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);
}
}
Swiftmailer is configured to send via SMTP within my app/config/config.yml
file (delivery_address: dev@domain.com
is also set in app/config/config_dev.yml
):
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
When running the command, it prints 1
to the command line, which I assume it means it was successful. However I'm monitoring my mail server's logs at the same time, and it's not even connecting.
运行命令时,它会在命令行中打印1,我认为这意味着它成功了。但是,我正在同时监视我的邮件服务器的日志,它甚至没有连接。
To confirm that my configuration was being loaded into the mailer, I changed the spool from memory
to file
and messages are being spooled to the file system when running the command and I can successfully flush the spool on the command line with php app/console swiftmailer:spool:send
.
为了确认我的配置被加载到邮件程序中,我将假脱机从内存更改为文件,并在运行命令时将消息假脱机到文件系统,我可以使用php app / console swiftmailer在命令行上成功刷新假脱机:阀芯:发送。
Does anyone have any ideas as to what's happening here, or any suggestions as to how I can debug it further? Nothing is appearing in my app/logs/dev.log
file. I'm using Symfony 2.1.3-DEV.
有没有人对这里发生的事情有任何想法,或者有关如何进一步调试它的任何建议?我的app / logs / dev.log文件中没有出现任何内容。我正在使用Symfony 2.1.3-DEV。
3 个解决方案
#1
27
From digging some Symfony and SwiftMailer code, I can see that the memory spool is flushed on the kernel.terminate
event that occurs after a response was sent. I'm not sure it works for the commands, but I may be wrong.
从挖掘一些Symfony和SwiftMailer代码,我可以看到内存假脱机在发送响应后发生的kernel.terminate事件上刷新。我不确定它是否适用于命令,但我可能错了。
Try adding this code at the end of your command and see if it helps:
尝试在命令末尾添加此代码,看看它是否有帮助:
$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'));
#2
2
Instead of memory spool use file spool. Modify your app/config/config.yml
:
而不是内存假脱机使用文件假脱机。修改你的app / config / config.yml:
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: file, path: %kernel.root_dir%/spool }
#3
-1
Just add this to the end of your execute action:
只需将其添加到执行操作的末尾:
$container = $this->getContainer();
$mailer = $container->get('mailer');
$spool = $mailer->getTransport()->getSpool();
$transport = $container->get('swiftmailer.transport.real');
$spool->flushQueue($transport);
#1
27
From digging some Symfony and SwiftMailer code, I can see that the memory spool is flushed on the kernel.terminate
event that occurs after a response was sent. I'm not sure it works for the commands, but I may be wrong.
从挖掘一些Symfony和SwiftMailer代码,我可以看到内存假脱机在发送响应后发生的kernel.terminate事件上刷新。我不确定它是否适用于命令,但我可能错了。
Try adding this code at the end of your command and see if it helps:
尝试在命令末尾添加此代码,看看它是否有帮助:
$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'));
#2
2
Instead of memory spool use file spool. Modify your app/config/config.yml
:
而不是内存假脱机使用文件假脱机。修改你的app / config / config.yml:
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: file, path: %kernel.root_dir%/spool }
#3
-1
Just add this to the end of your execute action:
只需将其添加到执行操作的末尾:
$container = $this->getContainer();
$mailer = $container->get('mailer');
$spool = $mailer->getTransport()->getSpool();
$transport = $container->get('swiftmailer.transport.real');
$spool->flushQueue($transport);