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

326 lines
9.8KB

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