首頁 > 後端開發 > php教程 > PHP郵件操作類

PHP郵件操作類

WBOY
發布: 2016-07-25 08:42:28
原創
981 人瀏覽過
複製程式碼
  1. class smtp
  2. {
  3. var $smtp_port;
  4. var $time_out;
  5. var $host_name;
  6. var $ log_file;
  7. var $relay_host;
  8. var $debug;
  9. var $auth;
  10. var $user;
  11. var $pass;
  12. var $sock;
  13. function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
  14. {
  15. $this->debug = true;
  16. $this- > smtp_port = $smtp_port;
  17. $this->relay_host = $relay_host;
  18. $this->time_out = 30;
  19. $this->auth = $auth;
  20. $this->user = $user;
  21. $this->pass = $pass;
  22. $this->host_name = "本地主機";
  23. $this->log_file ="";
  24. $this->sock = FALSE;
  25. }
  26. function sendmail($to, $from, $subject = "", $body = "" , $mailtype, $cc = "", $bcc = "", $additional_headers = "")
  27. {
  28. $mail_from = $this->get_address($this->strip_comment($from ));
  29. $body = ereg_replace("(^|(rn))(\.)", "\1.\3", $body);
  30. $header .= "MIME-版本:1.0 rn";
  31. if($mailtype=="HTML")
  32. {
  33. $header .= "內容類型:text/htmlrn";
  34. }
  35. $header .= "至: ".$to ."rn";
  36. if ($cc != "")
  37. {
  38. $header .= "抄送: ".$cc."rn";
  39. }
  40. $header .= "寄件者:$fromrn";
  41. $header .= "主題:".$subject."rn";
  42. $header .= $ extra_headers;
  43. $header .= "日期:".date("r")."rn";
  44. $header .= "X-Mailer:來自Redhat (PHP/".phpversion().") rn";
  45. list($msec, $sec) =explode(" ", microtime());
  46. $header .= "訊息ID: rn";
  47. $TO = Explode(",", $this->strip_comment($to));
  48. if ( $cc != "") {
  49. $TO = array_merge($TO,explode(",", $this->strip_comment($cc)));
  50. }
  51. if ($bcc != "") {
  52. $TO = array_merge($TO,explode(",", $this->strip_comment($bcc)));
  53. }
  54. $sent = TRUE;
  55. foreach ($TO as $rcpt_to) {
  56. $rcpt_to = $this->get_address($rcpt_to);
  57. if (!$this->smtp_sockopen($rcpt_to)) {
  58. $this-> log_write("錯誤:無法傳送電子郵件至".$rcpt_to."n");
  59. $sent = FALSE;
  60. 繼續;
  61. }
  62. if ($this->smtp_send($this ->host_name, $mail_from, $rcpt_to, $header, $body)) {
  63. $this->log_write(" 電子郵件已發送至n");
  64. } else {
  65. $this->log_write("錯誤:無法傳送電子郵件至n");
  66. $sent = FALSE;
  67. }
  68. fclose($this->sock);
  69. $this->log_write("與遠端主機n斷開連接");
  70. }
  71. }
  72. echo "
    ";
  73. echo $header;
  74. return $sent;
  75. }
  76. /* 內部函數*/
  77. function smtp_send($helo, $from, $to, $header, $body = "")
  78. {
  79. if (!$this->smtp_putcmd("HELO", $helo)) {
  80. return $this->; smtp_error("傳送 HELO 指令");
  81. }
  82. #auth
  83. if($this->auth){
  84. if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode ($ this->user))) {
  85. return $this->smtp_error("發送HELO 指令");
  86. }
  87. if (!$this->smtp_putcmd(" ", base64_encode($ this->pass))) {
  88. return $this->smtp_error("發送HELO 指令");
  89. }
  90. }
  91. #
  92. if (! $this->smtp_putcmd(" MAIL", "FROM:")) {
  93. return $this->smtp_error("從指令發送郵件");
  94. }
  95. if (!$ this->smtp_putcmd("RCPT", "TO:")) {
  96. return $this->smtp_error("發送RCPT TO 指令");
  97. }
  98. if (!$this->smtp_putcmd("DATA")) {
  99. return $this->smtp_error("發送DATA 指令") ;
  100. }
  101. if (!$this- >smtp_message($header, $body)) {
  102. return $this->smtp_error("發送訊息");
  103. }
  104. if (!$this->smtp_eom()) {
  105. return $this->smtp_error("正在發送.; [EOM]");
  106. }
  107. if (!$this->smtp_putcmd ("QUIT")) {
  108. return $this->smtp_error("發送QUIT 指令");
  109. }
  110. 回傳 TRUE;
  111. }
  112. function smtp_sockopen($address)
  113. {
  114. if ($this->relay_host == "") {
  115. 回傳$this->smtp_sockopen_mx($address);
  116. } else {
  117. 回傳$this->smtp_sockopen_relay();
  118. }}
  119. function smtp_sockopen_relay()
  120. {
  121. $this->log_write("嘗試".$this->relay_host.":".$this->smtp_port."n" );
  122. $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
  123. if (!( $this->sock && $this ->smtp_ok​​())) {
  124. $this->log_write("錯誤:無法連線到中繼主機".$this->relay_host."n");
  125. $this->log_write( "錯誤:".$errstr." (".$errno.")n");
  126. return FALSE;
  127. }
  128. $this->log_write ("連接到中繼主機".$this ->relay_host."n");
  129. 回傳TRUE;;
  130. }
  131. function smtp_sockopen_mx($address)
  132. {
  133. $domain = ereg_replace("^.@([ ^@] )$", "\1", $address);
  134. if (!@getmxrr($domain, $MXHOSTS)) {
  135. $this- >log_write("錯誤:無法解析MX "" .$domain.""n");
  136. return FALSE;
  137. }
  138. foreach ($MXHOSTS as $host) {
  139. $this- >log_write("嘗試".$host.": ".$this->smtp_port."n");
  140. $this->sock = @fsockopen($host, $this->smtp_port , $errno, $errstr, $this->time_out);
  141. if (!($this->sock && $this->smtp_ok​​())) {
  142. $this->log_write( "警告:無法連線到mx 主機".$host."n");
  143. $this->log_write("錯誤:".$errstr." (".$errno.")n");
  144. 繼續;
  145. }
  146. $this->log_write("連接到mx 主機".$host."n");
  147. 回傳TRUE;
  148. }
  149. $this-> ;log_write("錯誤:無法連線到任何mx 主機(".implode(", ", $MXHOSTS).")n");
  150. return FALSE;
  151. }
  152. function smtp_message( $header, $body)
  153. {
  154. fputs($this->sock $header."rn".$body);
  155. $this->smtp_debug(">; ".str_replace("rn", "n"."> ", $header."n> ".$body. "n> "));
  156. 回傳TRUE;
  157. }
  158. function smtp_eom()
  159. {
  160. fputs($this->sock, "rn.rn" );
  161. $this->smtp_debug(".[EOM]n");
  162. return $this->smtp_ok​​​​();
  163. }
  164. function smtp_ok​​​ ()
  165. {
  166. $response = str_replace("rn", "", fgets ($this->sock, 512));
  167. $this->smtp_debug($response."n") ;
  168. if (!ereg("^[23]", $response )) {
  169. fputs($this->sock, "QUITrn");
  170. fgets($this->sock , 512);
  171. $this->log_write("錯誤: 遠端主機回傳"".$response.""n");
  172. 回傳FALSE;
  173. }
  174. 回傳TRUE;
  175. }
  176. function smtp_putcmd($cmd, $arg = "" )
  177. {
  178. if ($arg != "") {
  179. if($cmd=="") $cmd = $arg;
  180. else $cmd = $cmd." ".$arg;
  181. }
  182. fputs($this->sock, $cmd."rn");
  183. $ this->smtp_debug("> ".$cmd. "n");
  184. return $this->smtp_ok​​();
  185. }
  186. function smtp_error($string)
  187. {
  188. $this->log_write ("錯誤:".$string.".n" 時發生錯誤);
  189. return FALSE;
  190. }
  191. function log_write($ message)
  192. {
  193. $this- >smtp_debug($message);
  194. if ($this->log_file == "") {
  195. 回傳TRUE;
  196. }
  197. $message = date(" M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
  198. if (!@file_exists($ this->log_file) || !( $fp = @fopen($this->log_file, "a"))) {
  199. $this->smtp_debug("警告:無法開啟日誌檔案"".$this- >log_file。 ""n ");
  200. 返回FALSE;
  201. }
  202. flock($fp, LOCK_EX);
  203. fputs($fp, $message);
  204. fclose($fp);
  205. return TRUE;
  206. }
  207. function strip_comment($address)
  208. {
  209. $comment = "\([^()]*\)";
  210. while>$comment = "\([^()]*\)";
  211. while (ereg( $comment, $address)) {
  212. $address = ereg_replace($comment, "", $address);
  213. }
  214. return $address;
  215. }
  216. function get_address($address)
  217. {
  218. $address = ereg_replace("([ trn]) ", "", $address);
  219. $address = ereg_replace("^.*.*$", "\1", $address);
  220. return $address;
  221. }
  222. function smtp_debug($message)
  223. {
  224. if ($this->debug) {
  225. echo $message."
    ";
  226. }}
  227. function get_attach_type($image_tag) { //
  228. $filedata = array();
  229. $img_file_con=fopen($image_tag,"r");
  230. $img_file_con=fopen($image_tag,"r");
  231. 取消設定($image_data);
  232. while ($tem_buffer=AddSlashes(fread($img_file_con,filesize($image_tag))))
  233. $image_data.=$tem_buffer;
  234. fclose; 🎜 >
  235. $filedata['context'] = $image_data;
  236. $filedata['filename']= basename($image_tag);
  237. $extension=substr($image_tag,strrpos($image_tag," . "),strlen($image_tag)-strrpos($image_tag,"."));
  238. switch($extension){
  239. case ".gif":
  240. $filedata['type'] = " image/gif";
  241. break;
  242. case ".gz":
  243. $filedata['type'] = "application/x-gzip";
  244. break;
  245. case ". htm ":
  246. $filedata['type'] = "text/html";
  247. break;
  248. case ".html":
  249. $filedata['type'] = "text/html" ;
  250. break;
  251. case ".jpg":
  252. $filedata['type'] = "image/jpeg";
  253. break;
  254. case ".tar":
  255. $filedata ['type'] = "application/x-tar";
  256. break;
  257. case ".txt":
  258. $filedata['type'] = "text/plain";
  259. break;
  260. case ".zip":
  261. $filedata['type'] = "application/zip";
  262. break;
  263. default:
  264. $filedata['type'] = "application/八位元組流";
  265. break;
  266. }
  267. return $filedata;
  268. }
  269. }
  270. ?>
  271. }
  272. ?>
  273. php
  274. $smtpserver = "smtp.163.com";//SMTP伺服器
  275. $smtpserverport =25;//SMTP伺服器連接埠
  276. $smtpusermail = "caowlong163@163.com";// /SMTP 伺服器的使用者信箱
  277. $smtpemailto = "caowlong@qq.com";//寄送給誰
  278. $smtpuser = "caowlong163@163.com";//SMTP 伺服器的使用者信箱
  279. $ smtppass = "XXX";//SMTP伺服器的使用者密碼
  280. $mailsubject = "PHP100測試郵件系統";//郵件主旨
  281. $mailbody = "

    你的使用者名稱是張三,密碼是11111

    ";//郵件內容
  282. $mailtype = "HTML";//郵件格式(HTML/TXT),TXT為郵件文字
$smtp = new smtp($smtpserver,$smtpserverport, true,$smtpuser,$smtppass);
$smtp->debug = true;//是否顯示發送的調試信息$smtp->sendmail($smtpemailto, $smtpusermail, $subject, $mailbody, $ mailtype);?>

PHP


相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板