html5中文学习网

您的位置: 首页 > 网络编程 > PHP编程 » 正文

PHP发送邮件_PHP教程_编程技术

[ ] 已经帮助:人解决问题

PHP发送邮件的实例,页面部分代码:FQCHTML5中文学习网 - HTML5先行者学习网

  1. $smtpserver = "smtp.163.com";//SMTP服务器 
  2.         $smtpserverport =25;//SMTP服务器端口 
  3.         $smtpusermail = "username@163.com";//SMTP服务器的用户邮箱 
  4.         $smtpemailto = $email;//发送给谁 
  5.         $smtpuser = "username@163.com";//SMTP服务器的用户帐号 
  6.         $smtppass = "password";//SMTP服务器的用户密码 
  7.         $mailsubject = "subject";//邮件主题 
  8.         $mailbody = file_get_contents("email.html");//读取html文件 
  9.         $mailbody = str_replace("{name}",$username,$mailbody);//替换变量 
  10.         $mailbody = str_replace("{email}",$email,$mailbody); 
  11.         $mailbody = str_replace("{password}",$password,$mailbody); 
  12.         $mailbody = str_replace("{mobile}",$mobile,$mailbody); 
  13.         $mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件 
  14.          
  15.         //邮件格式(HTML/TXT),TXT为文本邮件 
  16.         ########################################## 
  17.         $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证. 
  18.         //$smtp->debug = false;//是否显示发送的调试信息 
  19.         $smtp->sendmail($smtpemailto,$smtpusermail,$mailsubject,$mailbody,$mailtype); 

