UMailer.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <?php
  2. namespace app\index\service\passport;
  3. use yiovo\cache\facade\Cache;
  4. use app\common\service\BaseService;
  5. /**
  6. * Created by umail
  7. * User: NUC
  8. * Date: 2018/8/22
  9. * Time: 14:10
  10. */
  11. class UMailer extends BaseService
  12. {
  13. // 最大发送次数,默认10次
  14. protected $sendTimes = 10;
  15. // 发送限制间隔时间,默认24小时
  16. protected $safeTime = 86400;
  17. //验证码过期时间
  18. protected $expireTime = 300;
  19. //可重复使用次数
  20. public $checkTimes = 5;
  21. /* Public Variables */
  22. public $smtp_port;
  23. public $time_out;
  24. public $host_name;
  25. public $log_file;
  26. public $relay_host;
  27. public $debug;
  28. public $auth;
  29. public $user;
  30. public $pass;
  31. /* Private Variables */
  32. private $sock;
  33. /* Constractor */
  34. function __construct($relay_host = "", $smtp_port = 25, $auth = false, $user = '', $pass = '')
  35. {
  36. parent::__construct();
  37. $this->debug = FALSE;
  38. $this->smtp_port = $smtp_port;
  39. $this->relay_host = $relay_host;
  40. $this->time_out = 30; //is used in fsockopen()
  41. #
  42. $this->auth = $auth;//auth
  43. $this->user = $user;
  44. $this->pass = $pass;
  45. #
  46. $this->host_name = "localhost"; //is used in HELO command
  47. $this->log_file = "";
  48. $this->sock = FALSE;
  49. }
  50. /**
  51. * 记录短信验证码发送记录并判断是否超出发送限制
  52. * @param string $email
  53. * @return bool
  54. */
  55. private function record(string $email): bool
  56. {
  57. // 获取发送记录缓存
  58. $record = Cache::get("sendCaptchaEmail.$email");
  59. // 写入缓存:记录剩余发送次数
  60. if (empty($record)) {
  61. Cache::set("sendCaptchaEmail.$email", ['times' => $this->sendTimes - 1], $this->safeTime);
  62. return true;
  63. }
  64. // 判断发送次数是否合法
  65. if ($record['times'] <= 0) {
  66. $this->error = 'Sorry,up to the limit today.';
  67. return false;
  68. }
  69. // 发送次数递减
  70. Cache::update("sendCaptchaEmail.$email", ['times' => $record['times'] - 1]);
  71. return true;
  72. }
  73. /* Main Function */
  74. function sendmail($to, $from, $subject = "Code", $mailtype = 'TXT', $cc = "", $bcc = "", $additional_headers = "")
  75. {
  76. if (!$this->record($to)) {
  77. return false;
  78. }
  79. $smsCaptcha = (string)mt_rand(100000, 999999);
  80. Cache::set("captchaSMS.{$to}", ['code' => $smsCaptcha, 'times' => $this->checkTimes], $this->expireTime);
  81. $body = 'Your code is ' . $smsCaptcha . '.Please use it in 5 minutes';
  82. $header = '';
  83. $mail_from = $this->get_address($this->strip_comment($from));
  84. $body = preg_replace("/(^|(\r\n))(\\.)/", "\\1.\\3", $body);
  85. $header .= "MIME-Version:1.0\r\n";
  86. if ($mailtype == "HTML") {
  87. $header .= "Content-Type:text/html\r\n";
  88. }
  89. $header .= "To: " . $to . "\r\n";
  90. if ($cc != "") {
  91. $header .= "Cc: " . $cc . "\r\n";
  92. }
  93. $header .= "From: $from<" . $from . ">\r\n";
  94. $header .= "Subject: " . $subject . "\r\n";
  95. $header .= $additional_headers;
  96. $header .= "Date: " . date("r") . "\r\n";
  97. $header .= "X-Mailer:By Redhat (PHP/" . phpversion() . ")\r\n";
  98. list($msec, $sec) = explode(" ", microtime());
  99. $header .= "Message-ID: <" . date("YmdHis", $sec) . "." . ($msec * 1000000) . "." . $mail_from . ">\r\n";
  100. $TO = explode(",", $this->strip_comment($to));
  101. if ($cc != "") {
  102. $TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
  103. }
  104. if ($bcc != "") {
  105. $TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
  106. }
  107. $sent = TRUE;
  108. foreach ($TO as $rcpt_to) {
  109. $rcpt_to = $this->get_address($rcpt_to);
  110. if (!$this->smtp_sockopen($rcpt_to)) {
  111. $this->log_write("Error: Cannot send email to " . $rcpt_to . "\n");
  112. $sent = FALSE;
  113. continue;
  114. }
  115. if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
  116. $this->log_write("E-mail has been sent to <" . $rcpt_to . ">\n");
  117. } else {
  118. $this->log_write("Error: Cannot send email to <" . $rcpt_to . ">\n");
  119. $sent = FALSE;
  120. }
  121. fclose($this->sock);
  122. $this->log_write("Disconnected from remote host\n");
  123. }
  124. //echo "<br>";
  125. //echo $header; //
  126. return $sent;
  127. }
  128. /* Private Functions */
  129. function smtp_send($helo, $from, $to, $header, $body = "")
  130. {
  131. if (!$this->smtp_putcmd("HELO", $helo)) {
  132. return $this->smtp_error("sending HELO command");
  133. }
  134. #auth
  135. if ($this->auth) {
  136. if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
  137. return $this->smtp_error("sending HELO command");
  138. }
  139. if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
  140. return $this->smtp_error("sending HELO command");
  141. }
  142. }
  143. #
  144. if (!$this->smtp_putcmd("MAIL", "FROM:<" . $from . ">")) {
  145. return $this->smtp_error("sending MAIL FROM command");
  146. }
  147. if (!$this->smtp_putcmd("RCPT", "TO:<" . $to . ">")) {
  148. return $this->smtp_error("sending RCPT TO command");
  149. }
  150. if (!$this->smtp_putcmd("DATA")) {
  151. return $this->smtp_error("sending DATA command");
  152. }
  153. if (!$this->smtp_message($header, $body)) {
  154. return $this->smtp_error("sending message");
  155. }
  156. if (!$this->smtp_eom()) {
  157. return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
  158. }
  159. if (!$this->smtp_putcmd("QUIT")) {
  160. return $this->smtp_error("sending QUIT command");
  161. }
  162. return TRUE;
  163. }
  164. function smtp_sockopen($address)
  165. {
  166. if ($this->relay_host == "") {
  167. return $this->smtp_sockopen_mx($address);
  168. } else {
  169. return $this->smtp_sockopen_relay();
  170. }
  171. }
  172. function smtp_sockopen_relay()
  173. {
  174. $this->log_write("Trying to " . $this->relay_host . ":" . $this->smtp_port . "\n");
  175. $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
  176. if (!($this->sock && $this->smtp_ok())) {
  177. $this->log_write("Error: Cannot connenct to relay host " . $this->relay_host . "\n");
  178. $this->log_write("Error: " . $errstr . " (" . $errno . ")\n");
  179. return FALSE;
  180. }
  181. $this->log_write("Connected to relay host " . $this->relay_host . "\n");
  182. return TRUE;;
  183. }
  184. function smtp_sockopen_mx($address)
  185. {
  186. $domain = preg_replace("/^.+@([^@]+)$/", "\\1", $address);
  187. if (!@getmxrr($domain, $MXHOSTS)) {
  188. $this->log_write("Error: Cannot resolve MX \"" . $domain . "\"\n");
  189. return FALSE;
  190. }
  191. foreach ($MXHOSTS as $host) {
  192. $this->log_write("Trying to " . $host . ":" . $this->smtp_port . "\n");
  193. $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
  194. if (!($this->sock && $this->smtp_ok())) {
  195. $this->log_write("Warning: Cannot connect to mx host " . $host . "\n");
  196. $this->log_write("Error: " . $errstr . " (" . $errno . ")\n");
  197. continue;
  198. }
  199. $this->log_write("Connected to mx host " . $host . "\n");
  200. return TRUE;
  201. }
  202. $this->log_write("Error: Cannot connect to any mx hosts (" . implode(", ", $MXHOSTS) . ")\n");
  203. return FALSE;
  204. }
  205. function smtp_message($header, $body)
  206. {
  207. fputs($this->sock, $header . "\r\n" . $body);
  208. $this->smtp_debug("> " . str_replace("\r\n", "\n" . "> ", $header . "\n> " . $body . "\n> "));
  209. return TRUE;
  210. }
  211. function smtp_eom()
  212. {
  213. fputs($this->sock, "\r\n.\r\n");
  214. $this->smtp_debug(". [EOM]\n");
  215. return $this->smtp_ok();
  216. }
  217. function smtp_ok()
  218. {
  219. $response = str_replace("\r\n", "", fgets($this->sock, 512));
  220. $this->smtp_debug($response . "\n");
  221. if (!preg_match("/^[23]/", $response)) {
  222. fputs($this->sock, "QUIT\r\n");
  223. fgets($this->sock, 512);
  224. $this->log_write("Error: Remote host returned \"" . $response . "\"\n");
  225. return FALSE;
  226. }
  227. return TRUE;
  228. }
  229. function smtp_putcmd($cmd, $arg = "")
  230. {
  231. if ($arg != "") {
  232. if ($cmd == "") $cmd = $arg;
  233. else $cmd = $cmd . " " . $arg;
  234. }
  235. fputs($this->sock, $cmd . "\r\n");
  236. $this->smtp_debug("> " . $cmd . "\n");
  237. return $this->smtp_ok();
  238. }
  239. function smtp_error($string)
  240. {
  241. $this->log_write("Error: Error occurred while " . $string . ".\n");
  242. return FALSE;
  243. }
  244. function log_write($message)
  245. {
  246. $this->smtp_debug($message);
  247. if ($this->log_file == "") {
  248. return TRUE;
  249. }
  250. $message = date("M d H:i:s ") . get_current_user() . "[" . getmypid() . "]: " . $message;
  251. if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
  252. $this->smtp_debug("Warning: Cannot open log file \"" . $this->log_file . "\"\n");
  253. return FALSE;
  254. }
  255. flock($fp, LOCK_EX);
  256. fputs($fp, $message);
  257. fclose($fp);
  258. return TRUE;
  259. }
  260. function strip_comment($address)
  261. {
  262. $comment = "/\\([^()]*\\)/";
  263. while (preg_match($comment, $address)) {
  264. $address = preg_replace($comment, "", $address);
  265. }
  266. return $address;
  267. }
  268. function get_address($address)
  269. {
  270. $address = preg_replace("/([ \t\r\n])+/", "", $address);
  271. $address = preg_replace("/^.*<(.+)>.*$/", "\\1", $address);
  272. return $address;
  273. }
  274. function smtp_debug($message)
  275. {
  276. if ($this->debug) {
  277. log_record($message,'info');
  278. //echo $message . "<br>";
  279. }
  280. }
  281. function get_attach_type($image_tag)
  282. { //
  283. $filedata = array();
  284. $img_file_con = fopen($image_tag, "r");
  285. unset($image_data);
  286. while ($tem_buffer = AddSlashes(fread($img_file_con, filesize($image_tag))))
  287. $image_data .= $tem_buffer;
  288. fclose($img_file_con);
  289. $filedata['context'] = $image_data;
  290. $filedata['filename'] = basename($image_tag);
  291. $extension = substr($image_tag, strrpos($image_tag, "."), strlen($image_tag) - strrpos($image_tag, "."));
  292. switch ($extension) {
  293. case ".gif":
  294. $filedata['type'] = "image/gif";
  295. break;
  296. case ".gz":
  297. $filedata['type'] = "application/x-gzip";
  298. break;
  299. case ".html":
  300. case ".htm":
  301. $filedata['type'] = "text/html";
  302. break;
  303. case ".jpg":
  304. $filedata['type'] = "image/jpeg";
  305. break;
  306. case ".tar":
  307. $filedata['type'] = "application/x-tar";
  308. break;
  309. case ".txt":
  310. $filedata['type'] = "text/plain";
  311. break;
  312. case ".zip":
  313. $filedata['type'] = "application/zip";
  314. break;
  315. default:
  316. $filedata['type'] = "application/octet-stream";
  317. break;
  318. }
  319. return $filedata;
  320. }
  321. } // end class
  322. ?>