YII2 高级版本 发送163邮件

这篇博客介绍了如何在YII2高级版本中配置并使用yii2-swiftmailer扩展来发送163邮件。首先,需要在main-local.php配置文件中设置SMTP参数,包括主机、端口、用户名、密码和加密方式。然后,在控制器中编写发送邮件的代码。遇到'Expected response code 250 but got code 554'错误时,可以通过注释或删除setFrom()函数来解决。最后,别忘了在163邮箱中开启SMTP服务并获取授权密码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

YII2  高级版本  发送163邮件

1 配置yii2-swiftmailer 扩展

编辑common/config/main-local.php

        'mailer' => [
            'class'            => 'yii\swiftmailer\Mailer',
            'viewPath'         => '@common/mail',
            // 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的话,会在runtime/mail创建一个邮件文件,用于测试;false的话实时发送;

            'transport' => [
                'class' => 'Swift_SmtpTransport', //使用的类
                'host' => 'smtp.163.com', //邮箱服务一地址
                'port' => '25',  //服务器端口
                'username' => 'xxxxxxx@163.com',//你的邮箱地址
                'password' => 'xxxxxxxxxxxxx',  //自己填写邮箱密码,开通SMTP时的授权密码
                'encryption' => 'tls', //加密方式
            ],
            'messageConfig'=>[
                'charset'=>'UTF-8',
                'from'=>['xxxxxxx@163.com'=>'xxxxxxxx']
            ],
        ],

2 在控制器中编写发送程序

$res = Yii::$app->mailer->compose()
            //->setFrom('xxxxx.163.com') //注1
            ->setTo('1273567815@qq.com')
            ->setSubject('测试邮件')
            ->setTextBody('测试邮件')
            ->setHtmlBody('测试邮件')
            ->send();

注1:添加setFrom()函数时,如果是使用163邮箱发送的话,会产生Expected response code 250 but got code "554" 的错误。解决方法:可以将setFrom()函数一行注释或删除。

3 开通163邮箱的SMTP

POP/SMTP服务 显示 已关闭|开启  时,点击 开启 ,然后得到授权密码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值