PHPMailer实现简单邮箱验证码

00PHPMailer

PHPMailer是一个非常强大的 php发送邮件类,可以设定发送邮件地址、回复地址、邮件主题、html网页,上传附件,并且使用起来非常方便。

phpMailer 的特点:

1、在邮件中包含多个 TO、CC、BCC 和 REPLY-TO。
2、平台应用广泛,支持的 SMTP 服务器包括 Sendmail、qmail、Postfix、Gmail、Imail、Exchange 等等。
3、支持嵌入图像,附件,HTML 邮件。
4、可靠的强大的调试功能。
5、支持 SMTP 认证。
6、自定义邮件头。
7、支持 8bit、base64、binary 和 quoted-printable 编码。

01准备
phpmailer 安装或者下载方式:

1、从 github 上下载: https://github.com/PHPMailer/PHPMailer/

2、使用 composer 安装:

composer require phpmailer/phpmailer

我直接从github下载了.zip

邮箱配置

以qq邮箱为例,进入qq邮箱,依次找到首页–>账户–>POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务,开启前两个服务,要记录对应的授权码,这个邮箱就是用来发送验证码的邮箱(发件人)。

02发送邮件
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//根据自己的路径引入
require './PHPMailer-master/PHPMailer-master/src/Exception.php';
require './PHPMailer-master/PHPMailer-master/src/PHPMailer.php';
require './PHPMailer-master/PHPMailer-master/src/SMTP.php';

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //服务器配置
    $mail->CharSet ="UTF-8";                     //设定邮件编码
    $mail->SMTPDebug = 0;                        // 调试模式输出
    $mail->isSMTP();                             // 使用SMTP
    $mail->Host = 'smtp.qq.com';                // SMTP服务器
    $mail->SMTPAuth = true;                      // 允许 SMTP 认证
    $mail->Username = 'xxxxx@qq.com';                // SMTP 用户名  即邮箱的用户名
    $mail->Password = 'xxxxxxxxx';             // SMTP 密码  部分邮箱是授权码(例如163邮箱)
    $mail->SMTPSecure = 'tls';                    // 允许 TLS 或者ssl协议
    $mail->Port = 587;                            // 服务器端口 25 或者465 具体要看邮箱服务器支持

    $mail->setFrom('xxxxx@qq.com', '发件人');  //发件人
    $mail->addAddress('yyyyy@qq.com', '收件人');  // 收件人
    //$mail->addAddress('ellen@example.com');  // 可添加多个收件人
    $mail->addReplyTo('xxxxx@qq.com', 'info'); //回复的时候回复给哪个邮箱 建议和发件人一致
    //$mail->addCC('cc@example.com');                    //抄送
    //$mail->addBCC('bcc@example.com');                    //密送

    //发送附件
    // $mail->addAttachment('../xy.zip');         // 添加附件
    // $mail->addAttachment('../thumb-1.jpg', 'new.jpg');    // 发送附件并且重命名

    //Content
    $mail->isHTML(true);                                  // 是否以HTML文档格式发送  发送后客户端可直接显示对应HTML内容
    $mail->Subject = '这里是邮件标题' . time();
    $mail->Body    = '<h1>这里是邮件内容</h1>' . date('Y-m-d H:i:s');
    $mail->AltBody = '如果邮件客户端不支持HTML则显示此内容';

    $mail->send();
    echo '邮件发送成功';
} catch (Exception $e) {
    echo '邮件发送失败: ', $mail->ErrorInfo;
}
03实现简单的邮箱验证码注册

思路
1、注册的过程就是输入邮箱、密码,之后获取验证码,之后注册
2、要保证验证码在一定的时间内有效(要给验证码一个时间戳,以下代码并未实现,但是验证码的时间戳已经有了,可以直接查询最新的或者把用过的验证码删掉)
3、还要记住验证码是发给谁的,必需是对应的验证码才能注册成功
4、验证码先简单使用六位随机数
5、注册是向用户表插入数据,以下代码并未实现,只要在验证码验证成功的条件里写上插入操作就可以了

<!--register.php-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
</head>

