Smtp.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. <?php
  2. namespace app\common\util;
  3. class Smtp
  4. {
  5. /* Public Variables */
  6. public $smtp_port;
  7. public $time_out;
  8. public $host_name;
  9. public $log_file;
  10. public $relay_host;
  11. public $debug;
  12. public $auth;
  13. public $user;
  14. public $pass;
  15. /* Private Variables */
  16. private $sock;
  17. /* Constractor */
  18. function __construct($relay_host = "", $smtp_port = 25, $auth = false, $user, $pass)
  19. {
  20. $this->debug = FALSE;
  21. $this->smtp_port = $smtp_port;
  22. $this->relay_host = $relay_host;
  23. $this->time_out = 30; //is used in fsockopen()
  24. $this->auth = $auth;//auth
  25. $this->user = $user;
  26. $this->pass = $pass;
  27. $this->host_name = "localhost"; //is used in HELO command
  28. $this->log_file = "";
  29. $this->sock = FALSE;
  30. }
  31. /* Main Function */
  32. function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
  33. {
  34. $mail_from = $this->get_address($this->strip_comment($from));
  35. $body = preg_replace("/(^|(\r\n))(\.)/", "\1.\3", $body);
  36. $header = "MIME-Version:1.0\r\n";
  37. if ($mailtype == "HTML") {
  38. $header .= "Content-Type:text/html\r\n";
  39. }
  40. $header .= "To: " . $to . "\r\n";
  41. if ($cc != "") {
  42. $header .= "Cc: " . $cc . "\r\n";
  43. }
  44. $header .= "From: $from<" . $from . ">\r\n";
  45. $header .= "Subject: " . $subject . "\r\n";
  46. $header .= $additional_headers;
  47. $header .= "Date: " . date("r") . "\r\n";
  48. $header .= "X-Mailer:By Redhat (PHP/" . phpversion() . ")\r\n";
  49. list($msec, $sec) = explode(" ", microtime());
  50. $header .= "Message-ID: <" . date("YmdHis", $sec) . "." . ($msec * 1000000) . "." . $mail_from . ">\r\n";
  51. $TO = explode(",", $this->strip_comment($to));
  52. if ($cc != "") {
  53. $TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
  54. }
  55. if ($bcc != "") {
  56. $TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
  57. }
  58. $sent = TRUE;
  59. foreach ($TO as $rcpt_to) {
  60. $rcpt_to = $this->get_address($rcpt_to);
  61. if (!$this->smtp_sockopen($rcpt_to)) {
  62. $this->log_write("Error: Cannot send email to " . $rcpt_to . "\n");
  63. $sent = FALSE;
  64. continue;
  65. }
  66. if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
  67. $this->log_write("E-mail has been sent to <" . $rcpt_to . ">\n");
  68. } else {
  69. $this->log_write("Error: Cannot send email to <" . $rcpt_to . ">\n");
  70. $sent = FALSE;
  71. }
  72. fclose($this->sock);
  73. $this->log_write("Disconnected from remote host\n");
  74. }
  75. return $sent;
  76. }
  77. /* Private Functions */
  78. function smtp_send($helo, $from, $to, $header, $body = "")
  79. {
  80. if (!$this->smtp_putcmd("HELO", $helo)) {
  81. return $this->smtp_error("sending HELO command");
  82. }
  83. //auth
  84. if ($this->auth) {
  85. if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
  86. return $this->smtp_error("sending HELO command");
  87. }
  88. if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
  89. return $this->smtp_error("sending HELO command");
  90. }
  91. }
  92. if (!$this->smtp_putcmd("MAIL", "FROM:<" . $from . ">")) {
  93. return $this->smtp_error("sending MAIL FROM command");
  94. }
  95. if (!$this->smtp_putcmd("RCPT", "TO:<" . $to . ">")) {
  96. return $this->smtp_error("sending RCPT TO command");
  97. }
  98. if (!$this->smtp_putcmd("DATA")) {
  99. return $this->smtp_error("sending DATA command");
  100. }
  101. if (!$this->smtp_message($header, $body)) {
  102. return $this->smtp_error("sending message");
  103. }
  104. if (!$this->smtp_eom()) {
  105. return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
  106. }
  107. if (!$this->smtp_putcmd("QUIT")) {
  108. return $this->smtp_error("sending QUIT command");
  109. }
  110. return TRUE;
  111. }
  112. function smtp_sockopen($address)
  113. {
  114. if ($this->relay_host == "") {
  115. return $this->smtp_sockopen_mx($address);
  116. } else {
  117. return $this->smtp_sockopen_relay();
  118. }
  119. }
  120. function smtp_sockopen_relay()
  121. {
  122. $this->log_write("Trying to " . $this->relay_host . ":" . $this->smtp_port . "\n");
  123. $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
  124. if (!($this->sock && $this->smtp_ok())) {
  125. $this->log_write("Error: Cannot connenct to relay host " . $this->relay_host . "\n");
  126. $this->log_write("Error: " . $errstr . " (" . $errno . ")\n");
  127. return FALSE;
  128. }
  129. $this->log_write("Connected to relay host " . $this->relay_host . "\n");
  130. return TRUE;
  131. }
  132. function smtp_sockopen_mx($address)
  133. {
  134. $domain = preg_replace("/^.+@([^@]+)$/", "\1", $address);
  135. if (!@getmxrr($domain, $MXHOSTS)) {
  136. $this->log_write("Error: Cannot resolve MX \"" . $domain . "\"\n");
  137. return FALSE;
  138. }
  139. //专注与php学习 http://www.daixiaorui.com 欢迎您的访问
  140. foreach ($MXHOSTS as $host) {
  141. $this->log_write("Trying to " . $host . ":" . $this->smtp_port . "\n");
  142. $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
  143. if (!($this->sock && $this->smtp_ok())) {
  144. $this->log_write("Warning: Cannot connect to mx host " . $host . "\n");
  145. $this->log_write("Error: " . $errstr . " (" . $errno . ")\n");
  146. continue;
  147. }
  148. $this->log_write("Connected to mx host " . $host . "\n");
  149. return TRUE;
  150. }
  151. $this->log_write("Error: Cannot connect to any mx hosts (" . implode(", ", $MXHOSTS) . ")\n");
  152. return FALSE;
  153. }
  154. function smtp_message($header, $body)
  155. {
  156. fputs($this->sock, $header . "\r\n" . $body);
  157. $this->smtp_debug("> " . str_replace("\r\n", "\n" . "> ", $header . "\n> " . $body . "\n> "));
  158. return TRUE;
  159. }
  160. function smtp_eom()
  161. {
  162. fputs($this->sock, "\r\n.\r\n");
  163. $this->smtp_debug(". [EOM]\n");
  164. return $this->smtp_ok();
  165. }
  166. function smtp_ok()
  167. {
  168. $response = str_replace("\r\n", "", fgets($this->sock, 512));
  169. $this->smtp_debug($response . "\n");
  170. if (!preg_match("/^[23]/", $response)) {
  171. fputs($this->sock, "QUIT\r\n");
  172. fgets($this->sock, 512);
  173. $this->log_write("Error: Remote host returned \"" . $response . "\"\n");
  174. return FALSE;
  175. }
  176. return TRUE;
  177. }
  178. function smtp_putcmd($cmd, $arg = "")
  179. {
  180. if ($arg != "") {
  181. if ($cmd == "") $cmd = $arg;
  182. else $cmd = $cmd . " " . $arg;
  183. }
  184. fputs($this->sock, $cmd . "\r\n");
  185. $this->smtp_debug("> " . $cmd . "\n");
  186. return $this->smtp_ok();
  187. }
  188. function smtp_error($string)
  189. {
  190. $this->log_write("Error: Error occurred while " . $string . ".\n");
  191. return FALSE;
  192. }
  193. function log_write($message)
  194. {
  195. $this->smtp_debug($message);
  196. if ($this->log_file == "") {
  197. return TRUE;
  198. }
  199. $message = date("M d H:i:s ") . get_current_user() . "[" . getmypid() . "]: " . $message;
  200. if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
  201. $this->smtp_debug("Warning: Cannot open log file \"" . $this->log_file . "\"\n");
  202. return FALSE;
  203. }
  204. flock($fp, LOCK_EX);
  205. fputs($fp, $message);
  206. fclose($fp);
  207. return TRUE;
  208. }
  209. function strip_comment($address)
  210. {
  211. $comment = "/\([^()]*\)/";
  212. while (preg_match($comment, $address)) {
  213. $address = preg_replace($comment, "", $address);
  214. }
  215. return $address;
  216. }
  217. function get_address($address)
  218. {
  219. $address = preg_replace("/([ \t\r\n])+/", "", $address);
  220. $address = preg_replace("/^.*<(.+)>.*$/", "\1", $address);
  221. return $address;
  222. }
  223. function smtp_debug($message)
  224. {
  225. if ($this->debug) {
  226. echo $message;
  227. }
  228. }
  229. }