EmailService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use App\Models\ConfigModel;
  13. use Illuminate\Support\Facades\Mail;
  14. /**
  15. * 邮件管理-服务类
  16. * @author laravel开发员
  17. * @since 2020/11/11
  18. * Class ConfigService
  19. * @package App\Services
  20. */
  21. class EmailService extends BaseService
  22. {
  23. /**
  24. * 配置参数
  25. * @var array
  26. */
  27. protected $config = [];
  28. /**
  29. * 初始化配置
  30. * @return bool
  31. */
  32. public function __construct()
  33. {
  34. $config = ConfigService::make()->getConfigByGroup(3);
  35. $mail = config('mail');
  36. $mailKey = md5(json_encode($mail));
  37. $mail['default'] = isset($config['mail_mailer']['value']) && $config['mail_mailer']['value'] ? $config['mail_mailer']['value'] : $mail['default'];
  38. $mail['markdown']['paths'] = [resource_path('views/vendor/mail')];
  39. $smtp = isset($mail['mailers']['smtp']) ? $mail['mailers']['smtp'] : [];
  40. $smtp['host'] = isset($config['mail_host']['value']) && $config['mail_host']['value'] ? $config['mail_host']['value'] : $smtp['host'];
  41. $smtp['port'] = isset($config['mail_port']['value']) && $config['mail_port']['value'] ? $config['mail_port']['value'] : $smtp['port'];
  42. $smtp['username'] = isset($config['mail_username']['value']) && $config['mail_username']['value'] ? $config['mail_username']['value'] : $smtp['host'];
  43. $smtp['password'] = isset($config['mail_password']['value']) && $config['mail_password']['value'] ? $config['mail_password']['value'] : $smtp['password'];
  44. $smtp['timeout'] = isset($config['mail_timeout']['value']) && $config['mail_timeout']['value'] ? $config['mail_timeout']['value'] : $smtp['timeout'];
  45. if ($mail['default'] != 'smtp') {
  46. return false;
  47. }
  48. $mail['mailers']['smtp'] = $smtp;
  49. $mail['from']['address'] = $smtp['username'];
  50. $mail['from']['name'] = 'UTC';
  51. $this->config = array_merge($config, $smtp);
  52. if ($mailKey != md5(json_encode($mail))) {
  53. //file_put_contents(base_path() . '/config/mail.php', '<?php \\n /* 邮箱服务配置 */ \\n return ' . var_export($mail, true) . ';' . '\\n');
  54. }
  55. return true;
  56. }
  57. /**
  58. * 发送邮件验证码
  59. * @param $email
  60. * @param string $scene 场景:默认reg-注册
  61. * @return bool
  62. */
  63. public function sendCode($email, $scene = 'reg')
  64. {
  65. try {
  66. $cacheKey = "stores:codes:email_{$scene}:" . md5($email);
  67. // 发送频繁
  68. if (RedisService::get($cacheKey . '_lock')) {
  69. $this->error = '1014';
  70. // return false;
  71. }
  72. $title = isset($this->config['mail_title_' . $scene]['value']) && $this->config['mail_title_' . $scene]['value'] ? $this->config['mail_title_' . $scene]['value'] : "UTC网站注册邮箱验证码";
  73. $template = isset($this->config['mail_template_' . $scene]['value']) && $this->config['mail_template_' . $scene]['value'] ? $this->config['mail_template_' . $scene]['value'] : "您的验证码是:{code},5分钟有效期!!!";
  74. // 生成验证码
  75. $code = rand(100000, 999999);
  76. $template = str_replace('{code}', $code, $template);
  77. // 上锁
  78. RedisService::set($cacheKey.'_lock', ['email'=> $email, 'code'=> $code, 'date'=> date('Y-m-d H:i:s')], rand(5, 10));
  79. if (Mail::send('emails.index', ['title' => $title,'template'=> $template], function ($message) use ($email, $template) {
  80. $to = $email;
  81. $message->to($to)->subject($template);
  82. })) {
  83. RedisService::set($cacheKey, ['email'=> $email, 'code'=> $code, 'date'=> date('Y-m-d H:i:s')], 300);
  84. return true;
  85. }
  86. } catch (\Exception $exception){
  87. $this->error = $exception->getMessage();
  88. return false;
  89. }
  90. $this->error = '1005';
  91. return false;
  92. }
  93. /**
  94. * 验证邮件验证码
  95. * @param $email
  96. * @param string $scene 场景:默认reg-注册
  97. * @return bool
  98. */
  99. public function check($email, $scene = 'reg')
  100. {
  101. return true;
  102. }
  103. }