Email.class.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. namespace My;
  3. /**
  4. * Email驱动
  5. * @author Devil
  6. * @blog http://gong.gg/
  7. * @version 0.0.1
  8. * @datetime 2017-01-10T21:51:08+0800
  9. */
  10. class Email
  11. {
  12. private $interval_time;
  13. private $expire_time;
  14. private $key_code;
  15. public $error;
  16. private $obj;
  17. /**
  18. * [__construct 构造方法]
  19. * @author Devil
  20. * @blog http://gong.gg/
  21. * @version 0.0.1
  22. * @datetime 2017-03-07T14:03:02+0800
  23. * @param [int] $param['interval_time'] [间隔时间(默认30)单位(秒)]
  24. * @param [int] $param['expire_time'] [到期时间(默认30)单位(秒)]
  25. * @param [string] $param['key_prefix'] [验证码种存储前缀key(默认 空)]
  26. */
  27. public function __construct($param = array())
  28. {
  29. $this->interval_time = isset($param['interval_time']) ? intval($param['interval_time']) : 30;
  30. $this->expire_time = isset($param['expire_time']) ? intval($param['expire_time']) : 30;
  31. $this->key_code = isset($param['key_prefix']) ? trim($param['key_prefix']).'_sms_code' : '_sms_code';
  32. }
  33. /**
  34. * [EmailInit 邮件初始化]
  35. * @author Devil
  36. * @blog http://gong.gg/
  37. * @version 0.0.1
  38. * @datetime 2017-03-10T11:03:38+0800
  39. */
  40. private function EmailInit()
  41. {
  42. // 引入邮件发送类库
  43. vendor("PHPMailer.class#phpmailer");
  44. // 建立邮件发送类
  45. $this->obj = new \phpmailer();
  46. // 使用smtp方式发送
  47. $this->obj->IsSMTP();
  48. // 服务器host地址
  49. $this->obj->Host = MyC('common_email_smtp_host');
  50. //smtp验证功能;
  51. $this->obj->SMTPAuth = true;
  52. // 端口
  53. $this->obj->Port = MyC('common_email_smtp_port', 25, true);
  54. // 邮箱用户名
  55. $this->obj->Username = MyC('common_email_smtp_name');
  56. // 邮箱密码
  57. $this->obj->Password = MyC('common_email_smtp_pwd');
  58. // 发件人
  59. $this->obj->From = MyC('common_email_smtp_account');
  60. // 发件人姓名
  61. $this->obj->FromName = MyC('common_email_smtp_send_name');
  62. // 是否开启html格式
  63. $this->obj->IsHTML(true);
  64. // 设置编码
  65. $this->obj->CharSet = C('DEFAULT_CHARSET');
  66. }
  67. /**
  68. * [SendHtml html邮件发送]
  69. * @author Devil
  70. * @blog http://gong.gg/
  71. * @version 0.0.1
  72. * @datetime 2017-03-10T10:56:43+0800
  73. * @param [string] $param['email'] [收件邮箱]
  74. * @param [string] $param['content'] [内容]
  75. * @param [string] $param['title'] [标题]
  76. * @param [string] $param['code'] [验证码]
  77. * @param [string] $param['username'] [收件人名称]
  78. */
  79. public function SendHtml($param = array())
  80. {
  81. if(empty($param['email']))
  82. {
  83. $this->error = L('common_library_email_empty_error');
  84. return false;
  85. }
  86. if(empty($param['content']))
  87. {
  88. $this->error = L('common_library_content_empty_error');
  89. return false;
  90. }
  91. if(empty($param['title']))
  92. {
  93. $this->error = L('common_library_title_empty_error');
  94. return false;
  95. }
  96. // 是否频繁操作
  97. if(!$this->IntervalTimeCheck())
  98. {
  99. $this->error = L('common_prevent_harassment_error');
  100. return false;
  101. }
  102. // 验证码替换
  103. if(!empty($param['code']))
  104. {
  105. $param['content'] = str_replace('#code#', $param['code'], $param['content']);
  106. }
  107. // 邮件初始化
  108. $this->EmailInit();
  109. // 收件人地址,可以替换成任何想要接收邮件的email信箱,格式("收件人email","收件人姓名")
  110. $this->obj->AddAddress($param['email'], isset($param['username']) ? $param['username'] : $param['email']);
  111. // 邮件标题
  112. $this->obj->Subject = $param['title'];
  113. // 邮件内容
  114. $this->obj->Body = $param['content'];
  115. // 邮件正文不支持HTML的备用显示
  116. $this->obj->AltBody = strip_tags($param['content']);
  117. // 发送邮件
  118. if($this->obj->Send())
  119. {
  120. // 是否存在验证码
  121. if(!empty($param['code']))
  122. {
  123. $this->KindofSession($param['code']);
  124. }
  125. return true;
  126. } else {
  127. $this->error = $this->obj->ErrorInfo;
  128. }
  129. return false;
  130. }
  131. /**
  132. * [KindofSession 种验证码session]
  133. * @author Devil
  134. * @blog http://gong.gg/
  135. * @version 0.0.1
  136. * @datetime 2017-03-07T14:59:13+0800
  137. * @param [string] $code [验证码]
  138. */
  139. private function KindofSession($code)
  140. {
  141. $_SESSION[$this->key_code] = array(
  142. 'code' => $code,
  143. 'time' => time(),
  144. );
  145. }
  146. /**
  147. * [CheckExpire 验证码是否过期]
  148. * @author Devil
  149. * @blog http://gong.gg/
  150. * @version 0.0.1
  151. * @datetime 2017-03-05T19:02:26+0800
  152. * @return [boolean] [有效true, 无效false]
  153. */
  154. public function CheckExpire()
  155. {
  156. if(isset($_SESSION[$this->key_code]))
  157. {
  158. $data = $_SESSION[$this->key_code];
  159. return (time() <= $data['time']+$this->expire_time);
  160. }
  161. return false;
  162. }
  163. /**
  164. * [CheckCorrect 验证码是否正确]
  165. * @author Devil
  166. * @blog http://gong.gg/
  167. * @version 0.0.1
  168. * @datetime 2017-03-05T16:55:00+0800
  169. * @param [string] $code [验证码(默认从post读取)]
  170. * @return [booolean] [正确true, 错误false]
  171. */
  172. public function CheckCorrect($code = '')
  173. {
  174. if(isset($_SESSION[$this->key_code]['code']))
  175. {
  176. if(empty($code) && isset($_POST['code']))
  177. {
  178. $code = trim($_POST['code']);
  179. }
  180. return ($_SESSION[$this->key_code]['code'] == $code);
  181. }
  182. return false;
  183. }
  184. /**
  185. * [Remove 验证码清除]
  186. * @author Devil
  187. * @blog http://gong.gg/
  188. * @version 0.0.1
  189. * @datetime 2017-03-08T10:18:20+0800
  190. * @return [other] [无返回值]
  191. */
  192. public function Remove()
  193. {
  194. if(isset($_SESSION[$this->key_code]))
  195. {
  196. unset($_SESSION[$this->key_code]);
  197. }
  198. }
  199. /**
  200. * [IntervalTimeCheck 是否已经超过控制的间隔时间]
  201. * @author Devil
  202. * @blog http://gong.gg/
  203. * @version 0.0.1
  204. * @datetime 2017-03-10T11:26:52+0800
  205. * @return [booolean] [已超过间隔时间true, 未超过间隔时间false]
  206. */
  207. private function IntervalTimeCheck()
  208. {
  209. if(isset($_SESSION[$this->key_code]))
  210. {
  211. $data = $_SESSION[$this->key_code];
  212. return (time() > $data['time']+$this->interval_time);
  213. }
  214. return true;
  215. }
  216. }
  217. ?>