email.init.php 8.7 KB

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