<body>
    <div>
        <form action="../server/server_register.php" method="get">
            邮 箱<input type="email" name="email" id="email">

            密 码<input type="password" name="password" id="password"></br>
            验证码<input type="text" name="verificationcode" id="verificationcode"></br>

            <button>立刻注册</button>
        </form>
        <button onclick="getVerificationCode()">获取验证码</button>
    </div>

    <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.6.0.min.js"></script>
    <script>
        function getVerificationCode() 
        {
            Email = $('#email').val();
            //console.log(Email);
            $.ajax({
                //ajax请求路径,根据自己的写
                url: "http://localhost/allPHPcode/phpmailer/emailVerificationCode/server/createVerificationCode.php",
                type: 'get',
                data: { email:Email},
                success: function() {
                    alert("获取验证码成功!");
                }
            })
        }
    </script>
</body>

</html>
<?php
//  createVerificationCode.php



require './DB.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;


require '../../PHPMailer-master/PHPMailer-master/src/Exception.php';
require '../../PHPMailer-master/PHPMailer-master/src/PHPMailer.php';
require '../../PHPMailer-master/PHPMailer-master/src/SMTP.php';


$email = $_GET['email'];
// echo $email;
$time = time() + 8 * 3600;//时间戳,为后续验证码一分钟内有效而添加
$verficationCode = rand(100000, 999999);//六位验证码是100000-999999之间的随机数

$sql = "insert into emailverificationCode(id, username, verificationCode, createtime) values ('null', '$email', '$verficationCode' ,'$time')";
//echo $sql;
$count = DB::getInstance()->connect()->exec($sql);

function sendmail($email,$verficationCode)
{
    
$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //服务器配置
    $mail->CharSet ="UTF-8";                     //设定邮件编码
    $mail->SMTPDebug = 0;                        // 调试模式输出
    $mail->isSMTP();                             // 使用SMTP
    $mail->Host = 'smtp.qq.com';                // SMTP服务器
    $mail->SMTPAuth = true;                      // 允许 SMTP 认证
    $mail->Username = 'xxxxx@qq.com';                // SMTP 用户名  即邮箱的用户名
    $mail->Password = 'xxxxxxxxxxxxx';             // SMTP 密码  部分邮箱是授权码(例如163邮箱)
    $mail->SMTPSecure = 'tls';                    // 允许 TLS 或者ssl协议
    $mail->Port = 587;                            // 服务器端口 25 或者465 具体要看邮箱服务器支持

    $mail->setFrom('xxxxx@qq.com', 'Mailer');  //发件人
    $mail->addAddress($email, 'Joe');  // 收件人
    //$mail->addAddress('ellen@example.com');  // 可添加多个收件人
    $mail->addReplyTo('anngreen@qq.com', 'info'); //回复的时候回复给哪个邮箱 建议和发件人一致
    //$mail->addCC('cc@example.com');                    //抄送
    //$mail->addBCC('bcc@example.com');                    //密送

    //发送附件
    // $mail->addAttachment('../xy.zip');         // 添加附件
    // $mail->addAttachment('../thumb-1.jpg', 'new.jpg');    // 发送附件并且重命名

    //Content
    $mail->isHTML(true);                                  // 是否以HTML文档格式发送  发送后客户端可直接显示对应HTML内容
    $mail->Subject = '这里是邮件标题' . time();
    $mail->Body    = '<h1>验证码是</h1>' . $verficationCode."<br>" .date('Y-m-d H:i:s');
    $mail->AltBody = '如果邮件客户端不支持HTML则显示此内容';

    $mail->send();
    echo '邮件发送成功';
} catch (Exception $e) {
    echo '邮件发送失败: ', $mail->ErrorInfo;
}
}


if ($count > 0) {
    sendmail($email,$verficationCode);
    echo "<script>alert('获取成功!');</script>";
    //echo "<script>alert('添加成功!');</script>";
} else {
    echo "<script>alert('获取失败!');</script>";
}

<?php
//server_register.php

require 'DB.php';
$email = $_REQUEST['email'];
$password = $_REQUEST['password'];
$verificationcode = $_REQUEST['verificationcode'];

$sqlselectverificationcode = "select * from emailverificationCode where username = '$email' and $verificationcode = '$verificationcode'";
$stmt = DB::getInstance()->connect()->query($sqlselectverificationcode);
if(is_null($stmt)) 
{
    echo "<script>alert('注册成功!');</script>";
}else{
    echo "<script>alert('注册失败!');</script>";
}

<?php
//DB.php
/*
 * @Description: 数据库连接类
 */

