PHP实现邮箱验证码的发送

PHP发送邮件功能要使用mailer函数包,我们需要进行下载

mailer函数包下载链接

下载后,需要将Exception.php、PHPMailer.php及SMTP.php三个文件放在你的项目目录下。

同时,我们还要去对应邮箱的客户端去配置邮箱服务。这里我以126邮箱为例。

再在侧导航栏的POP3/SMTP/IMAP中开启服务即可 。

要保存好SMTP码!!!

下面是我们的代码部分:

$email = $_GET["email"];
$to = $email;    //从GET参数中获取用户提交的邮箱地址赋值给$to

function sendMail($to, $subject, $content): bool
{
  $mail = new PHPMailer(true);    // 创建一个新的PHPMailer实例

    try {
        $mail->isSMTP();    // 设置邮件发送方式为SMTP
        $mail->Host = 'smtp.163.com';    // 设置SMTP服务器地址
        //(QQ邮箱:smtp.qq.com,谷歌:smtp.gmail.com)

        $mail->SMTPAuth = true;    //启用SMTP身份验证

        $mail->Username = '填写你的SMTP用户名'
        $mail->Password = '填写SMTP密码';

        $mail->SMTPSecure = 'ssl';  //加密方法
        $mail->Port = 465;    //服务端口,不同品牌邮箱服务端口可百度查看
        $mail->CharSet = 'UTF-8';    //设置邮件字符编码    

        // 邮件内容设置
        $mail->setFrom('填写发送消息的邮箱号', '填写发送信息的用户名');    //发件人信息
        $mail->addAddress($to);     // 收件人邮箱地址
        $mail->isHTML(true);    //设置邮件内容为HTML格式
        $mail->Subject = $subject;    //设置邮件主题
        $mail->Body = $content;       //设置邮件正文内容

        // 发送邮件
        $mail->send();
        return true;
    } catch (Exception $e) {
        return false;
    }
  }
}

// 生成6位随机数作为验证码
$code = rand(100000, 999999);
$expireTime = time() + 180;
$encryptedCode = encrypt($code);

//加密验证码
function encrypt($data): string
{
    $key = '填写密钥';
    return base64_encode(openssl_encrypt($data, 'AES-128-ECB', $key, OPENSSL_RAW_DATA));
    //使用AES-128-ECB算法对数据进行加密,并对加密后的结果进行Base64编码,将加密结果转换为字符串
}

$last_send_time = $_SESSION['last_send_time'] ?? 0;    // 获取上一次发送邮件的时间戳,默认为0

// 判断是否距离上一次发送邮件的时间小于180秒(3分钟),且上一次发送邮件的收件人与当前收件人相同
if (time() - $last_send_time < 180 && isset($_SESSION['last_send_to']) && $_SESSION['last_send_to'] == $to) {
    echo '<script>alert("获取频繁,稍后重试");</script>';    //如果是,则弹窗提醒
    exit;
}
// 发送邮件

$subject = '填写邮件主题';    
$content = '填写邮件内容:'     //如果需要发送验证码,可直接引用变量$code
if (sendMail($to, $subject, $content)) {
    echo '<script>alert("验证码已发送,请注意查收");</script>';
    $_SESSION['last_send_time'] = time();    // 更新上一次发送邮件的时间为当前时间戳
    $_SESSION['last_send_to'] = $to;    // 更新上一次发送邮件的收件人为当前收件人
} else {
    echo '邮件发送失败';
}

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,下面是一个使用 thinkphp6 发送邮件验证码的示例: 1. 首先,需要在 `config/mail.php` 文件中进行邮箱配置: ```php return [ // 默认发送邮件设置 'default' => [ // 邮件服务器地址 'host' => 'smtp.163.com', // 邮件服务器端口 'port' => 465, // 发件人邮箱地址 'username' => 'your_email@example.com', // 邮箱授权码,非邮箱登录密码 'password' => 'your_email_password', // 邮箱加密方式,ssl 或 tls 'secure' => 'ssl', // 默认发件人 'from' => [ 'address' => 'your_email@example.com', '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` 是一个自定义的函数,用于验证邮箱地址的合法性。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

黑猫琴师

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

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

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

打赏作者

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

抵扣说明:

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

余额充值