Ems.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace app\api\controller;
  3. use ba\Captcha;
  4. use think\facade\Validate;
  5. use app\common\model\User;
  6. use app\common\library\Email;
  7. use app\common\controller\Frontend;
  8. use PHPMailer\PHPMailer\Exception as PHPMailerException;
  9. class Ems extends Frontend
  10. {
  11. protected $noNeedLogin = ['send'];
  12. public function initialize()
  13. {
  14. parent::initialize();
  15. }
  16. /**
  17. * 发送邮件
  18. * event 事件:user_register=用户注册,user_change_email=用户修改邮箱,user_retrieve_pwd=用户找回密码,user_email_verify=验证账户
  19. * 不同的事件,会自动做各种必要检查,其中 验证账户 要求用户输入当前密码才能发送验证码邮件
  20. */
  21. public function send()
  22. {
  23. $email = $this->request->post('email');
  24. $event = $this->request->post('event');
  25. $mail = new Email();
  26. if (!$mail->configured) {
  27. $this->error(__('Mail sending service unavailable'));
  28. }
  29. $validate = Validate::rule(['email' => 'require|email', 'event' => 'require'])->message(['email' => 'email format error', 'event' => 'Parameter error']);
  30. if (!$validate->check(['email' => $email, 'event' => $event])) {
  31. $this->error(__($validate->getError()));
  32. }
  33. // 检查频繁发送
  34. $captcha = (new Captcha())->getCaptchaData($email . $event);
  35. if ($captcha && time() - $captcha['createtime'] < 60) {
  36. $this->error(__('Frequent email sending'));
  37. }
  38. // 检查邮箱
  39. $userInfo = User::where('email', $email)->find();
  40. if ($event == 'user_register' && $userInfo) {
  41. $this->error(__('Email has been registered, please log in directly'));
  42. } elseif ($event == 'user_change_email' && $userInfo) {
  43. $this->error(__('The email has been occupied'));
  44. } elseif (in_array($event, ['user_retrieve_pwd', 'user_email_verify']) && !$userInfo) {
  45. $this->error(__('Email not registered'));
  46. }
  47. // 通过邮箱验证账户
  48. if ($event == 'user_email_verify') {
  49. if (!$this->auth->isLogin()) {
  50. $this->error(__('Please login first'));
  51. }
  52. if ($this->auth->email != $email) {
  53. $this->error(__('Please use the account registration email to send the verification code'));
  54. }
  55. // 验证账户密码
  56. $password = $this->request->post('password');
  57. if ($this->auth->password != encrypt_password($password, $this->auth->salt)) {
  58. $this->error(__('Password error'));
  59. }
  60. }
  61. // 生成一个验证码
  62. $captcha = new Captcha();
  63. $code = $captcha->create($email . $event);
  64. $subject = __($event) . '-' . get_sys_config('site_name');
  65. $body = __('Your verification code is: %s', [$code]);
  66. try {
  67. $mail->isSMTP();
  68. $mail->addAddress($email);
  69. $mail->isHTML();
  70. $mail->setSubject($subject);
  71. $mail->Body = $body;
  72. $mail->send();
  73. } catch (PHPMailerException $e) {
  74. $this->error($mail->ErrorInfo);
  75. }
  76. $this->success(__('Mail sent successfully~'));
  77. }
  78. }