Smtp.php 8.5 KB

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