smtp.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <?php
  2. /**
  3. * 邮件服务
  4. */
  5. namespace app\Lib\smtpMail;
  6. class smtp
  7. {
  8. public $smtp_port;
  9. public $time_out;
  10. public $host_name;
  11. public $log_file;
  12. public $relay_host;
  13. public $debug;
  14. public $auth;
  15. public $user;
  16. public $pass;
  17. public $sock;
  18. /**
  19. * smtp constructor.
  20. * @param string $relay_host
  21. * @param int $smtp_port
  22. * @param false $auth
  23. * @param $user
  24. * @param $pass
  25. */
  26. public function __construct($relay_host = "", $smtp_port = 25, $auth = false, $user, $pass)
  27. {
  28. $this->debug = FALSE;
  29. $this->smtp_port = $smtp_port;
  30. $this->relay_host = $relay_host;
  31. $this->time_out = 30; //is used in fsockopen()
  32. #
  33. $this->auth = $auth;//auth
  34. $this->user = $user;
  35. $this->pass = $pass;
  36. #
  37. $this->host_name = "localhost"; //is used in HELO command
  38. $this->log_file = "";
  39. $this->sock = FALSE;
  40. }
  41. /**
  42. * @param $to
  43. * @param $from
  44. * @param string $subject
  45. * @param string $body
  46. * @param $mailtype
  47. * @param string $cc
  48. * @param string $bcc
  49. * @param string $additional_headers
  50. * @return bool
  51. */
  52. public function sendmail($to, $from, $subject = "", $body = "", $mailtype='html', $cc = "", $bcc = "", $additional_headers = "")
  53. {
  54. $header = '';
  55. $mail_from = $this->get_address($this->strip_comment($from));
  56. $subject = "=?GB2312?B?" . base64_encode($subject) . "?=";
  57. $body = preg_replace('/(^|(\r\n))(\\.)/', "\\1.\\3", $body);
  58. $header .= "MIME-Version:1.0\r\n";
  59. if ($mailtype == "HTML") {
  60. $header .= "Content-Type:text/html;charset=utf8\r\n";
  61. } else {
  62. $header .= "Content-Type:text/plain;charset=utf8\r\n";
  63. }
  64. $header .= "To: " . $to . "\r\n";
  65. if ($cc != "") {
  66. $header .= "Cc: " . $cc . "\r\n";
  67. }
  68. $header .= "From: $from<" . $from . ">\r\n";
  69. $header .= "Subject: " . $subject . "\r\n";
  70. $header .= $additional_headers;
  71. $header .= "Date: " . date("r") . "\r\n";
  72. $header .= "X-Mailer:By Redhat (PHP/" . phpversion() . ")\r\n";
  73. list($msec, $sec) = explode(" ", microtime());
  74. $header .= "Message-ID: <" . date("YmdHis", $sec) . "." . ($msec * 1000000) . "." . $mail_from . ">\r\n";
  75. $TO = explode(",", $this->strip_comment($to));
  76. if ($cc != "") {
  77. $TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
  78. }
  79. if ($bcc != "") {
  80. $TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
  81. }
  82. $sent = TRUE;
  83. foreach ($TO as $rcpt_to) {
  84. $rcpt_to = $this->get_address($rcpt_to);
  85. if (!$this->smtp_sockopen($rcpt_to)) {
  86. $this->log_write("Error: Cannot send email to " . $rcpt_to . "\n");
  87. $sent = FALSE;
  88. continue;
  89. }
  90. if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
  91. $this->log_write("E-mail has been sent to <" . $rcpt_to . ">\n");
  92. } else {
  93. $this->log_write("Error: Cannot send email to <" . $rcpt_to . ">\n");
  94. $sent = FALSE;
  95. }
  96. fclose($this->sock);
  97. $this->log_write("Disconnected from remote host\n");
  98. }
  99. //echo "<br>";
  100. //echo $header;
  101. return $sent;
  102. }
  103. /**
  104. * hello 测试
  105. * @param $helo
  106. * @param $from
  107. * @param $to
  108. * @param $header
  109. * @param string $body
  110. * @return bool
  111. */
  112. public function smtp_send($helo, $from, $to, $header, $body = "")
  113. {
  114. if (!$this->smtp_putcmd("HELO", $helo)) {
  115. return $this->smtp_error("sending HELO command");
  116. }
  117. #auth
  118. if ($this->auth) {
  119. if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
  120. return $this->smtp_error("sending HELO command");
  121. }
  122. if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
  123. return $this->smtp_error("sending HELO command");
  124. }
  125. }
  126. #
  127. if (!$this->smtp_putcmd("MAIL", "FROM:<" . $from . ">")) {
  128. return $this->smtp_error("sending MAIL FROM command");
  129. }
  130. if (!$this->smtp_putcmd("RCPT", "TO:<" . $to . ">")) {
  131. return $this->smtp_error("sending RCPT TO command");
  132. }
  133. if (!$this->smtp_putcmd("DATA")) {
  134. return $this->smtp_error("sending DATA command");
  135. }
  136. if (!$this->smtp_message($header, $body)) {
  137. return $this->smtp_error("sending message");
  138. }
  139. if (!$this->smtp_eom()) {
  140. return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
  141. }
  142. if (!$this->smtp_putcmd("QUIT")) {
  143. return $this->smtp_error("sending QUIT command");
  144. }
  145. return TRUE;
  146. }
  147. /**
  148. * sockopen 方式
  149. * @param $address
  150. * @return bool
  151. */
  152. public function smtp_sockopen($address)
  153. {
  154. if ($this->relay_host == "") {
  155. return $this->smtp_sockopen_mx($address);
  156. } else {
  157. return $this->smtp_sockopen_relay();
  158. }
  159. }
  160. /**
  161. * @return bool
  162. */
  163. public function smtp_sockopen_relay()
  164. {
  165. $this->log_write("Trying to " . $this->relay_host . ":" . $this->smtp_port . "\n");
  166. $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
  167. if (!($this->sock && $this->smtp_ok())) {
  168. $this->log_write("Error: Cannot connenct to relay host " . $this->relay_host . "\n");
  169. $this->log_write("Error: " . $errstr . " (" . $errno . ")\n");
  170. return FALSE;
  171. }
  172. $this->log_write("Connected to relay host " . $this->relay_host . "\n");
  173. return TRUE;;
  174. }
  175. /**
  176. * @param $address
  177. * @return bool
  178. */
  179. public function smtp_sockopen_mx($address)
  180. {
  181. $domain = preg_replace("/^.+@([^@]+)$/", "\\1", $address);
  182. if (!@getmxrr($domain, $MXHOSTS)) {
  183. $this->log_write("Error: Cannot resolve MX \"" . $domain . "\"\n");
  184. return FALSE;
  185. }
  186. foreach ($MXHOSTS as $host) {
  187. $this->log_write("Trying to " . $host . ":" . $this->smtp_port . "\n");
  188. $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
  189. if (!($this->sock && $this->smtp_ok())) {
  190. $this->log_write("Warning: Cannot connect to mx host " . $host . "\n");
  191. $this->log_write("Error: " . $errstr . " (" . $errno . ")\n");
  192. continue;
  193. }
  194. $this->log_write("Connected to mx host " . $host . "\n");
  195. return TRUE;
  196. }
  197. $this->log_write("Error: Cannot connect to any mx hosts (" . implode(", ", $MXHOSTS) . ")\n");
  198. return FALSE;
  199. }
  200. /**
  201. * @param $header
  202. * @param $body
  203. * @return bool
  204. */
  205. public 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. /**
  212. * @return bool
  213. */
  214. public function smtp_eom()
  215. {
  216. fputs($this->sock, "\r\n.\r\n");
  217. $this->smtp_debug(". [EOM]\n");
  218. return $this->smtp_ok();
  219. }
  220. /**
  221. * @return bool
  222. */
  223. public function smtp_ok()
  224. {
  225. $response = str_replace("\r\n", "", fgets($this->sock, 512));
  226. $this->smtp_debug($response . "\n");
  227. if (!preg_match("/^[23]/", $response)) {
  228. fputs($this->sock, "QUIT\r\n");
  229. fgets($this->sock, 512);
  230. $this->log_write("Error: Remote host returned \"" . $response . "\"\n");
  231. return FALSE;
  232. }
  233. return TRUE;
  234. }
  235. /**
  236. * @param $cmd
  237. * @param string $arg
  238. * @return bool
  239. */
  240. public function smtp_putcmd($cmd, $arg = "")
  241. {
  242. if ($arg != "") {
  243. if ($cmd == "") $cmd = $arg;
  244. else $cmd = $cmd . " " . $arg;
  245. }
  246. fputs($this->sock, $cmd . "\r\n");
  247. $this->smtp_debug("> " . $cmd . "\n");
  248. return $this->smtp_ok();
  249. }
  250. /**
  251. * @param $string
  252. * @return false
  253. */
  254. public function smtp_error($string)
  255. {
  256. $this->log_write("Error: Error occurred while " . $string . ".\n");
  257. return FALSE;
  258. }
  259. /**
  260. * @param $message
  261. * @return bool
  262. */
  263. function log_write($message)
  264. {
  265. $this->smtp_debug($message);
  266. if ($this->log_file == "") {
  267. return TRUE;
  268. }
  269. $message = date("M d H:i:s ") . get_current_user() . "[" . getmypid() . "]: " . $message;
  270. if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
  271. $this->smtp_debug("Warning: Cannot open log file \"" . $this->log_file . "\"\n");
  272. return FALSE;
  273. }
  274. flock($fp, LOCK_EX);
  275. fputs($fp, $message);
  276. fclose($fp);
  277. return TRUE;
  278. }
  279. /**
  280. * @param $address
  281. * @return string
  282. */
  283. public function strip_comment($address)
  284. {
  285. $comment = "\\([^()]*\\)";
  286. while (preg_match($comment, $address)) {
  287. $address = preg_replace($comment, "", $address);
  288. }
  289. return $address;
  290. }
  291. /**
  292. * @param $address
  293. * @return string
  294. */
  295. public function get_address($address)
  296. {
  297. $address = preg_replace("/([ \t\r\n])+/", "", $address);
  298. $address = preg_replace("/^.*<(.+)>.*$/", "\\1", $address);
  299. return $address;
  300. }
  301. /**
  302. * @param $message
  303. */
  304. public function smtp_debug($message)
  305. {
  306. if ($this->debug) {
  307. echo $message . "<br>";
  308. }
  309. }
  310. /**
  311. * @param $image_tag
  312. * @return array
  313. */
  314. public function get_attach_type($image_tag)
  315. { //
  316. $filedata = array();
  317. $img_file_con = fopen($image_tag, "r");
  318. unset($image_data);
  319. while ($tem_buffer = AddSlashes(fread($img_file_con, filesize($image_tag))))
  320. $image_data .= $tem_buffer;
  321. fclose($img_file_con);
  322. $filedata['context'] = $image_data;
  323. $filedata['filename'] = basename($image_tag);
  324. $extension = substr($image_tag, strrpos($image_tag, "."), strlen($image_tag) - strrpos($image_tag, "."));
  325. switch ($extension) {
  326. case ".gif":
  327. $filedata['type'] = "image/gif";
  328. break;
  329. case ".gz":
  330. $filedata['type'] = "application/x-gzip";
  331. break;
  332. case ".htm":
  333. $filedata['type'] = "text/html";
  334. break;
  335. case ".html":
  336. $filedata['type'] = "text/html";
  337. break;
  338. case ".jpg":
  339. $filedata['type'] = "image/jpeg";
  340. break;
  341. case ".tar":
  342. $filedata['type'] = "application/x-tar";
  343. break;
  344. case ".txt":
  345. $filedata['type'] = "text/plain";
  346. break;
  347. case ".zip":
  348. $filedata['type'] = "application/zip";
  349. break;
  350. default:
  351. $filedata['type'] = "application/octet-stream";
  352. break;
  353. }
  354. return $filedata;
  355. }
  356. }
  357. ?>