UMailer.php 14 KB

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