UMailer.php 14 KB

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