无法从自定义Symfony2命令中发送电子邮件,但可以从应用程序中的其他位置发送电子邮件 [英] Unable to send e-mail from within custom Symfony2 command but can from elsewhere in app

查看:131
本文介绍了无法从自定义Symfony2命令中发送电子邮件,但可以从应用程序中的其他位置发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个自定义的控制台命令来查询我的数据库,生成报告并将其通过电子邮件发送到一个地址;但是我似乎无法成功发送电子邮件.我可以从应用程序内其他地方的常规控制器中正常发送电子邮件,如果我手动创建和配置Swift_Mailer实例而不通过容器获取它,也可以从控制台命令中发送电子邮件.

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.

这是我的控制台命令的精简版:

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配置为在我的app/config/config.yml文件中通过SMTP发送(delivery_address: dev@domain.com也已在app/config/config_dev.yml中设置):

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:
    transport: smtp
    host: smtp.domain.com
    username: username
    password: password
    spool:
        type: memory

运行命令时,它会将1打印到命令行,我认为这意味着成功.但是,我正在同时监视邮件服务器的日志,甚至没有连接.

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.

为确认我的配置已被加载到邮件程序中,我将假脱机从memory更改为file,并且在运行命令时将消息假脱机到文件系统,并且我可以成功刷新命令上的假脱机与php app/console swiftmailer:spool:send对齐.

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.

有人对这里发生的事情有任何想法吗,或者对我如何进一步调试它有任何建议吗?我的app/logs/dev.log文件中什么都没有显示.我正在使用Symfony 2.1.3-DEV.

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.

推荐答案

通过挖掘一些Symfony和SwiftMailer代码,我可以看到在发送响应后发生的kernel.terminate事件上,内存假脱机已刷新.我不确定它是否适用于这些命令,但是我可能是错的.

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.

尝试在命令末尾添加此代码,看看是否有帮助:

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'));

这篇关于无法从自定义Symfony2命令中发送电子邮件,但可以从应用程序中的其他位置发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