用TP框架写的邮箱验证

首先在公共函数库写一个发送邮件的函数:

function sendMail($to, $title, $content) {
    require 'class.phpmailer.php';
    $mail = new PHPMailer();
    $mail->IsSMTP(); // 启用SMTP
    $mail->Host=C('MAIL_HOST'); //smtp服务器的名称(这里以QQ邮箱为例)
    $mail->SMTPAuth = C('MAIL_SMTPAUTH'); //启用smtp认证
    $mail->Username = C('MAIL_USERNAME'); //发件人邮箱名
    $mail->Password = C('MAIL_PASSWORD') ; //163邮箱发件人授权密码
    $mail->From = C('MAIL_FROM'); //发件人地址(也就是你的邮箱地址)
    $mail->FromName = C('MAIL_FROMNAME'); //发件人姓名
    $mail->AddAddress($to,"尊敬的客户");
    $mail->WordWrap = 50; //设置每行字符长度
    $mail->IsHTML(C('MAIL_ISHTML')); // 是否HTML格式邮件
    $mail->CharSet=C('MAIL_CHARSET'); //设置邮件编码
    $mail->Subject =$title; //邮件主题
    $mail->Body = $content; //邮件内容
    $mail->AltBody = "这是一个纯文本的身体在非营利的HTML电子邮件客户端"; //邮件正文不支持HTML的备用显示
    return($mail->Send());
}

发送验证码:

 public function getEmailSign(){

        if(IS_AJAX){
            //接收账号信息
            $data['name'] = I('get.name');
            if(!$data['name']) $this->ajaxReturn("用户名不能为空",0);
            //接收邮箱
            $data['email'] = I('get.email');
            if(!$data['email']) $this->ajaxReturn("请填写邮箱账号",0);
            //验证邮箱格式
            $pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
            if(preg_match( $pattern, $data['email'])){
                //邮箱格式正确下,判断用户是否存在
                $where['user_name'] = I('get.name');
                $user = M('user')->where($where)->find();
                if($user){
                    //核对用户账号与邮箱是否一致
                    if($user['email'] == $data['email']){
                        //设置参数
                        $title = "邮箱验证";    //邮件标题
                        $font = "此邮件为修改密码验证邮件,请勿回复,您的验证码为"; //邮件内容
                        $code = rand(1000,9999);    //随机码
                        $content = $font.$code; //拼接
                        if(SendMail($data['email'],$title,$content)) {
                            //存验证码
                            session('email_code',$code);
                            session('email_date',time());
                            $this->ajaxReturn("发送成功,请在5分钟之内验证",0);
                        }else{
                            $this->ajaxReturn("发送失败",0);
                        }
                    }else{
                        $this->ajaxReturn("请核对用户信息的正确性",0);
                    }
                }else{
                    $this->ajaxReturn("请核对用户信息",0);
                }
            }else{
                $this->ajaxReturn("请核对邮箱信息",0);
            }
        }else{
            $this->ajaxReturn(0);
        }

    }

发送邮件(和上面一个意思):

public function bindEmail(){

        if(IS_AJAX){
            //接收账号信息
            $user_id = session('user_id');
            $name = I('get.name');
            $email = I('get.email');
            $data['name'] = $name;
            if(!$data['name']) $this->ajaxReturn("用户名不能为空",0);
            //接收邮箱
            $data['email'] = $email;
            if(!$data['email']) $this->ajaxReturn("请填写邮箱账号",0);
            //验证邮箱格式
            $pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
            if(preg_match( $pattern, $data['email'])){
                //邮箱格式正确下,判断用户是否存在
                $where['user_name'] = I('get.name');
                $user = M('user')->where($where)->find();
                if($user){
                    //核对用户账号与邮箱是否一致
                    if($user['email'] == $data['email']){
                        //设置参数
                        $title = "绑定邮箱";    //邮件标题
                        $date = time();
                        $key = 'yushi';
                        $info = substr(MD5($key),0,6);
                        $message=<<<str
你好!{$name}
<h2>欢迎注册本网站</h2>
绑定邮箱请点击如下地址:<br/><br/>
<a href="http://127.0.0.1/hyzx/index.php/Home/Login/bindEmail/date/{$date}/info/{$info}/i/{$user_id}/email/{$email}">
http://127.0.0.1/hyzx/index.php/Home/Login/bindEmail/date/{$date}/info/{$info}/i/{$user_id}/email/{$email}</a>
<br/><br/>
str;
                        if(SendMail($data['email'],$title,$message)) {
                            $this->ajaxReturn("发送成功,请去您的邮箱查看",0);
                        }else{
                            $this->ajaxReturn("发送失败",0);
                        }
                    }else{
                        $this->ajaxReturn("请核对用户信息的正确性",0);
                    }
                }else{
                    $this->ajaxReturn("请核对用户信息",0);
                }
            }else{
                $this->ajaxReturn("请核对邮箱信息",0);
            }
        }else{

            $user_id = session('user_id');
            $data['user_id'] = $user_id;
            $user = M('user')->where($data)->find();
            $this->assign('user',$user);
            $this->display();

        }

    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值