UMailer.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. function sendShareText($to, $from, $subject = "Code", $mailtype = 'TXT', $cc = "", $bcc = "", $additional_headers = "", $body = '')
  129. {
  130. if (!$this->record($to)) {
  131. return false;
  132. }
  133. $header = '';
  134. $mail_from = $this->get_address($this->strip_comment($from));
  135. $body = preg_replace("/(^|(\r\n))(\\.)/", "\\1.\\3", $body);
  136. $header .= "MIME-Version:1.0\r\n";
  137. if ($mailtype == "HTML") {
  138. $header .= "Content-Type:text/html\r\n";
  139. }
  140. $header .= "To: " . $to . "\r\n";
  141. if ($cc != "") {
  142. $header .= "Cc: " . $cc . "\r\n";
  143. }
  144. $header .= "From: $from<" . $from . ">\r\n";
  145. $header .= "Subject: " . $subject . "\r\n";
  146. $header .= $additional_headers;
  147. $header .= "Date: " . date("r") . "\r\n";
  148. $header .= "X-Mailer:By Redhat (PHP/" . phpversion() . ")\r\n";
  149. list($msec, $sec) = explode(" ", microtime());
  150. $header .= "Message-ID: <" . date("YmdHis", $sec) . "." . ($msec * 1000000) . "." . $mail_from . ">\r\n";
  151. $TO = explode(",", $this->strip_comment($to));
  152. if ($cc != "") {
  153. $TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
  154. }
  155. if ($bcc != "") {
  156. $TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
  157. }
  158. $sent = TRUE;
  159. foreach ($TO as $rcpt_to) {
  160. $rcpt_to = $this->get_address($rcpt_to);
  161. if (!$this->smtp_sockopen($rcpt_to)) {
  162. $this->log_write("Error: Cannot send email to " . $rcpt_to . "\n");
  163. $sent = FALSE;
  164. continue;
  165. }
  166. if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
  167. $this->log_write("E-mail has been sent to <" . $rcpt_to . ">\n");
  168. } else {
  169. $this->log_write("Error: Cannot send email to <" . $rcpt_to . ">\n");
  170. $sent = FALSE;
  171. }
  172. fclose($this->sock);
  173. $this->log_write("Disconnected from remote host\n");
  174. }
  175. //echo "<br>";
  176. //echo $header; //
  177. return $sent;
  178. }
  179. /* Private Functions */
  180. function smtp_send($helo, $from, $to, $header, $body = "")
  181. {
  182. if (!$this->smtp_putcmd("HELO", $helo)) {
  183. return $this->smtp_error("sending HELO command");
  184. }
  185. #auth
  186. if ($this->auth) {
  187. if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
  188. return $this->smtp_error("sending HELO command");
  189. }
  190. if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
  191. return $this->smtp_error("sending HELO command");
  192. }
  193. }
  194. #
  195. if (!$this->smtp_putcmd("MAIL", "FROM:<" . $from . ">")) {
  196. return $this->smtp_error("sending MAIL FROM command");
  197. }
  198. if (!$this->smtp_putcmd("RCPT", "TO:<" . $to . ">")) {
  199. return $this->smtp_error("sending RCPT TO command");
  200. }
  201. if (!$this->smtp_putcmd("DATA")) {
  202. return $this->smtp_error("sending DATA command");
  203. }
  204. if (!$this->smtp_message($header, $body)) {
  205. return $this->smtp_error("sending message");
  206. }
  207. if (!$this->smtp_eom()) {
  208. return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
  209. }
  210. if (!$this->smtp_putcmd("QUIT")) {
  211. return $this->smtp_error("sending QUIT command");
  212. }
  213. return TRUE;
  214. }
  215. function smtp_sockopen($address)
  216. {
  217. if ($this->relay_host == "") {
  218. return $this->smtp_sockopen_mx($address);
  219. } else {
  220. return $this->smtp_sockopen_relay();
  221. }
  222. }
  223. function smtp_sockopen_relay()
  224. {
  225. $this->log_write("Trying to " . $this->relay_host . ":" . $this->smtp_port . "\n");
  226. $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
  227. if (!($this->sock && $this->smtp_ok())) {
  228. $this->log_write("Error: Cannot connenct to relay host " . $this->relay_host . "\n");
  229. $this->log_write("Error: " . $errstr . " (" . $errno . ")\n");
  230. return FALSE;
  231. }
  232. $this->log_write("Connected to relay host " . $this->relay_host . "\n");
  233. return TRUE;;
  234. }
  235. function smtp_sockopen_mx($address)
  236. {
  237. $domain = preg_replace("/^.+@([^@]+)$/", "\\1", $address);
  238. if (!@getmxrr($domain, $MXHOSTS)) {
  239. $this->log_write("Error: Cannot resolve MX \"" . $domain . "\"\n");
  240. return FALSE;
  241. }
  242. foreach ($MXHOSTS as $host) {
  243. $this->log_write("Trying to " . $host . ":" . $this->smtp_port . "\n");
  244. $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
  245. if (!($this->sock && $this->smtp_ok())) {
  246. $this->log_write("Warning: Cannot connect to mx host " . $host . "\n");
  247. $this->log_write("Error: " . $errstr . " (" . $errno . ")\n");
  248. continue;
  249. }
  250. $this->log_write("Connected to mx host " . $host . "\n");
  251. return TRUE;
  252. }
  253. $this->log_write("Error: Cannot connect to any mx hosts (" . implode(", ", $MXHOSTS) . ")\n");
  254. return FALSE;
  255. }
  256. function smtp_message($header, $body)
  257. {
  258. fputs($this->sock, $header . "\r\n" . $body);
  259. $this->smtp_debug("> " . str_replace("\r\n", "\n" . "> ", $header . "\n> " . $body . "\n> "));
  260. return TRUE;
  261. }
  262. function smtp_eom()
  263. {
  264. fputs($this->sock, "\r\n.\r\n");
  265. $this->smtp_debug(". [EOM]\n");
  266. return $this->smtp_ok();
  267. }
  268. function smtp_ok()
  269. {
  270. $response = str_replace("\r\n", "", fgets($this->sock, 512));
  271. $this->smtp_debug($response . "\n");
  272. if (!preg_match("/^[23]/", $response)) {
  273. fputs($this->sock, "QUIT\r\n");
  274. fgets($this->sock, 512);
  275. $this->log_write("Error: Remote host returned \"" . $response . "\"\n");
  276. return FALSE;
  277. }
  278. return TRUE;
  279. }
  280. function smtp_putcmd($cmd, $arg = "")
  281. {
  282. if ($arg != "") {
  283. if ($cmd == "") $cmd = $arg;
  284. else $cmd = $cmd . " " . $arg;
  285. }
  286. fputs($this->sock, $cmd . "\r\n");
  287. $this->smtp_debug("> " . $cmd . "\n");
  288. return $this->smtp_ok();
  289. }
  290. function smtp_error($string)
  291. {
  292. $this->log_write("Error: Error occurred while " . $string . ".\n");
  293. return FALSE;
  294. }
  295. function log_write($message)
  296. {
  297. $this->smtp_debug($message);
  298. if ($this->log_file == "") {
  299. return TRUE;
  300. }
  301. $message = date("M d H:i:s ") . get_current_user() . "[" . getmypid() . "]: " . $message;
  302. if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
  303. $this->smtp_debug("Warning: Cannot open log file \"" . $this->log_file . "\"\n");
  304. return FALSE;
  305. }
  306. flock($fp, LOCK_EX);
  307. fputs($fp, $message);
  308. fclose($fp);
  309. return TRUE;
  310. }
  311. function strip_comment($address)
  312. {
  313. $comment = "/\\([^()]*\\)/";
  314. while (preg_match($comment, $address)) {
  315. $address = preg_replace($comment, "", $address);
  316. }
  317. return $address;
  318. }
  319. function get_address($address)
  320. {
  321. $address = preg_replace("/([ \t\r\n])+/", "", $address);
  322. $address = preg_replace("/^.*<(.+)>.*$/", "\\1", $address);
  323. return $address;
  324. }
  325. function smtp_debug($message)
  326. {
  327. if ($this->debug) {
  328. log_record($message,'info');
  329. //echo $message . "<br>";
  330. }
  331. }
  332. function get_attach_type($image_tag)
  333. { //
  334. $filedata = array();
  335. $img_file_con = fopen($image_tag, "r");
  336. unset($image_data);
  337. while ($tem_buffer = AddSlashes(fread($img_file_con, filesize($image_tag))))
  338. $image_data .= $tem_buffer;
  339. fclose($img_file_con);
  340. $filedata['context'] = $image_data;
  341. $filedata['filename'] = basename($image_tag);
  342. $extension = substr($image_tag, strrpos($image_tag, "."), strlen($image_tag) - strrpos($image_tag, "."));
  343. switch ($extension) {
  344. case ".gif":
  345. $filedata['type'] = "image/gif";
  346. break;
  347. case ".gz":
  348. $filedata['type'] = "application/x-gzip";
  349. break;
  350. case ".html":
  351. case ".htm":
  352. $filedata['type'] = "text/html";
  353. break;
  354. case ".jpg":
  355. $filedata['type'] = "image/jpeg";
  356. break;
  357. case ".tar":
  358. $filedata['type'] = "application/x-tar";
  359. break;
  360. case ".txt":
  361. $filedata['type'] = "text/plain";
  362. break;
  363. case ".zip":
  364. $filedata['type'] = "application/zip";
  365. break;
  366. default:
  367. $filedata['type'] = "application/octet-stream";
  368. break;
  369. }
  370. return $filedata;
  371. }
  372. } // end class
  373. ?>