国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

290 lines
9.6KB

  1. <?php
  2. /**
  3. * SMTP邮件操作类
  4. *
  5. * @version $Id: mail.class.php 1 15:59 2010年7月5日Z tianya $
  6. * @package DedeBIZ.Libraries
  7. * @copyright Copyright (c) 2020, DedeBIZ.COM
  8. * @license https://www.dedebiz.com/license
  9. * @link https://www.dedebiz.com
  10. */
  11. class smtp
  12. {
  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. // 私有变量
  24. var $sock;
  25. // 析构函数
  26. function smtp($relay_host = "", $smtp_port = 25, $auth = FALSE, $user, $pass)
  27. {
  28. $this->debug = FALSE;
  29. $this->smtp_port = $smtp_port;
  30. $this->relay_host = $relay_host;
  31. //is used in fsockopen()
  32. $this->time_out = 30;
  33. #
  34. $this->auth = $auth; //auth
  35. $this->user = $user;
  36. $this->pass = $pass;
  37. #
  38. //is used in HELO command
  39. $this->host_name = "localhost";
  40. $this->log_file = "";
  41. $this->sock = FALSE;
  42. }
  43. /**
  44. * 邮件主函数
  45. *
  46. * @access public
  47. * @param string $to 发送到的email
  48. * @param string $webname 站点名称
  49. * @param string $from 来自
  50. * @param string $subject 主题
  51. * @param string $body 邮件内容
  52. * @param string $mailtype 邮件类型
  53. * @return string
  54. */
  55. function sendmail($to, $webname, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
  56. {
  57. $mail_from = $this->get_address($this->strip_comment($from));
  58. $body = preg_replace("/(^|(\r\n))(\.)/", "\1.\3", $body);
  59. $header = "MIME-Version:1.0\r\n";
  60. if ($mailtype == "HTML") {
  61. $header .= "Content-Type:text/html\r\n";
  62. }
  63. if ($cc != "") {
  64. $header .= "Cc: " . $cc . "\r\n";
  65. }
  66. $header .= "From: $webname<" . $from . ">\r\n";
  67. $subject = "=?" . $GLOBALS['cfg_soft_lang'] . "?B?" . base64_encode($subject) . "?=";
  68. $header .= "Subject: " . $subject . "\r\n";
  69. $header .= $additional_headers;
  70. $header .= "Date: " . date("r") . "\r\n";
  71. $header .= "X-Mailer:By Redhat (PHP/" . phpversion() . ")\r\n";
  72. list($msec, $sec) = explode(" ", microtime());
  73. $header .= "Message-ID: <" . date("YmdHis", $sec) . "." . ($msec * 1000000) . "." . $mail_from . ">\r\n";
  74. $TO = explode(",", $this->strip_comment($to));
  75. if ($cc != "") {
  76. $TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
  77. }
  78. if ($bcc != "") {
  79. $TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
  80. }
  81. $sent = TRUE;
  82. foreach ($TO as $rcpt_to) {
  83. $headerto = "To: " . $rcpt_to . "\r\n";
  84. $headerall = $header . $headerto;
  85. $rcpt_to = $this->get_address($rcpt_to);
  86. if (!$this->smtp_sockopen($rcpt_to)) {
  87. $this->log_write("Error: Cannot send email to " . $rcpt_to . "\n");
  88. $sent = FALSE;
  89. continue;
  90. }
  91. if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $headerall, $body)) {
  92. $this->log_write("E-mail has been sent to <" . $rcpt_to . ">\n");
  93. } else {
  94. $this->log_write("Error: Cannot send email to <" . $rcpt_to . ">\n");
  95. $sent = FALSE;
  96. }
  97. fclose($this->sock);
  98. $this->log_write("Disconnected from remote host\n");
  99. }
  100. return $sent;
  101. }
  102. /**
  103. * SMTP发送
  104. *
  105. * @access public
  106. * @param string $helo 发送HELO
  107. * @param string $from 来自
  108. * @param string $to 到
  109. * @param string $header 头部信息
  110. * @param string $body 内容主体
  111. * @return string
  112. */
  113. function smtp_send($helo, $from, $to, $header, $body = "")
  114. {
  115. if (!$this->smtp_putcmd("HELO", $helo)) {
  116. return $this->smtp_error("sending HELO command");
  117. }
  118. #auth
  119. if ($this->auth) {
  120. if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
  121. return $this->smtp_error("sending HELO command");
  122. }
  123. if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
  124. return $this->smtp_error("sending HELO command");
  125. }
  126. }
  127. #
  128. if (!$this->smtp_putcmd("MAIL", "FROM:<" . $from . ">")) {
  129. return $this->smtp_error("sending MAIL FROM command");
  130. }
  131. if (!$this->smtp_putcmd("RCPT", "TO:<" . $to . ">")) {
  132. return $this->smtp_error("sending RCPT TO command");
  133. }
  134. if (!$this->smtp_putcmd("DATA")) {
  135. return $this->smtp_error("sending DATA command");
  136. }
  137. if (!$this->smtp_message($header, $body)) {
  138. return $this->smtp_error("sending message");
  139. }
  140. if (!$this->smtp_eom()) {
  141. return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
  142. }
  143. if (!$this->smtp_putcmd("QUIT")) {
  144. return $this->smtp_error("sending QUIT command");
  145. }
  146. return TRUE;
  147. }
  148. function smtp_sockopen($address)
  149. {
  150. if ($this->relay_host == "") {
  151. return $this->smtp_sockopen_mx($address);
  152. } else {
  153. return $this->smtp_sockopen_relay();
  154. }
  155. }
  156. function smtp_sockopen_relay()
  157. {
  158. $this->log_write("Trying to " . $this->relay_host . ":" . $this->smtp_port . "\n");
  159. $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
  160. if (!($this->sock && $this->smtp_ok())) {
  161. $this->log_write("Error: Cannot connenct to relay host " . $this->relay_host . "\n");
  162. $this->log_write("Error: " . $errstr . " (" . $errno . ")\n");
  163. return FALSE;
  164. }
  165. $this->log_write("Connected to relay host " . $this->relay_host . "\n");
  166. return TRUE;;
  167. }
  168. function smtp_sockopen_mx($address)
  169. {
  170. $domain = preg_replace("/^.+@([^@]+)$/i", "\1", $address);
  171. if (!@getmxrr($domain, $MXHOSTS)) {
  172. $this->log_write("Error: Cannot resolve MX \"" . $domain . "\"\n");
  173. return FALSE;
  174. }
  175. foreach ($MXHOSTS as $host) {
  176. $this->log_write("Trying to " . $host . ":" . $this->smtp_port . "\n");
  177. $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
  178. if (!($this->sock && $this->smtp_ok())) {
  179. $this->log_write("Warning: Cannot connect to mx host " . $host . "\n");
  180. $this->log_write("Error: " . $errstr . " (" . $errno . ")\n");
  181. continue;
  182. }
  183. $this->log_write("Connected to mx host " . $host . "\n");
  184. return TRUE;
  185. }
  186. $this->log_write("Error: Cannot connect to any mx hosts (" . implode(", ", $MXHOSTS) . ")\n");
  187. return FALSE;
  188. }
  189. function smtp_message($header, $body)
  190. {
  191. fputs($this->sock, $header . "\r\n" . $body);
  192. $this->smtp_debug("> " . str_replace("\r\n", "\n" . "> ", $header . "\n> " . $body . "\n> "));
  193. return TRUE;
  194. }
  195. function smtp_eom()
  196. {
  197. fputs($this->sock, "\r\n.\r\n");
  198. $this->smtp_debug(". [EOM]\n");
  199. return $this->smtp_ok();
  200. }
  201. function smtp_ok()
  202. {
  203. $response = str_replace("\r\n", "", fgets($this->sock, 512));
  204. $this->smtp_debug($response . "\n");
  205. if (!preg_match("#^[23]#", $response)) {
  206. fputs($this->sock, "QUIT\r\n");
  207. fgets($this->sock, 512);
  208. $this->log_write("Error: Remote host returned \"" . $response . "\"\n");
  209. return FALSE;
  210. }
  211. return TRUE;
  212. }
  213. function smtp_putcmd($cmd, $arg = "")
  214. {
  215. if ($arg != "") {
  216. if ($cmd == "") {
  217. $cmd = $arg;
  218. } else {
  219. $cmd = $cmd . " " . $arg;
  220. }
  221. }
  222. fputs($this->sock, $cmd . "\r\n");
  223. $this->smtp_debug("> " . $cmd . "\n");
  224. return $this->smtp_ok();
  225. }
  226. function smtp_error($string)
  227. {
  228. $this->log_write("Error: Error occurred while " . $string . ".\n");
  229. return FALSE;
  230. }
  231. function log_write($message)
  232. {
  233. $this->smtp_debug($message);
  234. if ($this->log_file == "") {
  235. return TRUE;
  236. }
  237. $message = date("M d H:i:s ") . get_current_user() . "[" . getmypid() . "]: " . $message;
  238. if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
  239. $this->smtp_debug("Warning: Cannot open log file \"" . $this->log_file . "\"\n");
  240. return FALSE;;
  241. }
  242. flock($fp, LOCK_EX);
  243. fputs($fp, $message);
  244. fclose($fp);
  245. return TRUE;
  246. }
  247. function strip_comment($address)
  248. {
  249. $comment = "#\([^()]*\)#";
  250. while (preg_match($comment, $address)) {
  251. $address = preg_replace($comment, "", $address);
  252. }
  253. return $address;
  254. }
  255. function get_address($address)
  256. {
  257. $address = preg_replace("#([ \t\r\n])+#", "", $address);
  258. $address = preg_replace("#^.*<(.+)>.*$#", "\1", $address);
  259. return $address;
  260. }
  261. function smtp_debug($message)
  262. {
  263. if ($this->debug) {
  264. echo $message;
  265. }
  266. }
  267. }//End Class