Email.php 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace email;
  3. /**
  4. * 邮件发送类
  5. */
  6. class Email {
  7. /* Public Variables */
  8. public $smtp_port;
  9. public $time_out;
  10. public $host_name;
  11. public $log_file;
  12. public $relay_host;
  13. public $debug;
  14. public $auth;
  15. public $user;
  16. public $pass;
  17. /* Private Variables */
  18. private $sock;
  19. /* Constractor */
  20. function __construct($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
  21. {
  22. $this->debug = FALSE;
  23. $this->smtp_port = $smtp_port;
  24. $this->relay_host = $relay_host;
  25. $this->time_out = 30; //is used in fsockopen()
  26. $this->auth = $auth;//auth
  27. $this->user = $user;
  28. $this->pass = $pass;
  29. $this->host_name = "localhost"; //is used in HELO command
  30. $this->log_file = "";
  31. $this->sock = FALSE;
  32. }
  33. /* Main Function */
  34. function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
  35. {
  36. $mail_from = $this->get_address($this->strip_comment($from));
  37. $body = preg_replace("/(^|(\r\n))(\.)/", "\1.\3", $body);
  38. $header = "MIME-Version:1.0\r\n";
  39. if($mailtype=="HTML"){
  40. $header .= "Content-Type:text/html\r\n";
  41. }
  42. $header .= "To: ".$to."\r\n";
  43. if ($cc != "") {
  44. $header .= "Cc: ".$cc."\r\n";
  45. }
  46. $header .= "From: $from<".$from.">\r\n";
  47. $header .= "Subject: ".$subject."\r\n";
  48. $header .= $additional_headers;
  49. $header .= "Date: ".date("r")."\r\n";
  50. $header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n";
  51. list($msec, $sec) = explode(" ", microtime());
  52. $header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n";
  53. $TO = explode(",", $this->strip_comment($to));
  54. if ($cc != "") {
  55. $TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
  56. }
  57. if ($bcc != "") {
  58. $TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
  59. }
  60. $sent = TRUE;
  61. foreach ($TO as $rcpt_to) {
  62. $rcpt_to = $this->get_address($rcpt_to);
  63. if (!$this->smtp_sockopen($rcpt_to)) {
  64. $this->log_write("Error: Cannot send email to ".$rcpt_to."\n");
  65. $sent = FALSE;
  66. continue;
  67. }
  68. if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
  69. $this->log_write("E-mail has been sent to <".$rcpt_to.">\n");
  70. } else {
  71. $this->log_write("Error: Cannot send email to <".$rcpt_to.">\n");
  72. $sent = FALSE;
  73. }
  74. fclose($this->sock);
  75. $this->log_write("Disconnected from remote host\n");
  76. }
  77. return $sent;
  78. }
  79. /* Private Functions */
  80. function smtp_send($helo, $from, $to, $header, $body = "")
  81. {
  82. if (!$this->smtp_putcmd("HELO", $helo)) {
  83. return $this->smtp_error("sending HELO command");
  84. }
  85. //auth
  86. if($this->auth){
  87. if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
  88. return $this->smtp_error("sending HELO command");
  89. }
  90. if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
  91. return $this->smtp_error("sending HELO command");
  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. 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. 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. function smtp_sockopen_mx($address)
  135. {
  136. $domain = preg_replace("/^.+@([^@]+)$/", "\1", $address);
  137. if (!@getmxrr($domain, $MXHOSTS)) {
  138. $this->log_write("Error: Cannot resolve MX \"".$domain."\"\n");
  139. return FALSE;
  140. }
  141. //专注与php学习 http://www.daixiaorui.com 欢迎您的访问
  142. foreach ($MXHOSTS as $host) {
  143. $this->log_write("Trying to ".$host.":".$this->smtp_port."\n");
  144. $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
  145. if (!($this->sock && $this->smtp_ok())) {
  146. $this->log_write("Warning: Cannot connect to mx host ".$host."\n");
  147. $this->log_write("Error: ".$errstr." (".$errno.")\n");
  148. continue;
  149. }
  150. $this->log_write("Connected to mx host ".$host."\n");
  151. return TRUE;
  152. }
  153. $this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n");
  154. return FALSE;
  155. }
  156. function smtp_message($header, $body)
  157. {
  158. fputs($this->sock, $header."\r\n".$body);
  159. $this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> "));
  160. return TRUE;
  161. }
  162. function smtp_eom()
  163. {
  164. fputs($this->sock, "\r\n.\r\n");
  165. $this->smtp_debug(". [EOM]\n");
  166. return $this->smtp_ok();
  167. }
  168. function smtp_ok()
  169. {
  170. $response = str_replace("\r\n", "", fgets($this->sock, 512));
  171. $this->smtp_debug($response."\n");
  172. if (!preg_match("/^[23]/", $response)) {
  173. fputs($this->sock, "QUIT\r\n");
  174. fgets($this->sock, 512);
  175. $this->log_write("Error: Remote host returned \"".$response."\"\n");
  176. return FALSE;
  177. }
  178. return TRUE;
  179. }
  180. function smtp_putcmd($cmd, $arg = "")
  181. {
  182. if ($arg != "") {
  183. if($cmd=="") $cmd = $arg;
  184. else $cmd = $cmd." ".$arg;
  185. }
  186. fputs($this->sock, $cmd."\r\n");
  187. $this->smtp_debug("> ".$cmd."\n");
  188. return $this->smtp_ok();
  189. }
  190. function smtp_error($string)
  191. {
  192. $this->log_write("Error: Error occurred while ".$string.".\n");
  193. return FALSE;
  194. }
  195. function log_write($message)
  196. {
  197. $this->smtp_debug($message);
  198. if ($this->log_file == "") {
  199. return TRUE;
  200. }
  201. $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
  202. if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
  203. $this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n");
  204. return FALSE;
  205. }
  206. flock($fp, LOCK_EX);
  207. fputs($fp, $message);
  208. fclose($fp);
  209. return TRUE;
  210. }
  211. function strip_comment($address)
  212. {
  213. $comment = "/\([^()]*\)/";
  214. while (preg_match($comment, $address)) {
  215. $address = preg_replace($comment, "", $address);
  216. }
  217. return $address;
  218. }
  219. function get_address($address)
  220. {
  221. $address = preg_replace("/([ \t\r\n])+/", "", $address);
  222. $address = preg_replace("/^.*<(.+)>.*$/", "\1", $address);
  223. return $address;
  224. }
  225. function smtp_debug($message)
  226. {
  227. if ($this->debug) {
  228. echo $message;
  229. }
  230. }
  231. }