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

时间:2015-05-15 11:15:58

标签: php email yii2 swiftmailer

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

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

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

所以SwiftMailer正在工作,甚至是日志系统。

现在问题

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

        'useFileTransport'  => true,

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

出了什么问题?

3 个答案:

答案 0 :(得分:2)

对所有人抱歉。

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

如果您不使用php函数发送电子邮件,则需要像这样的传输

    ref = Sheets("Ref").Cells(1, 1)

    cres = ref + 5

    lformula = 2
    cformula = ref + 2
    cformulacomp = ref + 3

    For cref = 1 To ref
    Dim lastrow As Long
    lastrow = Cells(Rows.Count, cref).End(xlUp).Row
    For ccomp = cref + 1 To ref

    For lref = 2 To lastrow 'substituir por 5 para testes / lastrow para solução final

    For lcomp = 2 To lastrow 'substituir por 5 para testes / lastrow para solução final

    If ActiveSheet.Cells(1, cres) = "" Then
    ActiveSheet.Cells(1, cres).Value = cref & " x " & ccomp
    lformula = lformula + 1
    End If

    If Cells(lref, cref) = Cells(lcomp, ccomp) Then
    Cells(lref, cres) = "OK"
    End If

    Next lcomp


    Next lref

    If ActiveSheet.Cells(lformula, cformula) = "" Then
    ActiveSheet.Cells(lformula, cformula).Value = cref & " x " & ccomp
    Cells(lformula, cformula + 1).FormulaR1C1 = _
        "=IF(COUNTA(R2C" & cref & ":R1200C" & cref & ")=COUNTIF(R2C" & cres & ":R1200C" & cres & ",""OK""),""Iguais"",""Diferentes"")" 'Trocar R5C por R1200C

    End If

    cres = cres + 1

    Next ccomp


    While Cells(linhaverif, cformula + 1).Value = "Iguais"
    cref = cref + 1
    linhaverif = linhaverif + 1
    Wend

     Next cref

    Application.CutCopyMode = False
'
'Apagar botões e pintar células
'
    Dim botoes As Integer

    On Error Resume Next
        For botoes = 1 To 200
            ActiveWorkbook.Sheets(botoes).Buttons.Delete
        Next botoes

        Range(Columns(ref + 5), Selection.End(xlToRight)).Select
    Selection.SpecialCells(xlCellTypeBlanks).Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Range("A1").Select
End Sub

如果你不使用gmail,你需要以正确的方式配置参数。