EmailService.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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\Lib\smtpMail\smtp as smtpService;
  13. use PHPMailer\PHPMailer\PHPMailer;
  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. $mail = new PHPMailer(true);
  79. $mail->CharSet ="UTF-8"; //设定邮件编码
  80. $mail->SMTPDebug = 0; //调试模式输出:0 不输出,2 输出
  81. $mail->Timeout = 10; //超时
  82. // $mail->Mailer = 'SMTP';
  83. // $mail->isSendmail(); //使用SMTP
  84. $mail->Host = $this->config['host']; // SMTP服务器:以QQ为例
  85. $mail->SMTPAuth = true; // 允许 SMTP 认证
  86. $mail->Username = $this->config['username']; // SMTP用户名: 即发送方的邮箱
  87. $mail->Password = $this->config['password']; // SMTP授权码: 即发送方的邮箱授权码
  88. $mail->SMTPSecure = isset($this->config['mail_source'])? $this->config['mail_source'] : 'tls'; // 允许 TLS 或者ssl协议
  89. $mail->Port = $this->config['port']; // 服务器端口: 25 或者465 或者587 具体要看邮箱服务器支持
  90. RedisService::set('mailer', $this->config, 600);
  91. // 上锁
  92. RedisService::set($cacheKey.'_lock', ['email'=> $email, 'code'=> $code, 'date'=> date('Y-m-d H:i:s')], rand(5, 10));
  93. // 设置发送信息参数
  94. $mail->setFrom($this->config['username'], "UTC交易平台"); //发件人:邮箱与用户名
  95. $mail->addAddress($email, 'Mailer'); //收件人
  96. $mail->isHTML(true); //是否以HTML文档格式发送 发送后客户端可直接显示对应HTML内容
  97. $mail->Subject = $title; //邮件标题
  98. $mail->Body = $template; //邮件内容
  99. $mail->AltBody = "您的验证码是:{$code}"; //邮件内容:如果邮件客户端不支持HTML则显示此内容//*/
  100. $mail->send();
  101. RedisService::set($cacheKey, ['email'=> $email, 'code'=> $code, 'date'=> date('Y-m-d H:i:s')], 300);
  102. return true;
  103. } catch (\Exception $exception){
  104. $this->error = $exception->getMessage();
  105. return false;
  106. }
  107. }
  108. /**
  109. * 验证邮件验证码
  110. * @param $email
  111. * @param $code
  112. * @param string $scene 场景:默认reg-注册
  113. * @return bool
  114. */
  115. public function check($email, $code, $scene = 'reg')
  116. {
  117. return true;
  118. }
  119. }