Email.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use think\facade\Db;
  5. use PHPMailer\PHPMailer\PHPMailer;
  6. use PHPMailer\PHPMailer\Exception;
  7. //邮件控制器
  8. class Email extends BaseController
  9. {
  10. protected $noNeedLogin = ['sendVerifyEmail'];
  11. // 发送邮箱验证码
  12. public function sendVerifyEmail() {
  13. $email = $this->request->post('email'); // 邮箱地址
  14. $event = $this->request->post('event' ?? 'register'); // 验证码事件
  15. if (!$email || !$event) {
  16. $this->fail(500, '请检查传参!');
  17. }
  18. // 随机验证码
  19. $code = mt_rand(1000, 9999);
  20. // 插入数据库
  21. $row = Db::table('tb_email_captcha')->insert([
  22. 'email' => $email,
  23. 'code' => $code,
  24. 'event' => $event,
  25. 'ip' => $this->request->ip(),
  26. 'createtime' => time(),
  27. ]);
  28. if (!$row) {
  29. $this->fail(500, '发送失败!请联系管理员处理!');
  30. }
  31. $mail = new PHPMailer(true);
  32. $mail->isSMTP();
  33. $mail->Host = 'smtp.yeah.net'; // 设置 SMTP 服务器
  34. $mail->SMTPAuth = true; // 启用 SMTP 认证
  35. $mail->Username = 'danjiwanjia@yeah.net'; // 用户名
  36. $mail->Password = 'UMt6A8pm2KY2unCM'; // 密码
  37. $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // SMTP 协议类型
  38. $mail->Port = 25; // SMTP 端口号
  39. $mail->setFrom('danjiwanjia@yeah.net', '单机顽家');
  40. $mail->addAddress($email); // 添加收件人
  41. $mail->isHTML(true); // Set email format to HTML
  42. $mail->Subject = '单机顽家验证码';
  43. $mail->Body = '<b>您的验证码是:' . $code . ',请在5分钟内使用,答应我不要告诉别人哦!</b>';
  44. $mail->AltBody = '您的验证码是:' . $code;
  45. $mail->send();
  46. $this->success('发送成功!', null);
  47. }
  48. // 内部调用 - 验证邮箱
  49. public function verifyEmail($email, $code, $event = 'register')
  50. {
  51. if (!$email || !$event) {
  52. $this->fail(500, '请检查传参!');
  53. }
  54. // 查询邮箱验证码
  55. $query = Db::table('tb_email_captcha')
  56. ->where('email', $email)
  57. ->where('code', $code);
  58. $email = $query->find();
  59. // 校验验证码状态
  60. if (!$email) {
  61. // 验证码未找到或不正确
  62. return false;
  63. }
  64. if ($email['times'] != 0 || time() - $email['createtime'] > 300) {
  65. // 验证码已经被使用或超过五分钟未使用
  66. return false;
  67. }
  68. // 通过验证 将验证码标记为已使用
  69. $query->update(['times' => 1]);
  70. return true;
  71. }
  72. // 用户验证邮箱
  73. public function verifyUserEmail()
  74. {
  75. $code = $this->request->post('code');// 邮箱验证码
  76. $user_id = $this->getUser()->user_id;
  77. // 验证用户
  78. $user = Db::table('tb_user')->where('id', $user_id)->find();
  79. if (!$user) {
  80. $this->fail(500, '无法找到用户信息!');
  81. }
  82. // 验证用户邮箱
  83. $verify = $this->verifyEmail($user['email'], $code, 'verify');
  84. if (!$verify) {
  85. $this->fail(500, '验证码错误或已过期!');
  86. }
  87. // 校验通过 更新用户邮箱已认证
  88. Db::table('tb_user')->where('id', $user['id'])->update(['is_email_verified' => 1]);
  89. $this->success('success', null);
  90. }
  91. }