EmailService.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace App\Modules\Mini\Services;
  3. use App\Base\BaseService;
  4. use App\Exceptions\ClientException;
  5. use App\Mail\ForgetPasswordEmail;
  6. use App\Mail\RegisterEmail;
  7. use Illuminate\Support\Facades\Mail;
  8. class EmailService extends BaseService
  9. {
  10. public function emailCaptcha($email, $source)
  11. {
  12. switch ($source) {
  13. case "register":
  14. return $this->register($email);
  15. case "forgetPassword":
  16. return $this->forgetPassword($email);
  17. default:
  18. throw new ClientException("source未匹配");
  19. }
  20. }
  21. private function register($email): bool
  22. {
  23. $captcha = app(CaptchaService::class)->createCaptcha($email);
  24. try {
  25. Mail::to($email)->send(new RegisterEmail($captcha));
  26. } catch (\Exception $e) {
  27. logger()->error("邮件发送失败 " . $e->getMessage());
  28. throw new ClientException("邮件发送失败,请稍后再试");
  29. }
  30. return true;
  31. }
  32. private function forgetPassword($email): bool
  33. {
  34. $captcha = app(CaptchaService::class)->createCaptcha($email);
  35. try {
  36. Mail::to($email)->send(new ForgetPasswordEmail($captcha));
  37. } catch (\Exception $e) {
  38. logger()->error("邮件发送失败 " . $e->getMessage());
  39. throw new ClientException("邮件发送失败,请稍后再试");
  40. }
  41. return true;
  42. }
  43. }