ThinkPHP5实现注册激活邮件
代码:http://download.csdn.net/detail/u012995856/9815802
邮件发送部分,参考:http://www.thinkphp.cn/topic/43753.html
效果:
思路:
- 注册成功后保存使用user_id 与reg_time合并并做的md5作为active_code激活码,置active激活状态为0
- 生成携带此激活码的链接发送至用户邮箱,
- 用户点击此连接后,通过各种验证,设置激活状态active为1
数据库字段:
目录:
1.在项目根目录使用composer下载phpmail
composer require phpmailer/phpmailer
2.在common.php中写下发送邮件的函数
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
// 应用公共文件
/**
* 系统邮件发送函数
* @param string $tomail 接收邮件者邮箱
* @param string $name 接收邮件者名称
* @param string $subject 邮件主题
* @param string $body 邮件内容
* @param string $attachment 附件列表
* @return boolean
* @author static7 <static7@qq.com>
*/
function send_mail($tomail, $name, $subject = '', $body = '', $attachment = null) {
$mail = new \PHPMailer(); //实例化PHPMailer对象
$mail->CharSet = 'UTF-8'; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码
$mail->IsSMTP(); // 设定使用SMTP服务
$mail->SMTPDebug = 0; // SMTP调试功能 0=关闭 1 = 错误和消息 2 = 消息
$mail->SMTPAuth = true; // 启用 SMTP 验证功能
$mail->SMTPSecure = 'ssl'; // 使用安全协议
$mail->Host = "smtp.sina.com"; // SMTP 服务器
$mail->Port = 465; // SMTP服务器的端口号
$mail->Username = "pangpython@sina.com"; // SMTP服务器用户名
$mail->Password = "密码"; // SMTP服务器密码
$mail->SetFrom('pangpython@sina.com', 'static7');
$replyEmail = ''; //留空则为发件人EMAIL
$replyName = ''; //回复名称(留空则为发件人名称)
$mail->AddReplyTo($replyEmail, $replyName);
$mail->Subject = $subject;
$mail->MsgHTML($body);
$mail->AddAddress($tomail, $name);
if (is_array($attachment)) { // 添加附件
foreach ($attachment as $file) {
is_file($file) && $mail->AddAttachment($file);
}
}
return $mail->Send() ? true : $mail->ErrorInfo;
}
注意:设置SMTP服务器,不要用QQ邮箱,接收邮件的邮箱也不要用QQ邮箱,会被拦截地址
3.database.php配置数据库参数
4.index\modelUser.php
<?php
namespace app\index\model;
use think\Model;
/**
*
*/
class User extends Model
{
}
5.app\index\Regist.php注册控制器
<?php
namespace app\index\controller;
use app\index\model\User;
/**
*
*/
class Regist
{
//注册
function index(){
//随机数设置user_id
$user_id = mt_rand();
//当前unix时间戳作为注册时间
$reg_time = time();
//注册默认设置没有激活账户
$active = 0;
//激活码为user_id与reg_time的md5串
$active_code = md5($user_id+$reg_time);
$user = new User;
$user->user_id = $user_id;
$user->reg_time = $reg_time;
$user->active = $active;
$user->active_code = $active_code;
if ($user->save()) {
//收件人邮箱
$toemail='target@163.com';
//发件人昵称
$name='static7';
//邮件标题
$subject='请激活您的账户';
//邮件内容
$content="<h1>恭喜你,注册成功。</h1><a href='http://localhost/public/index/active/active/activecode/".$active_code."'>点击激活</a>";
//$content = '';
//如果页面打印bool(true)则发送成功
if(send_mail($toemail,$name,$subject,$content)){
echo '注册成功!请检查邮箱激活邮件...';
}
}
}
}
6.激活控制器app\index\Active.php
<?php
namespace app\index\controller;
use think\request;
use app\index\model\User;
/**
*
*/
class Active
{
function active(){
$request = Request::instance();
//此加密串为user_id+reg_time的md5
//获取邮件请求中的激活加密串
$active_code = $request->param('activecode');
if (!($user = User::get(['active_code' => $active_code]))) {
echo "用户不存在!";
exit;
}else {
//检查用户激活时间,把用户表中注册字段与当前时间进行对比,超过30分钟即为过期
if (((time()-$user->reg_time)/60)>30) {
echo "激活邮件已经过期!";
}
//检查用户是否已经激活,用户表设置一个字段标识是1否0已经激活
if ($user->active == 1) {
echo "该用户已激活!";
exit;
}
//把此加密串对应的user的user_id与reg_time进行md5并与此加密串进行对比
if ($active_code == $user->active_code) {
$user->active = 1;
if ($user->save()) {
echo "激活成功!";
}
}
}
}
}