首页  >  文章  >  后端开发  >  php smtp发送邮件

php smtp发送邮件

WBOY
WBOY原创
2016-07-25 08:42:111176浏览

php通过smtp发送邮件需要通过一个类smtp

发邮件的代码如下:

  1. require_once 'smtp.php';
  2. ##########################################
  3. $smtpserver = "smtp.sina.com";//SMTP服务器
  4. $smtpserverport = 25;//SMTP服务器端口
  5. $smtpusermail = "god_chen@sina.com";//SMTP服务器的用户邮箱
  6. $smtpemailto = "45323333@qq.com";//发送给谁
  7. $smtpuser = "name";//SMTP服务器的用户帐号
  8. $smtppass = "123456";//SMTP服务器的用户密码
  9. $mailsubject = "中文";//邮件主题
  10. $mailbody = "

    中文

    测试下能淤泥新年感";//邮件内容
  11. $mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件
  12. ##########################################
  13. $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
  14. $smtp->debug = FALSE;//是否显示发送的调试信息
  15. $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype)
复制代码



smtp类的源码如下:

  1. class smtp
  2. {
  3. /* Public Variables */
  4. var $smtp_port;
  5. var $time_out;
  6. var $host_name;
  7. var $log_file;
  8. var $relay_host;
  9. var $debug;
  10. var $auth;
  11. var $user;
  12. var $pass;
  13. /* Private Variables */
  14. var $sock;
  15. /* Constractor */
  16. function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
  17. {
  18. $this->debug = FALSE;
  19. $this->smtp_port = $smtp_port;
  20. $this->relay_host = $relay_host;
  21. $this->time_out = 30; //is used in fsockopen()
  22. $this->auth = $auth;//auth
  23. $this->user = $user;
  24. $this->pass = $pass;
  25. $this->host_name = "localhost"; //is used in HELO command
  26. $this->log_file = "";
  27. $this->sock = FALSE;
  28. }
  29. /* Main Function */
  30. function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
  31. {
  32. $mail_from = $this->get_address($this->strip_comment($from));
  33. $body = ereg_replace("(^|(\r\n))(\.)", "\1.\3", $body);
  34. $header = "MIME-Version:1.0\r\n";
  35. if($mailtype=="HTML")
  36. {
  37. $header .= "Content-Type:text/html\r\n";
  38. }
  39. $header .= "To: ".$to."\r\n";
  40. if ($cc != "")
  41. {
  42. $header .= "Cc: ".$cc."\r\n";
  43. }
  44. $header .= "From: $from\r\n";
  45. $header .= "Subject: ".$subject."\r\n";
  46. $header .= $additional_headers;
  47. $header .= "Date: ".date("r")."\r\n";
  48. $header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n";
  49. list($msec, $sec) = explode(" ", microtime());
  50. $header .= "Message-ID: \r\n";
  51. $TO = explode(",", $this->strip_comment($to));
  52. if ($cc != "")
  53. {
  54. $TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
  55. }
  56. if ($bcc != "")
  57. {
  58. $TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
  59. }
  60. $sent = TRUE;
  61. foreach ($TO as $rcpt_to)
  62. {
  63. $rcpt_to = $this->get_address($rcpt_to);
  64. if (!$this->smtp_sockopen($rcpt_to))
  65. {
  66. $this->log_write("Error: Cannot send email to ".$rcpt_to."\n");
  67. $sent = FALSE;
  68. continue;
  69. }
  70. if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body))
  71. {
  72. $this->log_write("E-mail has been sent to \n");
  73. }
  74. else
  75. {
  76. $this->log_write("Error: Cannot send email to \n");
  77. $sent = FALSE;
  78. }
  79. fclose($this->sock);
  80. $this->log_write("Disconnected from remote host\n");
  81. }
  82. return $sent;
  83. }
  84. /* Private Functions */
  85. function smtp_send($helo, $from, $to, $header, $body = "")
  86. {
  87. if (!$this->smtp_putcmd("HELO", $helo))
  88. {
  89. return $this->smtp_error("sending HELO command");
  90. }
  91. #auth
  92. if($this->auth)
  93. {
  94. if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user)))
  95. {
  96. return $this->smtp_error("sending HELO command");
  97. }
  98. if (!$this->smtp_putcmd("", base64_encode($this->pass)))
  99. {
  100. return $this->smtp_error("sending HELO command");
  101. }
  102. }
  103. if (!$this->smtp_putcmd("MAIL", "FROM:"))
  104. {
  105. return $this->smtp_error("sending MAIL FROM command");
  106. }
  107. if (!$this->smtp_putcmd("RCPT", "TO:"))
  108. {
  109. return $this->smtp_error("sending RCPT TO command");
  110. }
  111. if (!$this->smtp_putcmd("DATA"))
  112. {
  113. return $this->smtp_error("sending DATA command");
  114. }
  115. if (!$this->smtp_message($header, $body))
  116. {
  117. return $this->smtp_error("sending message");
  118. }
  119. if (!$this->smtp_eom())
  120. {
  121. return $this->smtp_error("sending . [EOM]");
  122. }
  123. if (!$this->smtp_putcmd("QUIT"))
  124. {
  125. return $this->smtp_error("sending QUIT command");
  126. }
  127. return TRUE;
  128. }
  129. function smtp_sockopen($address)
  130. {
  131. if ($this->relay_host == "")
  132. {
  133. return $this->smtp_sockopen_mx($address);
  134. }
  135. else
  136. {
  137. return $this->smtp_sockopen_relay();
  138. }
  139. }
  140. function smtp_sockopen_relay()
  141. {
  142. $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n");
  143. $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
  144. if (!($this->sock && $this->smtp_ok()))
  145. {
  146. $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n");
  147. $this->log_write("Error: ".$errstr." (".$errno.")\n");
  148. return FALSE;
  149. }
  150. $this->log_write("Connected to relay host ".$this->relay_host."\n");
  151. return TRUE;;
  152. }
  153. function smtp_sockopen_mx($address)
  154. {
  155. $domain = ereg_replace("^.+@([^@]+)$", "\1", $address);
  156. if (!@getmxrr($domain, $MXHOSTS))
  157. {
  158. $this->log_write("Error: Cannot resolve MX \"".$domain."\"\n");
  159. return FALSE;
  160. }
  161. foreach ($MXHOSTS as $host)
  162. {
  163. $this->log_write("Trying to ".$host.":".$this->smtp_port."\n");
  164. $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
  165. if (!($this->sock && $this->smtp_ok()))
  166. {
  167. $this->log_write("Warning: Cannot connect to mx host ".$host."\n");
  168. $this->log_write("Error: ".$errstr." (".$errno.")\n");
  169. continue;
  170. }
  171. $this->log_write("Connected to mx host ".$host."\n");
  172. return TRUE;
  173. }
  174. $this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n");
  175. return FALSE;
  176. }
  177. function smtp_message($header, $body)
  178. {
  179. fputs($this->sock, $header."\r\n".$body);
  180. $this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> "));
  181. return TRUE;
  182. }
  183. function smtp_eom()
  184. {
  185. fputs($this->sock, "\r\n.\r\n");
  186. $this->smtp_debug(". [EOM]\n");
  187. return $this->smtp_ok();
  188. }
  189. function smtp_ok()
  190. {
  191. $response = str_replace("\r\n", "", fgets($this->sock, 512));
  192. $this->smtp_debug($response."\n");
  193. if (!ereg("^[23]", $response))
  194. {
  195. fputs($this->sock, "QUIT\r\n");
  196. fgets($this->sock, 512);
  197. $this->log_write("Error: Remote host returned \"".$response."\"\n");
  198. return FALSE;
  199. }
  200. return TRUE;
  201. }
  202. function smtp_putcmd($cmd, $arg = "")
  203. {
  204. if ($arg != "")
  205. {
  206. if($cmd=="")
  207. {
  208. $cmd = $arg;
  209. }
  210. else
  211. {
  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. {
  229. return TRUE;
  230. }
  231. $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
  232. if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a")))
  233. {
  234. $this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n");
  235. return FALSE;;
  236. }
  237. flock($fp, LOCK_EX);
  238. fputs($fp, $message);
  239. fclose($fp);
  240. return TRUE;
  241. }
  242. function strip_comment($address)
  243. {
  244. $comment = "\([^()]*\)";
  245. while (ereg($comment, $address))
  246. {
  247. $address = ereg_replace($comment, "", $address);
  248. }
  249. return $address;
  250. }
  251. function get_address($address)
  252. {
  253. $address = ereg_replace("([ \t\r\n])+", "", $address);
  254. $address = ereg_replace("^.*.*$", "\1", $address);
  255. return $address;
  256. }
  257. function smtp_debug($message)
  258. {
  259. if ($this->debug)
  260. {
  261. echo $message;
  262. }
  263. }
  264. }
复制代码


发送邮件, php, smtp


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn