1、config/web.php中开启邮箱配置
'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', //每种邮箱的host配置不一样
'username' => 'xxxxx@qq.com',//改成自己的邮箱
'password' => 'xxxxxxxx',//改成自己的邮箱token
'port' => '465',
'encryption' => 'ssl',
],
'messageConfig'=>[
'charset'=>'UTF-8',
'from'=>['xxxxx@qq.com'=>'YiiAdmin']//邮件显示名称
],
],
2、SiteController.php控制器文件添加
public function actionSendMail(){
$mail= Yii::$app->mailer->compose('reset-password',['token'=>'xxxxxx']);
// 渲染一个视图作为邮件模板 文件路径mail/reset-password.php,注意,不在view中
$mail->setTo('xxxxx@hotmail.com');//要发送到的邮箱地址
$mail->setSubject("邮件测试【重置密码】");//邮件标题
if($mail->send())
echo "success";
else
echo "failse";
die();
}
3、视图文件
$resetLink = Yii::$app->urlManager->createAbsoluteUrl(['site/reset-password', 'token' => $token]);
?>