Yii2 发送邮件配置

243 阅读1分钟
  • 控制器
// 禁用layout
\Yii::$app->mailer->htmlLayout = false;

$res = \Yii::$app->mailer->compose('/mail/mail', [
        'title' => 'hello'
    ])->setFrom('test1@test.com')
    ->setTo('test2@test.com')
    ->setSubject('测试邮件')
    ->send();

var_dump($res);
  • 配置文件:
'mailer' => [
    'class' => 'yii\swiftmailer\Mailer',
    //'viewPath' => 'mail',
    //'useFileTransport' => false,
    'transport' => [
        'class' => 'Swift_SmtpTransport',
        'host' => 'smtp.qq.com',
        // 邮箱账号
        'username' => 'test1@qq.com',
        // 邮箱密码,QQ邮箱是“授权码”
        'password' => 'xxxxxxxxxx',
        'port' => '465', 
        'encryption' => 'ssl',
    ],
    'messageConfig'=>[
        // 信息字符集
        'charset'=>'UTF-8',
        // 设置发件人
        'from'=>['test1@test.com'=>'display name']
    ],
],