You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

453 lines
8.6 KiB

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