PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/upload/include/smtp.php

https://github.com/bluelovers/Bug-Tracker
PHP | 314 lines | 218 code | 59 blank | 37 comment | 51 complexity | 2b12a176b4c5862f8d44f2f4fa3d585d MD5 | raw file
  1. <?php
  2. class smtp
  3. {
  4. /* Public Variables */
  5. var $smtp_port;
  6. var $time_out;
  7. var $host_name;
  8. var $log_file;
  9. var $relay_host;
  10. var $debug;
  11. var $auth;
  12. var $user;
  13. var $pass;
  14. /* Private Variables */
  15. var $sock;
  16. /* Constractor */
  17. function smtp($relay_host = "", $smtp_port = 25, $auth = false, $user, $pass)
  18. {
  19. $this->debug = FALSE;
  20. $this->smtp_port = $smtp_port;
  21. $this->relay_host = $relay_host;
  22. $this->time_out = 30; //is used in fsockopen()
  23. $this->auth = $auth;//auth
  24. $this->user = $user;
  25. $this->pass = $pass;
  26. /* Use localhost in the host_name would reject by gmail. */
  27. $this->host_name = $_SERVER['SERVER_NAME']; //is used in HELO command
  28. if ($this->host_name == "") {
  29. $this->host_name = $_SERVER['HOST']?$_SERVER['HOST']:"localhost.com";
  30. }
  31. $this->log_file = "";
  32. $this->sock = FALSE;
  33. }
  34. /* Main Function */
  35. function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
  36. {
  37. if (strstr($from, "<")) {
  38. preg_match("/(.*)<(.*)>/", $from, $matches);
  39. $name = trim($matches[1]);
  40. $email = trim($matches[2]);
  41. if ($email == "") {
  42. $email = $from;
  43. }
  44. if ($name == "") {
  45. $name = $from;
  46. }
  47. } else {
  48. $email = $from;
  49. $name = $from;
  50. }
  51. $body = str_replace("\r\n.", "\r\n..", $body);
  52. $header = "MIME-Version:1.0\r\n";
  53. if($mailtype == "HTML"){
  54. $header .= "Content-Type:text/html; charset=utf-8\r\n";
  55. }
  56. $header .= "To: ".$to."\r\n";
  57. if ($cc != "") {
  58. $header .= "Cc: ".$cc."\r\n";
  59. }
  60. $header .= "From: $name<".$email.">\r\n";
  61. $header .= "Subject: ".$subject."\r\n";
  62. $header .= $additional_headers;
  63. $header .= "Date: ".date("r")."\r\n";
  64. $header .= "X-Mailer:By Bug Tracker (PHP/".phpversion().")\r\n";
  65. list($msec, $sec) = explode(" ", microtime());
  66. $header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$email.">\r\n";
  67. $TO = explode(",", $to);
  68. if ($cc != "") {
  69. $TO = array_merge($TO, explode(",", $cc));
  70. }
  71. if ($bcc != "") {
  72. $TO = array_merge($TO, explode(",", $bcc));
  73. }
  74. if (!$this->smtp_sockopen()) {
  75. $this->log_write("Error: Failed to open socket.\n");
  76. return FALSE;
  77. }
  78. if (!$this->smtp_send($this->host_name, $email, $TO, $header, $body)) {
  79. $this->log_write("Error: Cannot send email to <".$rcpt_to.">\n");
  80. fclose($this->sock);
  81. return FALSE;
  82. }
  83. fclose($this->sock);
  84. $this->log_write("Disconnected from remote host\n");
  85. return TRUE;
  86. }
  87. /* Private Functions */
  88. function smtp_send($helo, $from, $to_array, $header, $body = "")
  89. {
  90. if ($this->auth) {
  91. if (!$this->smtp_putcmd("EHLO", $helo)) {
  92. return $this->smtp_error("sending HELO command");
  93. }
  94. } else {
  95. if (!$this->smtp_putcmd("HELO", $helo)) {
  96. return $this->smtp_error("sending HELO command");
  97. }
  98. }
  99. #auth
  100. if($this->auth){
  101. if (!$this->smtp_putcmd("AUTH LOGIN", "")) {
  102. return $this->smtp_error("sending AUTH LOGIN ");
  103. }
  104. if (!$this->smtp_putcmd("", base64_encode($this->user))) {
  105. return $this->smtp_error("sending AUTH user command");
  106. }
  107. if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
  108. return $this->smtp_error("sending ATUH password command");
  109. }
  110. }
  111. if (!$this->smtp_putcmd("MAIL", "From: <".$from.">")) {
  112. return $this->smtp_error("sending MAIL FROM command");
  113. }
  114. foreach ($to_array as $rcpt_to) {
  115. $rcpt_to = $this->get_address($rcpt_to);
  116. if ($rcpt_to == "") {
  117. continue;
  118. }
  119. if (!$this->smtp_putcmd("RCPT", "To: <".$rcpt_to.">")) {
  120. return $this->smtp_error("sending RCPT TO command");
  121. }
  122. }
  123. if (!$this->smtp_putcmd("DATA")) {
  124. return $this->smtp_error("sending DATA command");
  125. }
  126. if (!$this->smtp_message($header, $body)) {
  127. return $this->smtp_error("sending message");
  128. }
  129. if (!$this->smtp_eom()) {
  130. return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
  131. }
  132. if (!$this->smtp_putcmd("QUIT")) {
  133. return $this->smtp_error("sending QUIT command");
  134. }
  135. return TRUE;
  136. }
  137. function smtp_sockopen()
  138. {
  139. $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n");
  140. $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
  141. if (!($this->sock && $this->smtp_ok())) {
  142. $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n");
  143. $this->log_write("Error: ".$errstr." (".$errno.")\n");
  144. return FALSE;
  145. }
  146. $this->log_write("Connected to relay host ".$this->relay_host."\n");
  147. return TRUE;
  148. }
  149. /*
  150. function smtp_sockopen_mx($address)
  151. {
  152. $domain = ereg_replace("^.+@([^@]+)$", "\1", $address);
  153. if (!@getmxrr($domain, $MXHOSTS)) {
  154. $this->log_write("Error: Cannot resolve MX \"".$domain."\"\n");
  155. return FALSE;
  156. }
  157. foreach ($MXHOSTS as $host) {
  158. $this->log_write("Trying to ".$host.":".$this->smtp_port."\n");
  159. $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
  160. if (!($this->sock && $this->smtp_ok())) {
  161. $this->log_write("Warning: Cannot connect to mx host ".$host."\n");
  162. $this->log_write("Error: ".$errstr." (".$errno.")\n");
  163. continue;
  164. }
  165. $this->log_write("Connected to mx host ".$host."\n");
  166. return TRUE;
  167. }
  168. $this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n");
  169. return FALSE;
  170. }
  171. */
  172. function smtp_message($header, $body)
  173. {
  174. fputs($this->sock, $header."\r\n".$body);
  175. $this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> "));
  176. return TRUE;
  177. }
  178. function smtp_eom()
  179. {
  180. fputs($this->sock, "\r\n.\r\n");
  181. $this->smtp_debug(". [EOM]\n");
  182. return $this->smtp_ok();
  183. }
  184. function smtp_ok()
  185. {
  186. $sock = array($this->sock);
  187. // create a copy, so $sock doesn't get modified by stream_select()
  188. $read_sock = $sock;
  189. while (0 < stream_select($read_sock, $w = NULL, $e = NULL, 20)) {
  190. $response = str_replace("\r\n", "", fgets($this->sock, 512));
  191. $this->smtp_debug($response."\n");
  192. // Skip the multiple 250-XXXX lines introduced by EHLO
  193. if (strncmp($response, "250-", 4) == 0) continue;
  194. if (!preg_match("/^[23]/", $response)) {
  195. fputs($this->sock, "QUIT\r\n");
  196. fgets($this->sock, 512);
  197. $this->log_write("Error: Remote host returned \"".$response."\"\n");
  198. return FALSE;
  199. }
  200. return TRUE;
  201. }
  202. // Never reached
  203. return FALSE;
  204. }
  205. function smtp_putcmd($cmd, $arg = "")
  206. {
  207. if ($arg != "") {
  208. if($cmd=="") $cmd = $arg;
  209. else $cmd = $cmd." ".$arg;
  210. }
  211. fputs($this->sock, $cmd."\r\n");
  212. $this->smtp_debug("> ".$cmd."\n");
  213. return $this->smtp_ok();
  214. }
  215. function smtp_error($string)
  216. {
  217. $this->log_write("Error: Error occurred while ".$string.".\n");
  218. return FALSE;
  219. }
  220. function log_write($message)
  221. {
  222. $this->smtp_debug($message);
  223. if ($this->log_file == "") {
  224. return TRUE;
  225. }
  226. $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
  227. if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
  228. $this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n");
  229. return FALSE;;
  230. }
  231. flock($fp, LOCK_EX);
  232. fputs($fp, $message);
  233. fclose($fp);
  234. return TRUE;
  235. }
  236. function get_address($address)
  237. {
  238. if (strstr($address, "<")) {
  239. preg_match("/<(.*)>/", $address, $matches);
  240. $address = $matches[1];
  241. }
  242. return $address;
  243. }
  244. function smtp_debug($message)
  245. {
  246. if ($this->debug) {
  247. echo htmlspecialchars($message)."<br>";
  248. }
  249. }
  250. }
  251. ?>