簡體   English   中英

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,

當我重新加載索引頁面時,我得到了SECOND MAIL,一個手動編寫並發送的郵件,但是我沒有收到第一封郵件,即應該由日志系統使用SwiftMailer自動編寫和發送的郵件。

怎么了?

對不起。

問題是目的地址錯誤。 使用記錄器無法發送錯誤導致的電子郵件,但使用手動發送目標地址錯誤將被忽略,並發送電子郵件。

我認為'useFileTransport'的默認值為true 最好設置為false ,不要評論它

如果你不使用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,則需要以正確的方式配置參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM