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

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