国内流行的内容管理系统(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.

286 lines
9.7KB

  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 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. {
  66. $TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
  67. }
  68. if ($bcc != "")
  69. {
  70. $TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
  71. }
  72. $sent = TRUE;
  73. foreach ($TO as $rcpt_to)
  74. {
  75. $headerto = "To: ".$rcpt_to."\r\n";
  76. $headerall = $header.$headerto;
  77. $rcpt_to = $this->get_address($rcpt_to);
  78. if (!$this->smtp_sockopen($rcpt_to))
  79. {
  80. $this->log_write("Error: Cannot send email to ".$rcpt_to."\n");
  81. $sent = FALSE;
  82. continue;
  83. }
  84. if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $headerall, $body))
  85. {
  86. $this->log_write("E-mail has been sent to <".$rcpt_to.">\n");
  87. } else {
  88. $this->log_write("Error: Cannot send email to <".$rcpt_to.">\n");
  89. $sent = FALSE;
  90. }
  91. fclose($this->sock);
  92. $this->log_write("Disconnected from remote host\n");
  93. }
  94. return $sent;
  95. }
  96. /**
  97. * SMTP发送
  98. *
  99. * @access public
  100. * @param string $helo 发送HELO
  101. * @param string $from 来自
  102. * @param string $to 到
  103. * @param string $header 头部信息
  104. * @param string $body 文档主体
  105. * @return string
  106. */
  107. function smtp_send($helo, $from, $to, $header, $body = "")
  108. {
  109. if (!$this->smtp_putcmd("HELO", $helo))
  110. {
  111. return $this->smtp_error("sending HELO command");
  112. }
  113. if ($this->auth)
  114. {
  115. if (!$this->smtp_putcmd("AUTH LOGIN"))
  116. {
  117. return $this->smtp_error("sending AUTH LOGIN command");
  118. }
  119. if (!$this->smtp_putcmd("", base64_encode($this->user)))
  120. {
  121. return $this->smtp_error("sending HELO command");
  122. }
  123. if (!$this->smtp_putcmd("", base64_encode($this->pass)))
  124. {
  125. return $this->smtp_error("sending HELO command");
  126. }
  127. }
  128. if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">"))
  129. {
  130. return $this->smtp_error("sending MAIL FROM command");
  131. }
  132. if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">"))
  133. {
  134. return $this->smtp_error("sending RCPT TO command");
  135. }
  136. if (!$this->smtp_putcmd("DATA"))
  137. {
  138. return $this->smtp_error("sending DATA command");
  139. }
  140. if (!$this->smtp_message($header, $body))
  141. {
  142. return $this->smtp_error("sending message");
  143. }
  144. if (!$this->smtp_eom())
  145. {
  146. return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
  147. }
  148. if (!$this->smtp_putcmd("QUIT"))
  149. {
  150. return $this->smtp_error("sending QUIT command");
  151. }
  152. return TRUE;
  153. }
  154. function smtp_sockopen($address)
  155. {
  156. if ($this->relay_host == "")
  157. {
  158. return $this->smtp_sockopen_mx($address);
  159. } else {
  160. return $this->smtp_sockopen_relay();
  161. }
  162. }
  163. function smtp_sockopen_relay()
  164. {
  165. $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n");
  166. $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
  167. if (!($this->sock && $this->smtp_ok()))
  168. {
  169. $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n");
  170. $this->log_write("Error: ".$errstr." (".$errno.")\n");
  171. return FALSE;
  172. }
  173. $this->log_write("Connected to relay host ".$this->relay_host."\n");
  174. return TRUE;;
  175. }
  176. function smtp_sockopen_mx($address)
  177. {
  178. $domain = preg_replace("/^.+@([^@]+)$/i", "\1", $address);
  179. if (!@getmxrr($domain, $MXHOSTS))
  180. {
  181. $this->log_write("Error: Cannot resolve MX \"".$domain."\"\n");
  182. return FALSE;
  183. }
  184. foreach ($MXHOSTS as $host)
  185. {
  186. $this->log_write("Trying to ".$host.":".$this->smtp_port."\n");
  187. $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
  188. if (!($this->sock && $this->smtp_ok()))
  189. {
  190. $this->log_write("Warning: Cannot connect to mx host ".$host."\n");
  191. $this->log_write("Error: ".$errstr." (".$errno.")\n");
  192. continue;
  193. }
  194. $this->log_write("Connected to mx host ".$host."\n");
  195. return TRUE;
  196. }
  197. $this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n");
  198. return FALSE;
  199. }
  200. function smtp_message($header, $body)
  201. {
  202. fputs($this->sock, $header."\r\n".$body);
  203. $this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> "));
  204. return TRUE;
  205. }
  206. function smtp_eom()
  207. {
  208. fputs($this->sock, "\r\n.\r\n");
  209. $this->smtp_debug(". [EOM]\n");
  210. return $this->smtp_ok();
  211. }
  212. function smtp_ok()
  213. {
  214. $response = str_replace("\r\n", "", fgets($this->sock, 512));
  215. $this->smtp_debug($response."\n");
  216. if (!preg_match("#^[23]#", $response))
  217. {
  218. fputs($this->sock, "QUIT\r\n");
  219. fgets($this->sock, 512);
  220. $this->log_write("Error: Remote host returned \"".$response."\"\n");
  221. return FALSE;
  222. }
  223. return TRUE;
  224. }
  225. function smtp_putcmd($cmd, $arg = "")
  226. {
  227. if ($arg != "")
  228. {
  229. if ($cmd == "")
  230. {
  231. $cmd = $arg;
  232. } else {
  233. $cmd = $cmd." ".$arg;
  234. }
  235. }
  236. fputs($this->sock, $cmd."\r\n");
  237. $this->smtp_debug("> ".$cmd."\n");
  238. return $this->smtp_ok();
  239. }
  240. function smtp_error($string)
  241. {
  242. $this->log_write("Error: Error occurred while ".$string.".\n");
  243. return FALSE;
  244. }
  245. function log_write($message)
  246. {
  247. $this->smtp_debug($message);
  248. if ($this->log_file == "")
  249. {
  250. return TRUE;
  251. }
  252. $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
  253. if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a")))
  254. {
  255. $this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n");
  256. return FALSE;;
  257. }
  258. flock($fp, LOCK_EX);
  259. fputs($fp, $message);
  260. fclose($fp);
  261. return TRUE;
  262. }
  263. function strip_comment($address)
  264. {
  265. $comment = "#\([^()]*\)#";
  266. while (preg_match($comment, $address))
  267. {
  268. $address = preg_replace($comment, "", $address);
  269. }
  270. return $address;
  271. }
  272. function get_address($address)
  273. {
  274. $address = preg_replace("#([ \t\r\n])+#", "", $address);
  275. $address = preg_replace("#^.*<(.+)>.*$#", "\1", $address);
  276. return $address;
  277. }
  278. function smtp_debug($message)
  279. {
  280. if ($this->debug)
  281. {
  282. echo $message;
  283. }
  284. }
  285. }//End Class
  286. ?>