Yii 2:无法通过邮件发送日志

Yii 2: Unable to send log via mail

这是我的邮件组件。 如您所见,由于我的测试目的,我在文件中使用电子邮件

'mailer' => [
        'class'             => 'yii\swiftmailer\Mailer',
        'viewPath'          => '@common/mail',
        'useFileTransport'  => true,
    ],

这是我的日志组件。

'log'       => [
        'traceLevel' => YII_DEBUG ? 3 : 0,
        'targets'   => [
            [
                // for this target, see http://www.yiiframework.com/doc-2.0/guide-runtime-logging.html
                'class'         => 'yii\log\EmailTarget',
                'levels'        => ['error'],
                'categories'    => ['yii\db\*', 'email_test'],
                'message'       => [
                   'from'           => ESHOP_EMAIL,
                   'to'             => DEVELOPER_EMAIL,
                   'subject'        => 'Errore db [' . YII_ENV . ']',
                ],
            ],
        ],
    ],

在 siteControlle->actionIndex() 中,我正在以这种方式测试日志和电子邮件组件

public function actionIndex()
{
    Yii::error("testing mail log", "email_test");

    Yii::$app->mailer->compose()
        ->setFrom([ESHOP_EMAIL => ESHOP])
        ->setTo( DEVELOPER_EMAIL)
        ->setSubject("actionIndex executed on time " . date ("H:i:s"))
        ->setTextBody("Useless body")
        ->send();

    return $this->render('index');
}

如我所料,每次重新加载索引页面时,我都会在 frontend/runtime/mail 文件夹中创建 2 个 .eml 文件。

所以 SwiftMailer 正在运行,甚至是日志系统。

现在的问题

我尝试从 mailer 组件中删除对文件的使用,注释行

        'useFileTransport'  => true,

当我重新加载索引页面时,我收到了第二封邮件,这是手动撰写和发送的邮件,但我没有收到第一封邮件,应该由日志系统使用 SwiftMailer 自动撰写和发送的邮件。

怎么了?

如果您不使用 php 发送电子邮件的功能,您需要这样的传输方式

 'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'viewPath' => '@common/mail',
        //'useFileTransport' => true,
        'useFileTransport' => false, //set this property to false to send mails to real email addresses
        //comment the following array to send mail using php's mail function
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'smtp.gmail.com',
            'username' => 'your_mail@gmail.com',
            'password' => 'your_password',
            'port' => '587',
            'encryption' => 'tls',
        ],

如果您不使用 gmail,则需要以适当的方式配置参数。

我认为 'useFileTransport' 的默认值是 true。最好设置成false,不要评论

对不起大家。

问题是目标地址有误。使用记录器导致无法发送电子邮件的错误,但使用手动发送目标地址错误将被忽略并发送电子邮件。