class DB
{
  // 定义私有的单例对象
  static private $_instance;
  // 定义私有的连接对象
  private $_pdo;
  // 定义私有的配置信息对象
  private $config = [
    'dsn' => 'mysql:host=localhost;dbname=test;port=3306;charset=utf8',
    'username' => 'root',
    'password' => '',
    'option' => [
      PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // 默认是 :PDO::ERRMODE_SILENT,
      PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    ]
  ];

  // 私有化构造函数
  private function __construct() {}

  /**
   * @description: 获取单例对象 $_instance
   * @param {*}
   * @return {*}
   * @Author: Humbert Cheung
   */
  static public function getInstance() {
    // 判断 $_instance 是否是DB类的实例,即判断 $_instance 是否不为空
    if(!self::$_instance instanceof self) {
      // 为空,则实例化
      self::$_instance = new self();
    }
    return self::$_instance;
  }
  public function connect() {
    // 判断 $_pdo 是否存在
    if(!$this->_pdo) {
      try {
        // 不存在则实例化
        $this->_pdo = new PDO($this->config['dsn'], $this->config['username'], $this->config['password'], $this->config['option']);
      } catch (PDOException $e) {
        die('数据库连接失败:' . $e->getMessage());
      }
    }
    return $this->_pdo;
  }
}

test数据库下emailverficationcode只有id(自增主键)、username、verificationCode、createtime

参考1
参考2

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个使用 thinkphp6 发送邮件验证码的示例: 1. 首先,需要在 `config/mail.php` 文件中进行邮箱配置: ```php return [ // 默认发送邮件设置 'default' => [ // 邮件服务器地址 'host' => 'smtp.163.com', // 邮件服务器端口 'port' => 465, // 发件人邮箱地址 'username' => '[email protected]', // 邮箱授权码,非邮箱登录密码 'password' => 'your_email_password', // 邮箱加密方式,ssl 或 tls 'secure' => 'ssl', // 默认发件人 'from' => [ 'address' => '[email protected]', 'name' => 'your_name', ], ], ]; ``` 2. 然后,创建一个 `MailService` 类,用于发送邮件: ```php <?php namespace app\service; use think\facade\Cache; use think\facade\Config; use think\facade\View; use think\facade\Lang; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; class MailService { /** * 发送邮件验证码 * * @param string $email 邮箱地址 * @param string $type 验证码类型,例如 register、forget * @return boolean */ public function sendVerifyCode($email, $type) { // 生成验证码 $code = mt_rand(100000, 999999); // 邮件主题和内容 $subject = Lang::get('mail.' . $type . '_subject'); $body = View::fetch('mail/' . $type . '_body', ['code' => $code]); // 实例化 PHPMailer 对象 $mail = new PHPMailer(true); try { // 配置 SMTP 服务器 $mail->SMTPDebug = 0; // 调试:0 关闭,1 开启 $mail->isSMTP(); $mail->Host = Config::get('mail.default.host'); $mail->SMTPAuth = true; $mail->Username = Config::get('mail.default.username'); $mail->Password = Config::get('mail.default.password'); $mail->SMTPSecure = Config::get('mail.default.secure'); $mail->Port = Config::get('mail.default.port'); // 设置发件人、收件人、邮件主题、内容 $mail->setFrom(Config::get('mail.default.from.address'), Config::get('mail.default.from.name')); $mail->addAddress($email); $mail->Subject = $subject; $mail->Body = $body; // 发送邮件 $mail->send(); // 将验证码存入缓存,有效期为 5 分钟 Cache::set('verify_code:' . $email, $code, 300); return true; } catch (Exception $e) { return false; } } } ``` 3. 最后,在控制器中调用 `MailService` 类的 `sendVerifyCode` 方法即可: ```php <?php namespace app\controller; use app\service\MailService; use think\facade\Request; class UserController { /** * 发送注册验证码 * * @return json */ public function sendRegisterVerifyCode() { $email = Request::post('email'); if (!validate_email($email)) { return json(['code' => -1, 'msg' => '邮箱地址不正确']); } $mailService = new MailService(); if ($mailService->sendVerifyCode($email, 'register')) { return json(['code' => 0, 'msg' => '验证码已发送']); } else { return json(['code' => -1, 'msg' => '验证码发送失败']); } } } ``` 其中,`validate_email` 是一个自定义的函数,用于验证邮箱地址的合法性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值