国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

262 行
9.4KB

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