Yii2安装Yii2-User及配置SwiftMailer中的坑(Yii2基础版)

Yii2安装Yii2-User及配置SwiftMailer中的坑(Yii2基础版)

0.记录我在学习Yii2系列教程四:实现用户注册,验证,登录这个过程中遇到的坑。

上述文章以outlook邮箱为例子,这里我以163邮箱为例子。
注:提供个Blog,各邮箱smtp服务器及支持的协议
这个Blog只是让我们了解以下各邮箱smtp服务器及支持的协议,有些SMTP服务器地址已经更新了,具体地址需要去专门查看。

1.安装Yii2-User

这里我们的项目叫HelloYii,打开命令行,cd 到HelloYii目录下
按照Yii2-User的官方文档说明:
a.下载并安装Yii2-User:

composer require dektrium/yii2-user

b.稍微等待一下,安装完毕之后就可以进行对应的配置了,我们需要配置的文件是config/web.php,找到components,然后在与它同级的位置增加一个modules:

'components' => [
        // other settings...
    ],
'modules' => [
        'user' => [
            'class' => 'dektrium\user\Module',
            'confirmWithin' => 21600,
            'cost' => 12,
            'admins' => ['admin']
        ],
    ],

这里还需要将components下面的user部分注释掉,不然就会一直登录不了:

 /* 'user' => [
            'identityClass' => 'app\models\User',
            'enableAutoLogin' => true,
        ],*/

2.最后一步就是执行Yii2-User的migration了,在helloYii/目录下执行:

php yii migrate/up [email protected]/dektrium/yii2-user/migrations

or

./yii migrate/up [email protected]/dektrium/yii2-user/migrations

正常情况下,你会想那个Blog一样:
Yii2安装Yii2-User及配置SwiftMailer中的坑(Yii2基础版)

但是,这里我是记录的是坑,说说我遇到的第一个坑:

这个坑不是Blog坑,是自己搭建开发环境的坑:

php yii2提示Database Exception – yii\db\Exception错误
Yii2安装Yii2-User及配置SwiftMailer中的坑(Yii2基础版)

原因:

产生这个坑的原因是你安装时选的那个php,他的php.ini配置只是最基础的,并没有完全满足Yii2框架的需求。
访问:http://localhost:810/requirements.php
我修改php.ini后,可以看到比较满足的情况:
Yii2安装Yii2-User及配置SwiftMailer中的坑(Yii2基础版)
而我产生上面的坑的时候,我的配置不是这样的,是因为我没满足PDO MySQL extension这个选项。

解决方法:

windows下的php.ini的修改如下:
Yii2安装Yii2-User及配置SwiftMailer中的坑(Yii2基础版)
修改成上图后,基本上就是我的http://localhost:810/requirements.php页面返回一样了。
注意,这里的extension开启的不要重复,不然也会报错。
比如我在1965行开启了:
Yii2安装Yii2-User及配置SwiftMailer中的坑(Yii2基础版)
然后在925行也开启的话:
Yii2安装Yii2-User及配置SwiftMailer中的坑(Yii2基础版)
就会报如下错误:
PHP Core Warning ‘yii\base\ErrorException’ with message ‘Module ‘mbstring’ already loaded’Yii2安装Yii2-User及配置SwiftMailer中的坑(Yii2基础版)
解决!!!上传一个我的php.ini,附件。

3.配置SwiftMailer

安装完Yii2-User之后我们先不急着去想怎么实现登录和注册(其实很是比较简单的),我们之前说过的目标是实现用户在注册时候发送验证邮件的,这里我们先来配置一下我们的邮箱服务,因为Yii2-User可以直接使用邮箱来进行注册验证和密码找回等功能。在config/web.php找到mailer这个部分:

'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' => true,
],

修改成我们下面的这个样子:
注意,不同的邮箱,对应的配置不同,我这里用163
关于163邮箱的授权码:登录-》设置-》POP3/SMTP/IMAP-》按提示开启SMTP即可。

        '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' => true,

            'viewPath' => '@app/mailer',
            'useFileTransport' => false,//这个不能少,false发送邮件,true只是生成邮件在runtime文件夹下,不发邮件
            'transport' => [
                'class' => 'Swift_SmtpTransport',
                'host' => 'smtp.163.com',//每种邮箱的host配置不一样,看你的邮箱是什么
                'username' => '[email protected]',//你的邮箱
                'password' => '123456',//163邮箱填的是开起SMTP后的授权码,outlook邮箱是密码,其他邮箱应该是有授权码就授权码,没得就密码
                'port' => '25',
                'encryption' => 'tls',//参考我上面提供的Blog,具体加密协议和端口,需要看你邮箱平台是什么。
            ],
            'messageConfig'=>[
                'charset'=>'UTF-8',
                'from'=>['[email protected]'=>'admin']
            ],

然后,就可以通过下面提供的控制器的前三种方法去发邮件了。
但是,你会发现,注册的时候,你还是发不了邮件,而且会报错:
Swift_TransportException
Expected response code 250 but got code “553”, with message "553 Mail from must equal authorized user
Yii2安装Yii2-User及配置SwiftMailer中的坑(Yii2基础版)
这是因为有些邮件服务器要求from和username必须一致,163邮箱就是这样。
为避免这个问题,你还需要修改config/params.php:

<?php

return [
    //'adminEmail' => '[email protected]',
	//163
    'adminEmail'=>'[email protected]',//这里的邮箱需要config/web.php填的一样。
    //outllook
    //    'adminEmail'=>'[email protected]',
];

到这里,你就可以通过Yii2-User的注册功能发送邮件来注册用户啦!!!
注意:你发送的邮件,很可能被丢到接收邮件的垃圾箱里。

PS:
提供一个测试邮箱的代码:
controllers/MailController.php:

<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;

class MailController extends Controller
{
    public function actionMailer()
    {
        $mail= Yii::$app->mailer->compose();
        $mail->setTo('[email protected]');
        $mail->setSubject("Hello");
        $mail->setTextBody('Body11111111111111');
        //$mail->setHtmlBody("Test HTML");
        var_dump($mail->send()); //true是发送成功,false是失败,具体需要看目标邮箱到底收到信息没有。
    }

    public function actionMailer3()
    {
        $mail = Yii::$app->mailer;
        $mail->useFileTransport = false;
        $mail= $mail->compose();
        $mail->setTo('[email protected]');
        $mail->setSubject("title");
        $mail->setTextBody('content333333333333');
        var_dump($mail->send());
    }

    public function actionMailer4()
    {
        $mail= Yii::$app->mailer->compose();
        $mail->setTo('[email protected]');
        $mail->setSubject("邮件测试");
        $mail->setHtmlBody("<br>问我我我我我");    //发布可以带html标签的文本
        if($mail->send())
            echo "success";
        else
            echo "false";
        die();
    }
	//
	//以上三种方式,需要在config/web.php里配置好SwiftMailer,才可以使用。
	//
	//下面这种方法,不需要在config/web.php配置好SwiftMailer,也可以发送邮件。
    public function actionMailer2()
    {
        $mailer = new \yii\swiftmailer\Mailer();
        $mailer->transport=[
            'class' => 'Swift_SmtpTransport',
            'host' => 'smtp.163.com',
            'username' => '[email protected]',
            'password' => '123456',
            'port' => '25',
            'encryption' => 'tls',
            //下面的也可以用,是我测试过的,对应的端口配对应的协议
            //-------------------------------
            //'port' => '587',
            //'encryption' => 'ssl',
            //-------------------------------
            //'port' => '465',
            //'encryption' => 'ssl',
            //-------------------------------

        	//下面outlook邮箱的,可用
        	//'host' => 'smtp.office365.com',
        	//'username' => '[email protected]',
        	//'password' => '000000000',
        	//'port' => '587',
        	//'encryption' => 'tls',
        ];
        $mailer->messageConfig=[
            'charset'=>'UTF-8',
            'from'=>['[email protected]'=>'admin']
            //'from'=>['[email protected]'=>'admin']
        ];
        $mailer->useFileTransport = false;
        $mail= $mailer->compose();
        $mail->setTo('[email protected]');
        $mail->setSubject("Hello1");
        $mail->setTextBody('Happy');
        var_dump($mail->send());

//        $mail= Yii::$app->mailer->compose();
//        $mail->setTo('[email protected]');
//        $mail->setSubject("Hello");
//        $mail->setTextBody('Body11111111111111');
        //$mail->setHtmlBody("Test HTML");
//        var_dump($mail->send());
    }
}

其他报错:

1.
Swift_TransportException
Failed to authenticate on SMTP server with username "[email protected]" using 2 possible authenticators
Yii2安装Yii2-User及配置SwiftMailer中的坑(Yii2基础版)
如果是这个报错,可能是你的邮箱的账号或者密码(或授权码)填错了。

2.
Expected response code 220 but got code “”, with message “”
Yii2安装Yii2-User及配置SwiftMailer中的坑(Yii2基础版)
如果是这个报错,可能是你的端口填错了。

3.
Swift_TransportException
Connection could not be established with host smtp.163.com [ #0]
Yii2安装Yii2-User及配置SwiftMailer中的坑(Yii2基础版)

如果是这个报错,可能是你的加密的协议填错了。

4.
Swift_TransportException
Connection could not be established with host smtp.163.com [Unable to find the socket transport “starttls” - did you forget to enable it when you configured PHP? #-1534985344]
Yii2安装Yii2-User及配置SwiftMailer中的坑(Yii2基础版)
如果是这个报错,也可能是你的加密的协议填错了。

--------------------------------------------------------------------END---------------------------------------------------------

参考文章:
yii2邮件配置教程,报Expected response code 250 but got code "553"原因

yii2邮件配置教程,报Expected response code 250 but got code "553"原因

yii2 swift mailer 发送邮件不成功的问题

Yii2 邮件发送 [ 2.0 版本 ]

Yii 2.0的权威指南

Yii2 swiftmailer Expected response code 220 but got code “502”, with message "502 Error:

原:Yii2系列教程四:实现用户注册,验证,登录

Yii2系列教程四:实现用户注册,验证,登录