Email.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. /**
  3. * Email.php UTF-8
  4. * 邮箱发送
  5. *
  6. * @date : 2018/1/31 14:22
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : linjiebin <ljb@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\controller\email;
  13. //use huo\controller\common\Commonfunc;
  14. use think\Log;
  15. use think\Session;
  16. class Email {
  17. public function __construct($expire_diff = 1800) {
  18. $this->expaire_diff = $expire_diff;
  19. }
  20. /**
  21. * 自定义错误处理
  22. *
  23. * @param string $msg 输出的信息
  24. * @param string $level
  25. */
  26. private function _error($msg = '', $level = 'error') {
  27. $_info = 'email\Email Error:'.$msg;
  28. Log::record($_info, $level);
  29. }
  30. /**
  31. * 检查邮箱正确性
  32. *
  33. * @param string $email
  34. *
  35. *
  36. * @return bool
  37. */
  38. public function checkEmail($email) {
  39. $checkExpressions = "/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/i";
  40. if (false == preg_match($checkExpressions, $email)) {
  41. return false;
  42. }
  43. return true;
  44. }
  45. public function check($email, $code) {
  46. /* 1 校验邮箱是否正确 */
  47. $_rs = $this->checkEmail($email);
  48. if (false == $_rs || empty($code)) {
  49. $_rdata['code'] = '400';
  50. $_rdata['msg'] = '参数错误';
  51. return $_rdata;
  52. }
  53. $_sms_info = Session::get('user_email');
  54. /* 2 检查是否发送过邮件 */
  55. if (empty($_sms_info)
  56. || empty($_sms_info['email_code'])
  57. || empty($_sms_info['email'])
  58. || empty($_sms_info['expire_time'])
  59. ) {
  60. $_rdata['code'] = '416';
  61. $_rdata['msg'] = '请发送验证码';
  62. return $_rdata;
  63. }
  64. /* 3 检查邮件是否在有效期内 */
  65. $_rs = $this->hasSend($email, $_sms_info['email'], $_sms_info['expire_time']);
  66. if (false == $_rs) {
  67. $_rdata['code'] = '416';
  68. $_rdata['msg'] = '验证码已过期,请重新发送';
  69. return $_rdata;
  70. }
  71. if ($_sms_info['email_code'] != $code) {
  72. $_rdata['code'] = '416';
  73. $_rdata['msg'] = '验证码错误';
  74. return $_rdata;
  75. }
  76. Session::set('user_email', null);
  77. $_rdata['code'] = '200';
  78. $_rdata['msg'] = '验证通过';
  79. return $_rdata;
  80. }
  81. /**
  82. * 发送短信验证码
  83. *
  84. * @param string $post_email 接收的邮箱
  85. * @param INT $type 1 注册 2 登陆 3 修改密码 4 信息变更 5 找回密码
  86. * @param string $username 用户名
  87. *
  88. * @return bool
  89. */
  90. public function send($post_email, $username, $type = 0) {
  91. /* 检查邮箱格式是否正确 */
  92. $_rs = $this->checkEmail($post_email);
  93. if (false == $_rs) {
  94. $_rdata['code'] = '413';
  95. $_rdata['msg'] = '邮箱格式不正确';
  96. return $_rdata;
  97. }
  98. $_session_email_time = 60 + Session::get('email_time', 'user_email');
  99. $_rs = $this->hasSend($post_email, '', $_session_email_time);
  100. if ($_rs) {
  101. $_rdata['code'] = '416';
  102. $_rdata['msg'] = '验证码已发送,请稍后再试';
  103. return $_rdata;
  104. }
  105. $_code = rand(1000, 9999);
  106. $_rs = $this->sendEmail($post_email, $_code, $username, $type);
  107. if (true == $_rs) {
  108. Session::set('email', $post_email, 'user_email');
  109. Session::set('email_code', $_code, 'user_email');
  110. Session::set('type', $type, 'user_email');
  111. Session::set('email_time', time(), 'user_email');
  112. $_expairte_time = time() + $this->expaire_diff;
  113. Session::set('expire_time', $_expairte_time, 'user_email');
  114. $_rdata['code'] = '200';
  115. $_rdata['msg'] = '邮件发送成功';
  116. } else {
  117. $_rdata['code'] = '400';
  118. $_rdata['msg'] = '邮件发送失败';
  119. }
  120. return $_rdata;
  121. }
  122. /**
  123. * 判断是否已发送过验证码
  124. *
  125. * @param string $email 邮箱
  126. * @param string $session_email
  127. * @param int $session_ex_time
  128. *
  129. * @return bool 已发送返回true 未发送返回false
  130. */
  131. public function hasSend($email, $session_email = '', $session_ex_time = 0) {
  132. $_session_email = $session_email;
  133. if (empty($_session_email)) {
  134. $_session_email = Session::get('email', 'user_email');
  135. }
  136. $_session_ex_time = $session_ex_time;
  137. if (empty($_session_ex_time)) {
  138. $_session_ex_time = Session::get('expire_time', 'user_email');
  139. }
  140. if ($email == $_session_email && time() < $_session_ex_time) {
  141. return true;
  142. }
  143. return false;
  144. }
  145. /**
  146. * 发送邮箱验证码
  147. *
  148. * @param string $post_email 接收的邮箱
  149. * @param INT $type 1 注册 2 登陆 3 修改密码 4 信息变更 5 找回密码
  150. * @param string $code 验证码
  151. * @param string $username 用户名
  152. *
  153. *
  154. * @return bool
  155. */
  156. public function sendEmail($post_email, $code = '', $username, $type = 0) {
  157. $subject = "官方邮箱验证";
  158. $_type_string = \huo\controller\common\Commonfunc::getSmsTypeString($type);
  159. $_message = $this->getSendMsg($username, $code, $_type_string);
  160. $_email_info = sp_send_email($post_email, $subject, $_message);
  161. if (0 == $_email_info['error']) {
  162. return true;
  163. }
  164. $this->_error(json_encode($_email_info));
  165. return false;
  166. }
  167. /**
  168. * @param string $username 接收人玩家名称
  169. * @param string $code 接收人的code
  170. * @param string $type_string 接收人类型
  171. *
  172. * @return string 邮件内容
  173. */
  174. public function getSendMsg($username, $code, $type_string = '身份验证') {
  175. $_message
  176. = <<< EOT
  177. <html lang='zh-cn'>
  178. <head>
  179. <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
  180. <style>
  181. h1 {
  182. font-size: 16px;
  183. }
  184. p {
  185. font-size: 14px;
  186. line-height: 23px;
  187. }
  188. .red {
  189. color: #f30808;
  190. font-size: 14px;
  191. }
  192. .redNum {
  193. display: block;
  194. color: #fff;
  195. background: #aac5eb;
  196. font-size: 33px;
  197. margin: auto;
  198. width: 132px;
  199. text-align: center;
  200. border: 1px solid;
  201. height: 36px;
  202. line-height: 36px;
  203. position: relative;
  204. }
  205. .redNum:before {
  206. content: '';
  207. display: block;
  208. height: 100%;
  209. width: 3px;
  210. position: absolute;
  211. left: -4px;
  212. background: #aac5eb;
  213. }
  214. .redNum:after {
  215. content: '';
  216. display: block;
  217. height: 100%;
  218. width: 3px;
  219. position: absolute;
  220. top: 0;
  221. right: -4px;
  222. background: #aac5eb;
  223. }
  224. .wrap {
  225. width: 682px;
  226. height: 594px;
  227. no-repeat;
  228. padding: 60px 30px 50px 30px;
  229. }
  230. .content {
  231. width: 620px;
  232. height: 590px;
  233. word-break: break-all;
  234. padding-top: 20px;
  235. padding-left: 30px;
  236. padding-right: 30px;
  237. }
  238. .confirm-mail {
  239. display: block;
  240. width: 285px;
  241. height: 82px;
  242. margin-left: 134px;
  243. margin-bottom: 30px;
  244. }
  245. </style>
  246. </head>
  247. <body>
  248. <div class='wrap'>
  249. <div class='content'>
  250. <h1>尊敬的用户( {$username} ):</h1>
  251. <p>
  252. 您正在进行{$type_string},要完成该操作,请在<span class='red'>30分钟</span>内输入如下验证码:
  253. </p>
  254. <p>
  255. <span class='redNum'>{$code}</span>
  256. </p>
  257. <p>如果您输入验证码,或者点击上述链接,提示已过期,请重新发起设置申请,感谢您的配合与支持!</p>
  258. <p>(如非本人操作,请忽略此邮件)</p>
  259. <p>&nbsp;</p>
  260. </div>
  261. </div>
  262. </body>
  263. </html>
  264. EOT;
  265. return $_message;
  266. }
  267. }