UMailer.php 14 KB

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