yii2 发送邮件 yii\swiftmailer\Mailer

时间:2022-06-01 21:03:05

Yii2 中发送邮件 yii\swiftmailer\Mailer

        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'viewPath' => '@common/mail',
            'useFileTransport' => false,
            'transport' => [
                'class' => 'Swift_SmtpTransport',
                'host' => 'smtpdm.aliyun.com',
                'username' => '用户名',
                'password' => '密码',
                'port' => '端口',//阿里云的是80,宝塔中这里一定要有端口,阿里云是80
                'encryption' => 'tls',
            ],
            'messageConfig'=>[
                'charset'=>'UTF-8',
                'from'=>['***@qq.com'=>'dafei']
            ],
        ],

php 中调用

        Yii::$app->mailer->compose()
            ->setFrom(Yii::$app->get('mailer')->getTransport()->getUsername())//获取 mailer-->transport-->username , 即上面的用户名
            // ->setBcc(['123'=>'abc'])
            ->setTo('发送给谁@qq.com')
            ->setSubject('subject')
            ->setTextBody('hello world textBody')
            ->send();
        exit('发送邮件ok');