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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端薛小帅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值