MailCaptcha.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\service\passport;
  13. use think\facade\Log;
  14. use yiovo\cache\facade\Cache;
  15. use yiovo\captcha\facade\CaptchaApi;
  16. use app\api\validate\passport\EmailCaptcha as ValidateEmailCaptcha;
  17. use app\common\service\BaseService;
  18. use cores\exception\BaseException;
  19. use PHPMailer\PHPMailer\PHPMailer;
  20. use PHPMailer\PHPMailer\SMTP;
  21. use PHPMailer\PHPMailer\Exception;
  22. /**
  23. * 服务类:发送邮箱验证码
  24. * Class MailCaptcha
  25. * @package app\api\service\passport
  26. */
  27. class MailCaptcha extends BaseService
  28. {
  29. // 最大发送次数,默认10次
  30. protected int $sendTimes = 10;
  31. // 发送限制间隔时间,默认24小时
  32. protected int $safeTime = 86400;
  33. /**
  34. * 发送邮件验证码
  35. * @param array $data
  36. * @return bool
  37. * @throws BaseException
  38. */
  39. public function handle(array $data): bool
  40. {
  41. // 数据验证
  42. $this->validate($data);
  43. // 执行发送短信
  44. if (!$this->sendCaptcha($data['email'])) {
  45. return false;
  46. }
  47. return true;
  48. }
  49. /**
  50. * 执行发送邮箱验证码
  51. * @param string $email
  52. * @return bool
  53. */
  54. public function sendCaptcha(string $email): bool
  55. {
  56. // 缓存发送记录并判断次数
  57. if (!$this->record($email)) {
  58. return false;
  59. }
  60. // 生成验证码
  61. $smsCaptcha = CaptchaApi::createSMS($email);
  62. $mail = new PHPMailer(true);
  63. try {
  64. //Server settings
  65. $mail->SMTPDebug = SMTP::DEBUG_OFF;//DEBUG_SERVER //Enable verbose debug output
  66. $mail->isSMTP(); //Send using SMTP
  67. $mail->Host = 'smtp.163.com'; //Set the SMTP server to send through
  68. $mail->SMTPAuth = true; //Enable SMTP authentication
  69. $mail->Username = 'zhangdehua814@163.com'; //SMTP username
  70. $mail->Password = 'SGPMIQCKVKGIJUGQ'; //SMTP password
  71. $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
  72. $mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
  73. //Recipients
  74. $mail->setFrom('zhangdehua814@163.com', 'Mailer');
  75. $mail->addAddress($email, 'Gold'); //Add a recipient
  76. //$mail->addAddress('ellen@example.com'); //Name is optional
  77. //$mail->addReplyTo('info@example.com', 'Information');
  78. //$mail->addCC('cc@example.com');
  79. //$mail->addBCC('bcc@example.com');
  80. //Attachments
  81. //$mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments
  82. //$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name
  83. //Content
  84. //$mail->isHTML(true); //Set email format to HTML
  85. $mail->Subject = 'Captcha';
  86. $mail->Body = 'Your captcha is ' . $smsCaptcha['code'] . '.Please use it in 15 minutes';
  87. $mail->AltBody = 'This is the Your captcha for vapes';
  88. $mail->send();
  89. //echo 'Message has been sent';
  90. } catch (Exception $e) {
  91. //echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
  92. Log::error(__CLASS__ . ':' . __METHOD__ . $e->getMessage() . '---' . $mail->ErrorInfo);
  93. Log::error(__CLASS__ . ':' . __METHOD__, ['errMsg' => $e->getMessage(), 'mailError' => $mail->ErrorInfo]);
  94. return false;
  95. }
  96. return true;
  97. }
  98. /**
  99. * 发送营销文本邮件
  100. * @param string $email
  101. * @param string $subject
  102. * @param string $body
  103. * @param string $altBody
  104. * @param bool $isHtml
  105. * @param string $name
  106. * @return bool
  107. */
  108. public function sendText(string $email, string $subject = '', string $body = '', string $altBody = '', bool $isHtml = false, string $name = 'vip'): bool
  109. {
  110. $mail = new PHPMailer(true);
  111. try {
  112. //Server settings
  113. $mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
  114. $mail->isSMTP(); //Send using SMTP
  115. $mail->Host = 'smtp.163.com'; //Set the SMTP server to send through
  116. $mail->SMTPAuth = true; //Enable SMTP authentication
  117. $mail->Username = 'zhangdehua814@163.com'; //SMTP username
  118. $mail->Password = 'SGPMIQCKVKGIJUGQ'; //SMTP password
  119. $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
  120. $mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
  121. //Recipients
  122. $mail->setFrom('zhangdehua814@163.com', 'Mailer');
  123. $mail->addAddress($email, $name); //Add a recipient
  124. //Content
  125. if ($isHtml) {
  126. $mail->isHTML(true); //Set email format to HTML
  127. }
  128. $mail->Subject = $subject;
  129. $mail->Body = $body;
  130. if (!empty($altBody)) {
  131. $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
  132. }
  133. $mail->send();
  134. echo 'Message has been sent';
  135. } catch (Exception $e) {
  136. echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
  137. Log::error(__CLASS__ . ':' . __METHOD__, ['errMsg' => $e->getMessage(), 'mailError' => $mail->ErrorInfo]);
  138. return false;
  139. }
  140. return true;
  141. }
  142. /**
  143. * 记录短信验证码发送记录并判断是否超出发送限制
  144. * @param string $mobile
  145. * @return bool
  146. */
  147. private function record(string $email): bool
  148. {
  149. // 获取发送记录缓存
  150. $record = Cache::get("sendCaptchaEmail.$email");
  151. // 写入缓存:记录剩余发送次数
  152. if (empty($record)) {
  153. Cache::set("sendCaptchaEmail.$email", ['times' => $this->sendTimes - 1], $this->safeTime);
  154. return true;
  155. }
  156. // 判断发送次数是否合法
  157. if ($record['times'] <= 0) {
  158. $this->error = '很抱歉,已超出今日最大发送次数限制';
  159. return false;
  160. }
  161. // 发送次数递减
  162. Cache::update("sendCaptchaEmail.$email", ['times' => $record['times'] - 1]);
  163. return true;
  164. }
  165. /**
  166. * 数据验证
  167. * @param array $data
  168. * @throws BaseException
  169. */
  170. public function validate(array $data)
  171. {
  172. // 数据验证
  173. $validate = new ValidateEmailCaptcha;
  174. if (!$validate->check($data)) {
  175. throwError($validate->getError());
  176. }
  177. // 验证图形验证码
  178. // if (!CaptchaApi::check($data['captchaCode'], $data['captchaKey'])) {
  179. // throwError('很抱歉,图形验证码不正确');
  180. // }
  181. }
  182. public function checkCaptcha($captchaCode = '', $email = '')
  183. {
  184. $localCode = Cache::get('captchaSMS.' . $email);
  185. if (empty($localCode) || empty($localCode['code']) || $localCode['code'] != $captchaCode) {
  186. throwError('Sorry,your captcha is invalid');
  187. }
  188. //dd($localCode);
  189. }
  190. }