 利用yii 2框架发送电子邮件,具体步骤如下所示: 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、视图文件 视图文件的输出就是邮件的内容 <?php
$resetLink = Yii::$app->urlManager->createAbsoluteUrl(['site/reset-password', 'token' => $token]);
?>
<div>
<h5>密码重置服务</h5>
<a href="<?=$resetLink?>">点击重置密码</a>
</div> 4、访问 http://127.0.0.1/base/web/index.php?r=site/send-mail 出现 success则发送成功,若未收到确认邮箱已开启pop3服务  |