1. 配置SMTP服务,获取授权码(发送方邮箱是需要授权码的,接收方随意,也可以同时发送多个邮件)
  2. yii 日志信息发送到邮件里也可以使用这个swiftmail 发送。
  3. 先说发送邮件,swiftmail  是yii  安装的时候已经安装好的,如果没有可以通过composer安装。(在vendor/yiisoft/目录下看有没有yii2-swiftmailer)
    在component 数组里配置
    'mailer' => [
                'class' => 'yii\swiftmailer\Mailer',
                // send all mails to a file by default. You have to set
                // 'useFileTransport' to false and configure a transport
                // for the mailer to send real emails.
                'useFileTransport' => false,  // 设置true保存进文件,而不发送邮件
                'transport' => [
                    'class' => 'Swift_SmtpTransport',
                    'host' => 'smtp.qq.com',
                    'username' => '发送方的QQ邮箱',  // 接收方邮箱随意设置
                    'password' => '授权码',
                    'port' => '465',
                    'encryption' => 'ssl',
                ],
                'messageConfig' => [
                    'charset' => 'UTF-8',
                ],
            ],
    config 文件配置mail
    \Yii::$app->mailer->compose()
                ->setFrom('999888777@qq.com')  // 发送方是获取授权码的那个邮箱
                ->setTo('111222333@qq.com')   // 接收方随意
                ->setSubject('Message subject')
                ->setTextBody('Plain text content')
                ->setHtmlBody('<b>HTML content</b>')
                ->send();
    发送邮件简单用法,yii文档里有

     

  4. yii 日志发送到邮箱
    'log' => [
                'traceLevel' => YII_DEBUG ? 3 : 0,
                'targets' => [
                    [
                        'class' => 'yii\log\FileTarget',     // 保存进文件
                        'levels' => ['error', 'warning'],
                    ],
                    [
                        'class' => 'yii\log\EmailTarget',  // 发送邮件
                        'levels' => ['error'],   
    //                    'categories' => ['yii\db\*'],
                        'message' => [
                            'from' => ['999888777@qq.com'],  // 经过授权的
                            'to' => ['111222333@qq.com', '444555666@qq.com'], //可以发送多个邮件
                            'subject' => 'Database errors at example.com',
                        ],
                    ],
                ],
            ],
    log 信息发送到邮件