email.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. class smtp
  3. {
  4. /* Public Variables */
  5. public $smtp_port;
  6. public $time_out;
  7. public $host_name;
  8. public $log_file;
  9. public $relay_host;
  10. public $debug;
  11. public $auth;
  12. public $user;
  13. public $pass;
  14. /* Private Variables */
  15. private $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. #
  24. $this->auth = $auth;//auth
  25. $this->user = $user;
  26. $this->pass = $pass;
  27. #
  28. $this->host_name = "localhost"; //is used in HELO command
  29. $this->log_file = "";
  30. $this->sock = FALSE;
  31. }
  32. /* Main Function */
  33. function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
  34. {
  35. $mail_from = $this->get_address($this->strip_comment($from));
  36. $body = preg_replace("/(^|(\r\n))(\\.)/", "\\1.\\3", $body);
  37. $header = "MIME-Version:1.0\r\n";
  38. if ($mailtype == "HTML") {
  39. $header .= "Content-Type:text/html\r\n";
  40. }
  41. $header .= "To: " . $to . "\r\n";
  42. if ($cc != "") {
  43. $header .= "Cc: " . $cc . "\r\n";
  44. }
  45. $header .= "From: $from<" . $from . ">\r\n";
  46. $header .= "Subject: " . $subject . "\r\n";
  47. $header .= $additional_headers;
  48. $header .= "Date: " . date("r") . "\r\n";
  49. $header .= "X-Mailer:By Redhat (PHP/" . phpversion() . ")\r\n";
  50. list($msec, $sec) = explode(" ", microtime());
  51. $header .= "Message-ID: <" . date("YmdHis", $sec) . "." . ($msec * 1000000) . "." . $mail_from . ">\r\n";
  52. $TO = explode(",", $this->strip_comment($to));
  53. if ($cc != "") {
  54. $TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
  55. }
  56. if ($bcc != "") {
  57. $TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
  58. }
  59. $sent = TRUE;
  60. foreach ($TO as $rcpt_to) {
  61. $rcpt_to = $this->get_address($rcpt_to);
  62. if (!$this->smtp_sockopen($rcpt_to)) {
  63. $this->log_write("Error: Cannot send email to " . $rcpt_to . "\n");
  64. $sent = FALSE;
  65. continue;
  66. }
  67. if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
  68. $this->log_write("E-mail has been sent to <" . $rcpt_to . ">\n");
  69. } else {
  70. $this->log_write("Error: Cannot send email to <" . $rcpt_to . ">\n");
  71. $sent = FALSE;
  72. }
  73. fclose($this->sock);
  74. $this->log_write("Disconnected from remote host\n");
  75. }
  76. //echo "<br>";
  77. //echo $header;
  78. return $sent;
  79. }
  80. /* Private Functions */
  81. function smtp_send($helo, $from, $to, $header, $body = "")
  82. {
  83. if (!$this->smtp_putcmd("HELO", $helo)) {
  84. return $this->smtp_error("sending HELO command");
  85. }
  86. #auth
  87. if ($this->auth) {
  88. if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
  89. return $this->smtp_error("sending HELO command");
  90. }
  91. if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
  92. return $this->smtp_error("sending HELO command");
  93. }
  94. }
  95. #
  96. if (!$this->smtp_putcmd("MAIL", "FROM:<" . $from . ">")) {
  97. return $this->smtp_error("sending MAIL FROM command");
  98. }
  99. if (!$this->smtp_putcmd("RCPT", "TO:<" . $to . ">")) {
  100. return $this->smtp_error("sending RCPT TO command");
  101. }
  102. if (!$this->smtp_putcmd("DATA")) {
  103. return $this->smtp_error("sending DATA command");
  104. }
  105. if (!$this->smtp_message($header, $body)) {
  106. return $this->smtp_error("sending message");
  107. }
  108. if (!$this->smtp_eom()) {
  109. return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
  110. }
  111. if (!$this->smtp_putcmd("QUIT")) {
  112. return $this->smtp_error("sending QUIT command");
  113. }
  114. return TRUE;
  115. }
  116. function smtp_sockopen($address)
  117. {
  118. if ($this->relay_host == "") {
  119. return $this->smtp_sockopen_mx($address);
  120. } else {
  121. return $this->smtp_sockopen_relay();
  122. }
  123. }
  124. function smtp_sockopen_relay()
  125. {
  126. $this->log_write("Trying to " . $this->relay_host . ":" . $this->smtp_port . "\n");
  127. $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
  128. if (!($this->sock && $this->smtp_ok())) {
  129. $this->log_write("Error: Cannot connenct to relay host " . $this->relay_host . "\n");
  130. $this->log_write("Error: " . $errstr . " (" . $errno . ")\n");
  131. return FALSE;
  132. }
  133. $this->log_write("Connected to relay host " . $this->relay_host . "\n");
  134. return TRUE;
  135. }
  136. function smtp_sockopen_mx($address)
  137. {
  138. $domain = preg_replace("/^.+@([^@]+)$/", "\\1", $address);
  139. if (!@getmxrr($domain, $MXHOSTS)) {
  140. $this->log_write("Error: Cannot resolve MX \"" . $domain . "\"\n");
  141. return FALSE;
  142. }
  143. foreach ($MXHOSTS as $host) {
  144. $this->log_write("Trying to " . $host . ":" . $this->smtp_port . "\n");
  145. $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
  146. if (!($this->sock && $this->smtp_ok())) {
  147. $this->log_write("Warning: Cannot connect to mx host " . $host . "\n");
  148. $this->log_write("Error: " . $errstr . " (" . $errno . ")\n");
  149. continue;
  150. }
  151. $this->log_write("Connected to mx host " . $host . "\n");
  152. return TRUE;
  153. }
  154. $this->log_write("Error: Cannot connect to any mx hosts (" . implode(", ", $MXHOSTS) . ")\n");
  155. return FALSE;
  156. }
  157. function smtp_message($header, $body)
  158. {
  159. fputs($this->sock, $header . "\r\n" . $body);
  160. $this->smtp_debug("> " . str_replace("\r\n", "\n" . "> ", $header . "\n> " . $body . "\n> "));
  161. return TRUE;
  162. }
  163. function smtp_eom()
  164. {
  165. fputs($this->sock, "\r\n.\r\n");
  166. $this->smtp_debug(". [EOM]\n");
  167. return $this->smtp_ok();
  168. }
  169. function smtp_ok()
  170. {
  171. $response = str_replace("\r\n", "", fgets($this->sock, 512));
  172. $this->smtp_debug($response . "\n");
  173. if (!preg_match("/^[23]/", $response)) {
  174. fputs($this->sock, "QUIT\r\n");
  175. fgets($this->sock, 512);
  176. $this->log_write("Error: Remote host returned \"" . $response . "\"\n");
  177. return FALSE;
  178. }
  179. return TRUE;
  180. }
  181. function smtp_putcmd($cmd, $arg = "")
  182. {
  183. if ($arg != "") {
  184. if ($cmd == "") $cmd = $arg;
  185. else $cmd = $cmd . " " . $arg;
  186. }
  187. fputs($this->sock, $cmd . "\r\n");
  188. $this->smtp_debug("> " . $cmd . "\n");
  189. return $this->smtp_ok();
  190. }
  191. function smtp_error($string)
  192. {
  193. $this->log_write("Error: Error occurred while " . $string . ".\n");
  194. return FALSE;
  195. }
  196. function log_write($message)
  197. {
  198. $this->smtp_debug($message);
  199. if ($this->log_file == "") {
  200. return TRUE;
  201. }
  202. $message = date("M d H:i:s ") . get_current_user() . "[" . getmypid() . "]: " . $message;
  203. if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
  204. $this->smtp_debug("Warning: Cannot open log file \"" . $this->log_file . "\"\n");
  205. return FALSE;
  206. }
  207. flock($fp, LOCK_EX);
  208. fputs($fp, $message);
  209. fclose($fp);
  210. return TRUE;
  211. }
  212. function strip_comment($address)
  213. {
  214. $comment = "/\\([^()]*\\)/";
  215. while (preg_match($comment, $address)) {
  216. $address = preg_replace($comment, "", $address);
  217. }
  218. return $address;
  219. }
  220. function get_address($address)
  221. {
  222. $address = preg_replace("/([ \t\r\n])+/", "", $address);
  223. $address = preg_replace("/^.*<(.+)>.*$/", "\\1", $address);
  224. return $address;
  225. }
  226. function smtp_debug($message)
  227. {
  228. if ($this->debug) {
  229. echo $message . "<br>";
  230. }
  231. }
  232. function get_attach_type($image_tag)
  233. { //
  234. $filedata = array();
  235. $img_file_con = fopen($image_tag, "r");
  236. unset($image_data);
  237. while ($tem_buffer = AddSlashes(fread($img_file_con, filesize($image_tag))))
  238. $image_data = $tem_buffer;
  239. fclose($img_file_con);
  240. $filedata['context'] = $image_data;
  241. $filedata['filename'] = basename($image_tag);
  242. $extension = substr($image_tag, strrpos($image_tag, "."), strlen($image_tag) - strrpos($image_tag, "."));
  243. switch ($extension) {
  244. case ".gif":
  245. $filedata['type'] = "image/gif";
  246. break;
  247. case ".gz":
  248. $filedata['type'] = "application/x-gzip";
  249. break;
  250. case ".htm":
  251. $filedata['type'] = "text/html";
  252. break;
  253. case ".html":
  254. $filedata['type'] = "text/html";
  255. break;
  256. case ".jpg":
  257. $filedata['type'] = "image/jpeg";
  258. break;
  259. case ".tar":
  260. $filedata['type'] = "application/x-tar";
  261. break;
  262. case ".txt":
  263. $filedata['type'] = "text/plain";
  264. break;
  265. case ".zip":
  266. $filedata['type'] = "application/zip";
  267. break;
  268. default:
  269. $filedata['type'] = "application/octet-stream";
  270. break;
  271. }
  272. return $filedata;
  273. }
  274. } // end class
  275. ?>