Symfony2应用程序的控制台命令中发生错误时发送电子邮件 [英] Send email when error occurs in console command of Symfony2 app

查看:97
本文介绍了Symfony2应用程序的控制台命令中发生错误时发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在控制台命令中出现错误时,如何发送带有日志的电子邮件?目前,我已经将我的应用配置为通过网络界面发送电子邮件,并且可以正常运行。 Swiftmailer假脱机被禁用。我的配置是:

How is it possible to send email with log when something wrong in console command? Currently I've configured my app to send emails from web interface and it works correctly. Swiftmailer spool is disabled. My config is:

monolog:
    handlers:
        main:
            type:         fingers_crossed
            action_level: critical
            handler:      grouped
        grouped:
            type:    group
            members: [streamed, buffered]
        streamed:
            type:  stream
            path:  "%kernel.logs_dir%/%kernel.environment%.log"
            level: debug
        buffered:
            type:    buffer
            handler: swift
        swift:
            type:       swift_mailer
            from_email: info@site.com
            to_email:   username@site.com
            subject:    An Error Occurred!
            level:      debug

当我尝试执行 php app /控制台测试:exception-command -e prod 会引发没有电子邮件发送的异常。

When I try to execute php app/console test:exception-command -e prod which throws exception no email sends.

推荐答案

如果您在swiftmailer配置中禁用了后台打印功能来发送电子邮件,这将导致立即发送而不是假脱机发送电子邮件,并且您的命令使用了记录器。

It should send emails with the spool disabled in your swiftmailer config, this should cause emails to be sent out immediately rather than be spooled and your command uses the logger.

如果要使用内存假脱机,可以在命令的执行方法中刷新队列。

If you want to use the memory spool you can flush the queue in your command's execute methods.

/**
 * force emails to be sent out at the end of execute
 */
protected function execute(InputInterface $input, OutputInterface $output)
{
    $container = $this->getContainer();
    $logger = $container->get('logger');
    $logger->critical('My Critical Error Message');

    $mailer = $container->get('mailer');
    $spool = $mailer->getTransport()->getSpool();
    $transport = $container->get('swiftmailer.transport.real');
    $spool->flushQueue($transport);
}

参考: http://symfony.com/doc/current/cookbook/console/sending_emails.html#using-memory-spooling

如果要记录未捕获的异常,则必须配置控制台事件以与记录器一起使用。

If you are wanting to log Uncaught Exceptions you will have to configure a console event to work with the logger.

http://symfony.com /doc/current/cookbook/console/logging.html#enabling-automatic-exceptions-logging

这篇关于Symfony2应用程序的控制台命令中发生错误时发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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