Yii2邮件捕手

时间:2017-10-31 08:51:00

标签: yii2 swiftmailer mailer

在开发和测试期间,我希望Yii2邮件程序(swiftmailer)不要将电子邮件发送到实际地址,而是用开发人员的电子邮件替换它。在配置中有任何设置吗?

2 个答案:

答案 0 :(得分:0)

你应该为prod和dev环境设置配置 dev配置的路径(如果您有高级应用程序模板):yourProject/environments/dev/common/config/main-local.php和prod:yourProject/environments/prod/common/config/main-local.php.

You may use EmailTarget class for logs. dev环境的配置示例以获得线索:

return [
'bootstrap' => ['log'],
'components' => [
    ...
    'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'viewPath' => '@common/mail',
        'useFileTransport' => true,//set this property to false to send mails to real email addresses
        //comment the following array to send mail using php's mail function
    ],
    'log' => [
        'targets' =>
            [
                [
                    'class' => 'yii\log\EmailTarget',
                    'levels' => ['error'],
                    'except' => ['yii\web\HttpException:404'],
                    'message' => [
                        'to' => ['example@mail.ru'],
                        'from' => ['yourproject@mail.ru'],
                        'subject' => ' Errors ',
                    ]
                ],
            ],
    ],
],

];

类似于prod环境,但有不同的电子邮件。

如果你不想使用EmailTarget类,你可以在这里设置dev的params配置电子邮件:yourProject/environments/dev/common/config/params-local.php. 和prod的路径:yourProject/environments/prod/common/config/params-local.php

Params配置示例:

return [
'sendToEmails' => ['email1@mail.ru', 'email2@mail.ru']
];

然后您可以这样使用代码中的变量:Yii::$app->params['sendToEmails']来获取要发送消息的电子邮件数组。

在完成配置后,不要忘记在项目中执行php init命令。

You may see the detailed docs about environments here

答案 1 :(得分:-1)

只需在开发环境的组件配置中将useFileTransport设置为true,所有电子邮件都不会被发送,但会保存为文件,以便您轻松测试所有内容。