Thinkphp5.1实现发送邮箱验证码

这里使用的是 phpmailer/phpmailer 这个类

第一步加载类

 composer require phpmailer/phpmailer

第二步编写公共方法

/**
 * 邮箱验证码
 * @param string $to 发送到邮箱
 * @param string $name 当前邮箱服务器 
 * @return string $subject 发送标题
 * @return string $body 发送内容
 * @return string $attachment 附件
 */
function send_mail($to, $name, $subject = '', $body = '',$attachment = null) {
    $mail = new \PHPMailer\PHPMailer\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.163.com";       // SMTP 服务器
    $mail->Port = 465;                  // SMTP服务器的端口号
    $mail->Username = '你的邮箱@163.com';    // SMTP服务器用户名
    $mail->Password = '你的邮箱密码';     // SMTP服务器密码//这里的密码可以是邮箱登录密码也可以是SMTP服务器密码
    $mail->SetFrom('发件邮箱', 'xxx有限公司');
    $replyEmail = '';                   //留空则为发件人EMAIL
    $replyName = '';                    //回复名称(留空则为发件人名称)
    $mail->AddReplyTo($replyEmail, $replyName);
    $mail->Subject = $subject;
    $mail->MsgHTML($body);
    $mail->AddAddress($to, $name);
    if (is_array($attachment)) { // 添加附件
        foreach ($attachment as $file) {
            is_file($file) && $mail->AddAttachment($file);
        }
    }
    return $mail->Send() ? true : $mail->ErrorInfo;
}

第三步控制器中使用

<?php

namespace app\api\controller;

class Index extends Base {

    /**
     * @description 发送邮箱验证码
     * @api /api/index/getEmailCode
     */
    public function getEmailCode(){
        $subject='xxx有限公司,发送验证码';
        $code = mt_rand(100000,999999);
        $body='您的验证码是:'.$code;
        $to=$this->request->post('email');
        $name="xxx有限公司";
        $r=send_mail($to,$name,$subject,$body,$attachment = null);

        if($r){
            session('emailCode',$code);//记录邮件验证码
            session('emailCodeStartTime',time());//记录验证码发送时间
            $this -> apiSuccess('验证码已发送,有效期60秒');
        }else{
            $this -> apiError('验证码发送失败');
        }
    }

}

这里以邮箱、验证码、密码注册为例,主要实现了验证码60秒过期,注册成功验证码失效功能

以下是用户表的模型方法

<?php
namespace app\admin\model;

use think\Model;

class User extends Model {

    // 注册
    public function register($email,$code,$password){
        $user = $this->where('email',$email)->find();
        if($user) return reMsg(0,'用户已存在');

        // 设置验证码有效期60秒,过期清空
        if(time() > session('emailCodeStartTime') + 60){
            session('emailCode',null);
            return reMsg(0,'验证码过期');//reMsg是自己封装的方法,用于反馈数据库的操作结果
        }

        if($code != session('emailCode')) return reMsg(0,'验证码过期');

        $this->save([
            'email'     => $email,
            'password'  => md5($password)
        ]);
        // 当验证码成功使用,清空验证码
        session('emailCode',null);
        return reMsg(1,'注册成功');
    }

}

欢迎加入PHP学习交流群901759097

猜你喜欢

转载自blog.csdn.net/qq_41980461/article/details/119105733
今日推荐