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.

372 lines
12 KiB

7 years ago
  1. <?php
  2. /******************************************************************************\
  3. * SMTP邮件类
  4. *
  5. * 注:本邮件类经测试是成功了的,如果大家发送邮件的时候遇到了失败的问题,请从以下几点排查:
  6. * 1. 用户名和密码是否正确;
  7. * 2. 检查邮箱设置是否启用了smtp服务;
  8. * 3. 是否是php环境的问题导致;
  9. * 4. 将22行的$smtp->debug = false改为true,可以显示错误信息,然后可以复制报错信息到网上搜一下错误的原因;
  10. *
  11. * -----------------------------------------------------------------------------
  12. *
  13. * ******************************* 配置信息 ************************************
  14. * $smtpserver = 'smtp.163.com';//SMTP服务器
  15. * $smtpserverport = 25;//SMTP服务器端口
  16. * $smtpusermail = '1feng.2008@163.com';//SMTP服务器的用户邮箱
  17. * $smtpemailto = '277023115@qq.com';//发送给谁
  18. * $smtpuser = "1feng.2008@163.com";//SMTP服务器的用户帐号
  19. * $smtppass = "";//SMTP服务器的用户密码
  20. * $mailtitle = '邮件标题';//邮件主题
  21. * $mailcontent = '';//邮件内容
  22. * $mailtype = 'HTML';//邮件格式(HTML/TXT),TXT为文本邮件
  23. * $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
  24. * $smtp->debug = false;//是否显示发送的调试信息
  25. * $state = $smtp->sendmail($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype);
  26. * if($state==""){echo '对不起,邮件发送失败!请检查邮箱填写是否有误。';exit();}else{echo '邮件发送成功';}
  27. \******************************************************************************/
  28. namespace App\Common;
  29. class Smtp
  30. {
  31. /* Public Variables */
  32. var $smtp_port;//SMTP服务器端口
  33. var $time_out;//
  34. var $host_name;//
  35. var $log_file;
  36. var $relay_host;//SMTP服务器
  37. var $debug;//是否显示发送的调试信息
  38. var $auth;//true是表示使用身份验证,否则不使用身份验证
  39. var $user;//SMTP服务器的用户帐号
  40. var $pass;//SMTP服务器的用户密码
  41. /* Private Variables */
  42. var $sock;
  43. /* Constractor */
  44. function __construct($relay_host = '', $smtp_port = 25,$auth = false,$user,$pass)
  45. {
  46. $this->debug = FALSE;
  47. $this->smtp_port = $smtp_port;
  48. $this->relay_host = $relay_host;
  49. $this->time_out = 30; //is used in fsockopen()
  50. $this->auth = $auth;//auth
  51. $this->user = $user;
  52. $this->pass = $pass;
  53. $this->host_name = 'localhost'; //is used in HELO command
  54. $this->log_file = '';
  55. $this->sock = FALSE;
  56. }
  57. /*获取要抓取的列表的URL地址
  58. *
  59. * @param $to 发送给谁
  60. * @param $from SMTP服务器的用户邮箱
  61. * @param $subject 邮件主题
  62. * @param $body 邮件内容
  63. * @param $mailtype 邮件格式(HTML/TXT),TXT为文本邮件
  64. *
  65. * @return array 返回列表URL
  66. */
  67. function sendmail($to, $from, $subject = '', $body = '', $mailtype, $cc = '', $bcc = '', $additional_headers = '')
  68. {
  69. $mail_from = $this->get_address($this->strip_comment($from));
  70. $body = preg_replace("/(^|(\r\n))(\.)/", "\1.\3", $body);
  71. $header = "MIME-Version:1.0\r\n";
  72. if($mailtype=="HTML")
  73. {
  74. $header .= "Content-Type:text/html\r\n";
  75. }
  76. $header .= "To: ".$to."\r\n";
  77. if ($cc != "")
  78. {
  79. $header .= "Cc: ".$cc."\r\n";
  80. }
  81. $header .= "From: $from<".$from.">\r\n";
  82. $header .= "Subject: ".$subject."\r\n";
  83. $header .= $additional_headers;
  84. $header .= "Date: ".date("r")."\r\n";
  85. $header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n";
  86. list($msec, $sec) = explode(" ", microtime());
  87. $header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n";
  88. $TO = explode(",", $this->strip_comment($to));
  89. if ($cc != "")
  90. {
  91. $TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
  92. }
  93. if ($bcc != "")
  94. {
  95. $TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
  96. }
  97. $sent = TRUE;
  98. foreach ($TO as $rcpt_to)
  99. {
  100. $rcpt_to = $this->get_address($rcpt_to);
  101. if (!$this->smtp_sockopen($rcpt_to))
  102. {
  103. $this->log_write("Error: Cannot send email to ".$rcpt_to."\n");
  104. $sent = FALSE;
  105. continue;
  106. }
  107. if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body))
  108. {
  109. $this->log_write("E-mail has been sent to <".$rcpt_to.">\n");
  110. }
  111. else
  112. {
  113. $this->log_write("Error: Cannot send email to <".$rcpt_to.">\n");
  114. $sent = FALSE;
  115. }
  116. fclose($this->sock);
  117. $this->log_write("Disconnected from remote host\n");
  118. }
  119. return $sent;
  120. }
  121. /* Private Functions */
  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. if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">"))
  141. {
  142. return $this->smtp_error("sending MAIL FROM command");
  143. }
  144. if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">"))
  145. {
  146. return $this->smtp_error("sending RCPT TO command");
  147. }
  148. if (!$this->smtp_putcmd("DATA"))
  149. {
  150. return $this->smtp_error("sending DATA command");
  151. }
  152. if (!$this->smtp_message($header, $body))
  153. {
  154. return $this->smtp_error("sending message");
  155. }
  156. if (!$this->smtp_eom())
  157. {
  158. return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
  159. }
  160. if (!$this->smtp_putcmd("QUIT"))
  161. {
  162. return $this->smtp_error("sending QUIT command");
  163. }
  164. return TRUE;
  165. }
  166. function smtp_sockopen($address)
  167. {
  168. if ($this->relay_host == "")
  169. {
  170. return $this->smtp_sockopen_mx($address);
  171. }
  172. else
  173. {
  174. return $this->smtp_sockopen_relay();
  175. }
  176. }
  177. function smtp_sockopen_relay()
  178. {
  179. $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n");
  180. $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
  181. if (!($this->sock && $this->smtp_ok()))
  182. {
  183. $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n");
  184. $this->log_write("Error: ".$errstr." (".$errno.")\n");
  185. return FALSE;
  186. }
  187. $this->log_write("Connected to relay host ".$this->relay_host."\n");
  188. return TRUE;;
  189. }
  190. function smtp_sockopen_mx($address)
  191. {
  192. $domain = preg_replace("/^.+@([^@]+)$/", "\1", $address);
  193. if (!@getmxrr($domain, $MXHOSTS))
  194. {
  195. $this->log_write("Error: Cannot resolve MX \"".$domain."\"\n");
  196. return FALSE;
  197. }
  198. foreach ($MXHOSTS as $host)
  199. {
  200. $this->log_write("Trying to ".$host.":".$this->smtp_port."\n");
  201. $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
  202. if (!($this->sock && $this->smtp_ok()))
  203. {
  204. $this->log_write("Warning: Cannot connect to mx host ".$host."\n");
  205. $this->log_write("Error: ".$errstr." (".$errno.")\n");
  206. continue;
  207. }
  208. $this->log_write("Connected to mx host ".$host."\n");
  209. return TRUE;
  210. }
  211. $this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n");
  212. return FALSE;
  213. }
  214. function smtp_message($header, $body)
  215. {
  216. fputs($this->sock, $header."\r\n".$body);
  217. $this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> "));
  218. return TRUE;
  219. }
  220. function smtp_eom()
  221. {
  222. fputs($this->sock, "\r\n.\r\n");
  223. $this->smtp_debug(". [EOM]\n");
  224. return $this->smtp_ok();
  225. }
  226. function smtp_ok()
  227. {
  228. $response = str_replace("\r\n", "", fgets($this->sock, 512));
  229. $this->smtp_debug($response."\n");
  230. if (!preg_match("/^[23]/", $response))
  231. {
  232. fputs($this->sock, "QUIT\r\n");
  233. fgets($this->sock, 512);
  234. $this->log_write("Error: Remote host returned \"".$response."\"\n");
  235. return FALSE;
  236. }
  237. return TRUE;
  238. }
  239. function smtp_putcmd($cmd, $arg = "")
  240. {
  241. if ($arg != "")
  242. {
  243. if($cmd=="") $cmd = $arg;
  244. else $cmd = $cmd." ".$arg;
  245. }
  246. fputs($this->sock, $cmd."\r\n");
  247. $this->smtp_debug("> ".$cmd."\n");
  248. return $this->smtp_ok();
  249. }
  250. function smtp_error($string)
  251. {
  252. $this->log_write("Error: Error occurred while ".$string.".\n");
  253. return FALSE;
  254. }
  255. function log_write($message)
  256. {
  257. $this->smtp_debug($message);
  258. if ($this->log_file == "")
  259. {
  260. return TRUE;
  261. }
  262. $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
  263. if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a")))
  264. {
  265. $this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n");
  266. return FALSE;;
  267. }
  268. flock($fp, LOCK_EX);
  269. fputs($fp, $message);
  270. fclose($fp);
  271. return TRUE;
  272. }
  273. function strip_comment($address)
  274. {
  275. $comment = "/\([^()]*\)/";
  276. while (preg_match($comment, $address))
  277. {
  278. $address = preg_replace($comment, "", $address);
  279. }
  280. return $address;
  281. }
  282. function get_address($address)
  283. {
  284. $address = preg_replace("/([ \t\r\n])+/", "", $address);
  285. $address = preg_replace("/^.*<(.+)>.*$/", "\1", $address);
  286. return $address;
  287. }
  288. function smtp_debug($message)
  289. {
  290. if ($this->debug)
  291. {
  292. echo $message;
  293. }
  294. }
  295. }
  296. //~ $smtpserver = 'smtp.exmail.qq.com';//SMTP服务器
  297. //~ $smtpserverport = 25;//SMTP服务器端口
  298. //~ $smtpusermail = 'smtp@micw.com';//SMTP服务器的用户邮箱
  299. //~ $smtpemailto = '277023115@qq.com';//发送给谁
  300. //~ $smtpuser = "smtp@micw.com";//SMTP服务器的用户帐号
  301. //~ $smtppass = "XbdNew168!";//SMTP服务器的用户密码
  302. //~ $mailtitle = '三国演义ssl222';//邮件主题
  303. //~ $mailcontent = '《三国演义》是中国古典四大名著之一,是中国第一部长篇章回体历史演义小说,全名为《三国志通俗演义》,作者是元末明初的小说家罗贯中。';//邮件内容
  304. //~ $mailtype = 'HTML';//邮件格式(HTML/TXT),TXT为文本邮件
  305. //~ $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
  306. //~ $smtp->debug = false;//是否显示发送的调试信息
  307. //~ $state = $smtp->sendmail($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype);
  308. //~ if($state==""){echo '对不起,邮件发送失败!请检查邮箱填写是否有误。';exit();}else{echo '邮件发送成功';}
  309. ?>