以下是邮件服务器的类,直接调用就行了email.class.php:FQCHTML5中文学习网 - HTML5先行者学习网

  1. <? 
  2. class smtp 
  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.  
  14. /* Private Variables */ 
  15. var $sock
  16.  
  17. /* Constractor */ 
  18. function smtp($relay_host = ""$smtp_port = 25,$auth = false,$user,$pass
  19. $this->debug = FALSE; 
  20. $this->smtp_port = $smtp_port
  21. $this->relay_host = $relay_host
  22. $this->time_out = 30; //is used in fsockopen() 
  23. $this->auth = $auth;//auth 
  24. $this->user = $user
  25. $this->pass = $pass
  26. $this->host_name = "localhost"//is used in HELO command 
  27. $this->log_file =""
  28.  
  29. $this->sock = FALSE; 
  30.  
  31. /* Main Function */ 
  32. function sendmail($to$from$subject = ""$body = ""$mailtype$cc = ""$bcc = ""$additional_headers = ""
  33. $mail_from = $this->get_address($this->strip_comment($from)); 
  34. $body = ereg_replace("(^(/r/n))(//.)""//1.//3"$body); 
  35. $header = ''
  36. $header .= "MIME-Version:1.0/r/n"
  37. if($mailtype=="HTML"){ 
  38. $header .= "Content-Type:text/html/r/n"
  39. $header .= "To: ".$to."/r/n"
  40. if ($cc != "") { 
  41. $header .= "Cc: ".$cc."/r/n"
  42. $header .= "From: $from<".$from.">/r/n"
  43. $header .= "Subject: ".$subject."/r/n"
  44. $header .= $additional_headers
  45. $header .= "Date: ".date("r")."/r/n"
  46. $header .= "X-Mailer:By Redhat (PHP/".phpversion().")/r/n"
  47. list($msec$sec) = explode(" ", microtime()); 
  48. $header .= "Message-ID: <".date("YmdHis"$sec).".".($msec*1000000).".".$mail_from.">/r/n"
  49. $TO = explode(","$this->strip_comment($to)); 
  50.  
  51. if ($cc != "") { 
  52. $TO = array_merge($TOexplode(","$this->strip_comment($cc))); 
  53.  
  54. if ($bcc != "") { 
  55. $TO = array_merge($TOexplode(","$this->strip_comment($bcc))); 
  56.  
  57. $sent = TRUE; 
  58. foreach ($TO as $rcpt_to) { 
  59. $rcpt_to = $this->get_address($rcpt_to); 
  60. if (!$this->smtp_sockopen($rcpt_to)) { 
  61. $this->log_write("Error: Cannot send email to ".$rcpt_to."/n"); 
  62. $sent = FALSE; 
  63. continue
  64. if ($this->smtp_send($this->host_name, $mail_from$rcpt_to$header$body)) { 
  65. $this->log_write("E-mail has been sent to <".$rcpt_to.">/n"); 
  66. else { 
  67. $this->log_write("Error: Cannot send email to <".$rcpt_to.">/n"); 
  68. $sent = FALSE; 
  69. fclose($this->sock); 
  70. $this->log_write("Disconnected from remote host/n"); 
  71. //echo "<br>"; 
  72. //echo $header; 
  73. return $sent
  74.  
  75. /* Private Functions */ 
  76.  
  77. function smtp_send($helo$from$to$header$body = ""
  78. if (!$this->smtp_putcmd("HELO"$helo)) { 
  79. return $this->smtp_error("sending HELO command"); 
  80. #auth 
  81. if($this->auth){ 
  82. if (!$this->smtp_putcmd("AUTH LOGIN"base64_encode($this->user))) { 
  83. return $this->smtp_error("sending HELO command"); 
  84.  
  85. if (!$this->smtp_putcmd(""base64_encode($this->pass))) { 
  86. return $this->smtp_error("sending HELO command"); 
  87. if (!$this->smtp_putcmd("MAIL""FROM:<".$from.">")) { 
  88. return $this->smtp_error("sending MAIL FROM command"); 
  89.  
  90. if (!$this->smtp_putcmd("RCPT""TO:<".$to.">")) { 
  91. return $this->smtp_error("sending RCPT TO command"); 
  92.  
  93. if (!$this->smtp_putcmd("DATA")) { 
  94. return $this->smtp_error("sending DATA command"); 
  95.  
  96. if (!$this->smtp_message($header$body)) { 
  97. return $this->smtp_error("sending message"); 
  98.  
  99. if (!$this->smtp_eom()) { 
  100. return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]"); 
  101.  
  102. if (!$this->smtp_putcmd("QUIT")) { 
  103. return $this->smtp_error("sending QUIT command"); 
  104.  
  105. return TRUE; 
  106.  
  107. function smtp_sockopen($address
  108. if ($this->relay_host == "") { 
  109. return $this->smtp_sockopen_mx($address); 
  110. else { 
  111. return $this->smtp_sockopen_relay(); 
  112.  
  113. function smtp_sockopen_relay() 
  114. $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."/n"); 
  115. $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno$errstr$this->time_out); 
  116. if (!($this->sock && $this->smtp_ok())) { 
  117. $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."/n"); 
  118. $this->log_write("Error: ".$errstr." (".$errno.")/n"); 
  119. return FALSE; 
  120. $this->log_write("Connected to relay host ".$this->relay_host."/n"); 
  121. return TRUE;; 
  122.  
  123. function smtp_sockopen_mx($address
  124. $domain = ereg_replace("^.+@([^@]+){1}quot;, "//1", $address); 
  125. if (!@getmxrr($domain$MXHOSTS)) { 
  126. $this->log_write("Error: Cannot resolve MX /"".$domain."/"/n"); 
  127. return FALSE; 
  128. foreach ($MXHOSTS as $host) { 
  129. $this->log_write("Trying to ".$host.":".$this->smtp_port."/n"); 
  130. $this->sock = @fsockopen($host$this->smtp_port, $errno$errstr$this->time_out); 
  131. if (!($this->sock && $this->smtp_ok())) { 
  132. $this->log_write("Warning: Cannot connect to mx host ".$host."/n"); 
  133. $this->log_write("Error: ".$errstr." (".$errno.")/n"); 
  134. continue
  135. $this->log_write("Connected to mx host ".$host."/n"); 
  136. return TRUE; 
  137. $this->log_write("Error: Cannot connect to any mx hosts (".implode(", "$MXHOSTS).")/n"); 
  138. return FALSE; 
  139.  
  140. function smtp_message($header$body
  141. fputs($this->sock, $header."/r/n".$body); 
  142. $this->smtp_debug("> ".str_replace("/r/n""/n"."> "$header."/n> ".$body."/n> ")); 
  143.  
  144. return TRUE; 
  145.  
  146. function smtp_eom() 
  147. fputs($this->sock, "/r/n./r/n"); 
  148. $this->smtp_debug(". [EOM]/n"); 
  149.  
  150. return $this->smtp_ok(); 
  151.  
  152. function smtp_ok() 
  153. $response = str_replace("/r/n"""fgets($this->sock, 512)); 
  154. $this->smtp_debug($response."/n"); 
  155.  
  156. if (!ereg("^[23]"$response)) { 
  157. fputs($this->sock, "QUIT/r/n"); 
  158. fgets($this->sock, 512); 
  159. $this->log_write("Error: Remote host returned /"".$response."/"/n"); 
  160. return FALSE; 
  161. return TRUE; 
  162.  
  163. function smtp_putcmd($cmd$arg = ""
  164. if ($arg != "") { 
  165. if($cmd==""$cmd = $arg
  166. else $cmd = $cmd." ".$arg
  167.  
  168. fputs($this->sock, $cmd."/r/n"); 
  169. $this->smtp_debug("> ".$cmd."/n"); 
  170.  
  171. return $this->smtp_ok(); 
  172.  
  173. function smtp_error($string
  174. $this->log_write("Error: Error occurred while ".$string."./n"); 
  175. return FALSE; 
  176.  
  177. function log_write($message
  178. $this->smtp_debug($message); 
  179.  
  180. if ($this->log_file == "") { 
  181. return TRUE; 
  182.  
  183. $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message
  184. if (!@file_exists($this->log_file)  !($fp = @fopen($this->log_file, "a"))) { 
  185. $this->smtp_debug("Warning: Cannot open log file /"".$this->log_file."/"/n"); 
  186. return FALSE; 
  187. flock($fp, LOCK_EX); 
  188. fputs($fp$message); 
  189. fclose($fp); 
  190.  
  191. return TRUE; 
  192.  
  193. function strip_comment($address
  194. $comment = "//([^()]*//)"
  195. while (ereg($comment$address)) { 
  196. $address = ereg_replace($comment""$address); 
  197.  
  198. return $address
  199.  
  200. function get_address($address
  201. $address = ereg_replace("([ /t/r/n])+"""$address); 
  202. $address = ereg_replace("^.*<(.+)>.*{1}quot;, "//1", $address); 
  203.  
  204. return $address
  205.  
  206. function smtp_debug($message
  207. if ($this->debug) { 
  208. //echo $message."<br>"; 
  209.  
  210. function get_attach_type($image_tag) { // 
  211.  
  212. $filedata = array(); 
  213.  
  214. $img_file_con=fopen($image_tag,"r"); 
  215. unset($image_data); 
  216. while ($tem_buffer=AddSlashes(fread($img_file_con,filesize($image_tag)))) 
  217. $image_data.=$tem_buffer
  218. fclose($img_file_con); 
  219.  
  220. $filedata['context'] = $image_data
  221. $filedata['filename']= basename($image_tag); 
  222. $extension=substr($image_tag,strrpos($image_tag,"."),strlen($image_tag)-strrpos($image_tag,".")); 
  223. switch($extension){ 
  224. case ".gif"
  225. $filedata['type'] = "image/gif"
  226. break
  227. case ".gz"
  228. $filedata['type'] = "application/x-gzip"
  229. break
  230. case ".htm"
  231. $filedata['type'] = "text/html"
  232. break
  233. case ".html"
  234. $filedata['type'] = "text/html"
  235. break
  236. case ".jpg"
  237. $filedata['type'] = "image/jpeg"
  238. break
  239. case ".tar"
  240. $filedata['type'] = "application/x-tar"
  241. break
  242. case ".txt"
  243. $filedata['type'] = "text/plain"
  244. break
  245. case ".zip"
  246. $filedata['type'] = "application/zip"
  247. break
  248. default
  249. $filedata['type'] = "application/octet-stream"
  250. break
  251. return $filedata
  252. ?> 
FQCHTML5中文学习网 - HTML5先行者学习网
FQCHTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助