国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > php教程 > php发送邮件代码

php发送邮件代码

来源:程序员人生   发布时间:2014-02-05 10:19:08 阅读次数:3913次
  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);

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生