首頁  >  文章  >  後端開發  >  php寫的smtp郵件發送類

php寫的smtp郵件發送類

WBOY
WBOY原創
2016-07-25 08:54:40861瀏覽
複製程式碼
  1. $smtpserver = "*****";
  2. $smtpserverport = 25;
  3. $smtpuser = "*** ***";
  4. $smtppass = "*******";
  5. $smtp = new smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass); //這裡面的一個true是表示使用身份驗證,否則不使用身份驗證.
  6. $smtp->debug = false;
  7. //$emailtype = "HTML";
  8. for ($i=0; $i $smtp->sendmail("*****", "******", "Hello world!","This is only a test!");
  9. }
  10. echo "共發送了$i 封郵件!";
  11. ?>
複製程式碼

下面是具體類別的實作。

  1. class smtp {

  2. /* 公共標記 */
  3. var $smtpport;
  4. /* 公共標記 */
  5. var $smtpport;
  6. > var $time_out;
  7. var $host_name;
  8. var $log_file;
  9. var $relay_host;
  10. var $debug;
  11. var $auth;
  12. var $user;
  13. var $🎜> var $ pass;
  14. /* 儀器*/
  15. var $sock;
  16. /* 建構子*/
  17. function smtp($relay_host = "", $smtp_port = 25, $auth = false, $用戶,$pass)
  18. {
  19. $this->debug = false;
  20. $this->smtp_port = $smtp_port;
  21. $this->relay_host = $relay_host;
  22. $this- >超時= 30; //用於fsockopen()
  23. $this->auth = $auth; //auth
  24. $this->user = $user;
  25. $this->pass = $pass;
  26. $this->host_name = "localhost"; //用於HELO指令
  27. $this->log_file = "";
  28. $this->sock = false;
  29. }
  30. /* 主要函數 */
  31. function sendmail ($to , $from, $subject = "", $body = "", $mailtype= "", $cc = "", $bcc = "", $additional_headers = "")
  32. {
  33. $mail_from = $this->get_address($this->strip_comment($from));
  34. $body = ereg_replace("(^|( ))(.)", "1.3", $body);
  35. $header .= "MIME-Version:1.0 ";
  36. if ($mailtype == "HTML") {
  37. $header .= "Content-Type:text/html ";
  38. }
  39. $header .= "食譜:" . $ 至 . " ";
  40. if ($cc != "") {
  41. $header .= "抄送: " . $抄送。 ">; ";
  42. $header .= "主題:" . $主題。來自Redhat (PHP/" . phpversion() . ") ";
  43. list($msec, $sec) =explode(" ", microtime() );
  44. $header .= "訊息ID: if ($cc != "") {
  45. $TO = array_merge ($TO,explode(",",$this->strip_comment($cc)));
  46. }
  47. if ($bcc != "") {
  48. $TO = array_merge($TO ,explode(",", $this->strip_comment($bcc)));
  49. }
  50. $sent = true;
  51. foreach ($TO as $rcpt_to) {
  52. $rcpt_to = $this->get_address($rcpt_to);
  53. if (!$this->smtp_sockopen($rcpt_to) ) {
  54. $this->log_write("錯誤:無法發送電子郵件至" 。$rcpt_to . " ");
  55. $sent = false;
  56. 繼續;
  57. }
  58. if ( $this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
  59. $this->log_write(" 電子郵件已發送至; ");
  60. } else {
  61. $this-> ;log_write("錯誤:無法傳送電子郵件至; ");
  62. $sent = false;
  63. }
  64. fclose($this->sock);
  65. $this->log_write("與遠端斷開主機連線");
  66. }
  67. return $sent;
  68. }
  69. /* 真空函數*/

  70. function smtp_send($helo, $from, $to, $header, $body = "")
  71. {
  72. if (!$this- >smtp_putcmd("HELO" , $helo)) {
  73. return $this->smtp_error("發送HELO 指令");
  74. }
  75. // auth
  76. if ($this->auth) {
  77. if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
  78. return $this->smtp_error("發送HELO 指令");
  79. } }
  80. if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
  81. return $this->smtp_error("傳送HELO 指令");
  82. }
  83. }
  84. if (!$this->smtp_putcmd("MAIL", "FROM:;")) {
  85. return $this->smtp_error("從命令發送郵件");
  86. }
  87. if (!$this->smtp_putcmd("RCPT", "TO:;")) {
  88. return $this->smtp_error("發送RCPT TO 指令");
  89. }
  90. if (!$this->smtp_putcmd("DATA")) {
  91. return $this->smtp_error("發送 DATA 指令");
  92. }
  93. if (!$this->smtp_message ($header, $body)) {
  94. return $this->smtp_error("正在傳送訊息");
  95. }
  96. if (!$this->smtp_eom()) {
  97. return $ this->smtp_error("正在發送;;.;; [EOM]");
  98. }
  99. if (!$ this->smtp_putcmd("QUIT" )) {
  100. return $this->smtp_error("發送退出命令");
  101. }
  102. return true;
  103. }
  104. function smtp_sockopen( $address)
  105. {
  106. if ($this->relay_host == "") {
  107. return $this->smtp_sockopen_mx($address);
  108. } else {
  109. return $this->smtp_sockopen_relay();
  110. }
  111. }
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }
  125. }
  126. }
  127. }
  128. }
  129. }
  130. function smtp_sockopen_relay()
  131. {
  132. $this->log_write("嘗試" . $this->relay_host . " :" . $this->smtp_port . " ");
  133. $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
  134. if (!($this->sock && $this- >smtp_ok​​())) {
  135. $this->log_write(" 錯誤:無法連接到中繼主機" 。$this->relay_host 。" ");
  136. $this->log_write( "錯誤:" . $errstr . " (" . $errno . ") ");
  137. return false;
  138. }
  139. $this- >log_write("連接到中繼主機 " . $this->relay_host 。 " ");
  140. return true;;
  141. }
  142. function smtp_sockopen_mx($address)
  143. {
  144. $域 = ereg_replace("^.@([^@] )$", " 1", $address);
  145. if (!@getmxrr($domain, $MXHOSTS)) {
  146. $this->; log_write("錯誤:無法解析 MX "" . $domain . "" ");
  147. return false;
  148. }
  149. foreach ($MXHOSTS as $host) {
  150. $this->log_write ($MXHOSTS as $host) {
  151. $this->log_write ($MXHOSTS as $host) {
  152. $this->log_write ( "嘗試" . $host ":" . $this->smtp_port . " ");
  153. $this->sock=@fsockopen($host, $this->smtp_port, $errno, $errstr, $this - >time_out);
  154. if (!($this->sock && $this->smtp_ok​​())) {
  155. $this->log_write("警告:無法連接到mx 主機" . $主機. " ");
  156. $this->log_write("錯誤: " . $errstr . " (" . $errno . ") ");
  157. 繼續;
  158. }
  159. $this - >log_write("已連接到mx 主機" . $主機。" ");
  160. return true;
  161. }
  162. $this->log_write("錯誤:無法連接到任何mx 主機(" . implode) (", ", $MXHOSTS) . ") ") ;
  163. 回傳false;
  164. }
  165. function smtp_message($header, $body)
  166. {
  167. fputs($this- >sock) , $header . $body);
  168. $this->smtp_debug(">; " .str_replace(" ", " " . ">; ", $header .">; " . $body . " >; "));
  169. return true;
  170. }
  171. function smtp_eom()
  172. {
  173. fputs($this->sock, " . ");
  174. $this-> smtp_debug(". [ EOM] ");
  175. return $this->smtp_ok​​​​​​();
  176. }
  177. function smtp_ok​​​​​​()
  178. {
  179. $response = str_replace(" ", "", fgets($ this->sock, 512));
  180. $this->smtp_debug($response . " ");
  181. if (!ereg("^[23]", $response)) {
  182. fputs($this->sock, "QUIT ");
  183. fgets($this->sock ) , 512); $this->log_write("錯誤:遠端主機回傳"" 。$response . "" "); 回傳false; } 回傳true; } function smtp_putcmd($cmd, $arg = "") { if ( $arg != "") { if ($cmd == "") $cmd = $arg ; else $cmd = $cmd 。 」 「。 ; " . $cmd . " "); return $this->smtp_ok​​​​(); } function smtp_error($string) { $this-> log_write("錯誤:發生錯誤,而 " . $string . "); 回傳 false; }
  184. function log_write($message)
  185. {
  186. $this->smtp_debug($message);
  187. if ($this->log_file == "") {
  188. return true;
  189. }
  190. $message = date("M d H:i:s ") 。 get_current_user() 。 “[”。 getmypid() 。 ”]:“。 $message;
  191. if (!@ file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
  192. $this-> ;smtp_debug("警告: 无法打开日志文件 "" . $this->log_file . "" ");
  193. return false;;
  194. }
  195. 集群($fp, LOCK_EX);
  196. fputs($fp, $message);
  197. fclose ($fp);
  198. return true;
  199. }
  200. function strip_comment($address)
  201. {
  202. $comment = "([ ^()]*)";
  203. while ( ereg($comment, $address)) {
  204. $address = ereg_replace($comment, "", $address);
  205. }
  206. return $地址;
  207. }
  208. function get_address($ address)
  209. {
  210. $address = ereg_replace("([ ]) ", "", $address);
  211. $address = ereg_replace(" ^.*;.*$" , "1", $address);
  212. return $address;
  213. }
  214. function smtp_debug($message)
  215. {
  216. if ($this->debug) {
  217. echo $訊息 . ";";
  218. }
  219. }
  220. }
  221. ?>
複製程式碼
複製程式碼


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn