您好,欢迎访问一九零五行业门户网

利用yii 2框架发送电子邮件

利用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服务
推荐教程:yii框架
以上就是利用yii 2框架发送电子邮件的详细内容。
其它类似信息

推荐信息