Ems.php 3.7 KB

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