多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > php教程 > php發送郵件代碼

php發送郵件代碼

來源:程序員人生   發布時間:2014-02-05 10:19:08 閱讀次數:4282次
  1. <?php 
  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. /* Private Variables */ 
  14. var $sock
  15. /* Constractor */ 
  16. function smtp($relay_host = ""$smtp_port = 25,$auth = false,$user,$pass
  17. $this->debug = FALSE; 
  18. $this->smtp_port = $smtp_port
  19. $this->relay_host = $relay_host
  20. $this->time_out = 30; //is used in fsockopen() 
  21. $this->auth = $auth;//auth 
  22. $this->user = $user
  23. $this->pass = $pass
  24. $this->host_name = "localhost"//is used in HELO command 
  25. $this->log_file =""
  26. $this->sock = FALSE; 
  27. /* Main Function */ 
  28. function sendmail($to$from$subject = ""$body = ""$mailtype$cc = ""$bcc = ""$additional_headers = ""
  29. $mail_from = $this->get_address($this->strip_comment($from)); 
  30. $body = ereg_replace("(^|(rn))(.)""[url=file://1.3]1.3[/url]"$body); 
  31. $header .= "MIME-Version:1.0rn"
  32. if($mailtype=="HTML"){ 
  33. $header .= "Content-Type:text/htmlrn "
  34. $header .= "To: ".$to."rn"
  35. if ($cc != "") { 
  36. $header .= "Cc: ".$cc."rn"
  37. $header .= "From: $from<".$from.">rn"
  38. $header .= "Subject: ".$subject."rn"
  39. $header .= $additional_headers
  40. $header .= "Date: ".date("r")."rn"
  41. $header .= "X-Mailer:By Redhat (PHP/".phpversion().")rn"
  42. list($msec$sec) = explode(" ", microtime()); 
  43. $header .= "Message-ID: <".date("YmdHis"$sec).".".($msec*1000000).".".$mail_from.">rn"
  44. $TO = explode(","$this->strip_comment($to)); 
  45. if ($cc != "") { 
  46. $TO = array_merge($TOexplode(","$this->strip_comment($cc))); 
  47. if ($bcc != "") { 
  48. $TO = array_merge($TOexplode(","$this->strip_comment($bcc))); 
  49. $sent = TRUE; 
  50. foreach ($TO as $rcpt_to) { 
  51. $rcpt_to = $this->get_address($rcpt_to); 
  52. if (!$this->smtp_sockopen($rcpt_to)) { 
  53. $this->log_write("Error: Cannot send email to ".$rcpt_to."n"); 
  54. $sent = FALSE; 
  55. continue
  56. if ($this->smtp_send($this->host_name, $mail_from$rcpt_to$header$body)) { 
  57. $this->log_write("E-mail has been sent to <".$rcpt_to.">n"); 
  58. else { 
  59. $this->log_write("Error: Cannot send email to <".$rcpt_to.">n"); 
  60. $sent = FALSE; 
  61. fclose($this->sock); 
  62. $this->log_write("Disconnected from remote hostn"); 
  63. echo "<br>"
  64. echo $header
  65. return $sent
  66. /* Private Functions */ 
  67. function smtp_send($helo$from$to$header$body = ""
  68. if (!$this->smtp_putcmd("HELO"$helo)) { 
  69. return $this->smtp_error("sending HELO command"); 
  70. #auth 
  71. if($this->auth){ 
  72. if (!$this->smtp_putcmd("AUTH LOGIN"base64_encode($this->user))) { 
  73. return $this->smtp_error("sending HELO command"); 
  74. if (!$this->smtp_putcmd(""base64_encode($this->pass))) { 
  75. return $this->smtp_error("sending HELO command"); 
  76. if (!$this->smtp_putcmd("MAIL""FROM:<".$from.">")) { 
  77. return $this->smtp_error("sending MAIL FROM command"); 
  78. if (!$this->smtp_putcmd("RCPT""TO:<".$to.">")) { 
  79. return $this->smtp_error("sending RCPT TO command"); 
  80. if (!$this->smtp_putcmd("DATA")) { 
  81. return $this->smtp_error("sending DATA command"); 
  82. if (!$this->smtp_message($header$body)) { 
  83. return $this->smtp_error("sending message"); 
  84. if (!$this->smtp_eom()) { 
  85. return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]"); 
  86. if (!$this->smtp_putcmd("QUIT")) { 
  87. return $this->smtp_error("sending QUIT command"); 
  88. return TRUE; 
  89. function smtp_sockopen($address
  90. if ($this->relay_host == "") { 
  91. return $this->smtp_sockopen_mx($address); 
  92. else { 
  93. return $this->smtp_sockopen_relay(); 
  94. function smtp_sockopen_relay() 
  95. $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."n"); 
  96. $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno$errstr$this->time_out); 
  97. if (!($this->sock && $this->smtp_ok())) { 
  98. $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."n"); 
  99. $this->log_write("Error: ".$errstr." (".$errno.")n"); 
  100. return FALSE; 
  101. $this->log_write("Connected to relay host ".$this->relay_host."n"); 
  102. return TRUE;; 
  103. function smtp_sockopen_mx($address
  104. $domain = ereg_replace("[url=mailto:^.+@([^@]+)$]^.+@([^@]+)$[/url]""[url=file://1]1[/url]"$address); 
  105. if (!@getmxrr($domain$MXHOSTS)) { 
  106. $this->log_write("Error: Cannot resolve MX "".$domain.""n"); 
  107. return FALSE; 
  108. foreach ($MXHOSTS as $host) { 
  109. $this->log_write("Trying to ".$host.":".$this->smtp_port."n"); 
  110. $this->sock = @fsockopen($host$this->smtp_port, $errno$errstr$this->time_out); 
  111. if (!($this->sock && $this->smtp_ok())) { 
  112. $this->log_write("Warning: Cannot connect to mx host ".$host."n"); 
  113. $this->log_write("Error: ".$errstr." (".$errno.")n"); 
  114. continue
  115. $this->log_write("Connected to mx host ".$host."n"); 
  116. return TRUE; 
  117. $this->log_write("Error: Cannot connect to any mx hosts (".implode(", "$MXHOSTS).")n"); 
  118. return FALSE; 
  119. function smtp_message($header$body
  120. fputs($this->sock, $header."rn".$body); 
  121. $this->smtp_debug("> ".str_replace("rn""n"."> "$header."n> ".$body."n> ")); 
  122. return TRUE; 
  123. function smtp_eom() 
  124. fputs($this->sock, "rn.rn"); 
  125. $this->smtp_debug(". [EOM]n"); 
  126. return $this->smtp_ok(); 
  127. function smtp_ok() 
  128. $response = str_replace("rn"""fgets($this->sock, 512)); 
  129. $this->smtp_debug($response."n"); 
  130. if (!ereg("^[23]"$response)) { 
  131. fputs($this->sock, "QUITrn"); 
  132. fgets($this->sock, 512); 
  133. $this->log_write("Error: Remote host returned "".$response.""n"); 
  134. return FALSE; 
  135. return TRUE; 
  136. function smtp_putcmd($cmd$arg = ""
  137. if ($arg != "") { 
  138. if($cmd==""$cmd = $arg
  139. else $cmd = $cmd." ".$arg
  140. fputs($this->sock, $cmd."rn"); 
  141. $this->smtp_debug("> ".$cmd."n"); 
  142. return $this->smtp_ok(); 
  143. function smtp_error($string
  144. $this->log_write("Error: Error occurred while ".$string.".n"); 
  145. return FALSE; 
  146. function log_write($message
  147. $this->smtp_debug($message); 
  148. if ($this->log_file == "") { 
  149. return TRUE; 
  150. $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message
  151. if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) { 
  152. $this->smtp_debug("Warning: Cannot open log file "".$this->log_file.""n"); 
  153. return FALSE; 
  154. flock($fp, LOCK_EX); 
  155. fputs($fp$message); 
  156. fclose($fp); 
  157. return TRUE; 
  158. function strip_comment($address
  159. $comment = "[url=file://([^()]*]([^()]*[/url])"
  160. while (ereg($comment$address)) { 
  161. $address = ereg_replace($comment""$address); 
  162. return $address
  163. function get_address($address
  164. $address = ereg_replace("([ trn])+"""$address); 
  165. $address = ereg_replace("^.*<(.+)>.*$""[url=file://1]1[/url]"$address); 
  166. return $address
  167. function smtp_debug($message
  168. if ($this->debug) { 
  169. echo $message."<br>"
  170. function get_attach_type($image_tag) { // 
  171. $filedata = array(); 
  172. $img_file_con=fopen($image_tag,"r"); 
  173. unset($image_data); 
  174. while ($tem_buffer=AddSlashes(fread($img_file_con,filesize($image_tag)))) 
  175. $image_data.=$tem_buffer
  176. fclose($img_file_con); 
  177. $filedata['context'] = $image_data
  178. $filedata['filename']= basename($image_tag); 
  179. $extension=substr($image_tag,strrpos($image_tag,"."),strlen($image_tag)-strrpos($image_tag,".")); 
  180. switch($extension){ 
  181. case ".gif"
  182. $filedata['type'] = "image/gif"
  183. break
  184. case ".gz"
  185. $filedata['type'] = "application/x-gzip"
  186. break
  187. case ".htm"
  188. $filedata['type'] = "text/html"
  189. break
  190. case ".html"
  191. $filedata['type'] = "text/html"
  192. break
  193. case ".jpg"
  194. $filedata['type'] = "image/jpeg"
  195. break
  196. case ".tar"
  197. $filedata['type'] = "application/x-tar"
  198. break
  199. case ".txt"
  200. $filedata['type'] = "text/plain"
  201. break
  202. case ".zip"
  203. $filedata['type'] = "application/zip"
  204. break
  205. default
  206. $filedata['type'] = "application/octet-stream"
  207. break
  208. return $filedata
  209. ?> 

send.php:

$title                   = $_POST['title'];

$Message            = $_POST['Message'];

$obj_title           = iconv('UTF-8','Shift-JIS',$title);

$obj_message    = iconv('UTF-8', 'Shift-JIS',$Message);

$smtpserver     = "smtp.163.com";//SMTP服務器

$smtpserverport =25;//SMTP服務器端口

$smtpusermail   = "";//SMTP服務器的用戶郵箱

$smtpemailto    = "";//發送給誰

$smtpuser       = "";  //SMTP服務器的用戶帳號

$smtppass       = "";//SMTP服務器的用戶密碼

$mailsubject    = $title;//郵件主題

$mailbody       = $Message  ;//郵件內容

$mailtype       = "HTML";//郵件格式(HTML/TXT),TXT為文本郵件

$smtp           = new

smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//這里面的一true是表示使用身份驗證,否則不使用身份驗證.

$smtp->debug = true;//是否顯示發送的調試信息

$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 最新国产福利 | 精品久久精品久久 | 国产精品久久1024 | 亚欧成人乱码一区二区 | 日韩精品亚洲一级在线观看 | 1314成人网| 99亚洲| 天堂在线观看视频 | 搞黄视频在线观看 | 午夜免费啪在线观看视频网站 | 视频在线观看免费网址 | 日韩精品一区二区三区中文精品 | 亚洲人网| 亚洲天堂伦理 | 国产福利二区 | 春色在线 | 精品视频一区二区三三区四区 | 亚洲区小说区激情区图片区 | 色永久| 伊人狼人影院 | 亚洲最新视频在线观看 | 午夜免费啪视频观看网站 | 日韩欧美亚洲一区精选 | 国产亚洲欧美另类久久久 | 另类小说亚洲 | 久久久久毛片成人精品 | 中文字幕一区二区三区在线观看 | 青青国产成人精品视频 | 老司机午夜免费 | 九色av99久久 | 色老成人精品视频在线观看 | 五月激情六月丁香 | 免费区欧美一级毛片精品 | 欧美精品国产一区二区三区 | 一二三四观看在线视频中文 | 中文字幕一区二区三区乱码 | 日韩欧美成末人一区二区三区 | 春色视频免费版高清在线观看 | 亚洲 欧美 另类 综合 日韩 | 自拍网址| 欧美一级毛片大片免费播放 |