email.init.php 10 KB

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