-
-
$smtpserver = "*****";
- $smtpserverport = 25;
- $smtpuser = "*** ***";
- $smtppass = "*******";
- $smtp = new smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass); //這裡面的一個true是表示使用身份驗證,否則不使用身份驗證.
- $smtp->debug = false;
- //$emailtype = "HTML";
- for ($i=0; $i $smtp->sendmail("*****", "******", "Hello world!","This is only a test!");
- }
- echo "共發送了$i 封郵件!";
- ?>
複製程式碼
下面是具體類別的實作。
-
-
class smtp {
- /* 公共標記 */
- var $smtpport;
- /* 公共標記 */
- var $smtpport;
- > var $time_out;
- var $host_name;
- var $log_file;
- var $relay_host;
- var $debug;
- var $auth;
- var $user;
- var $🎜> var $ pass;
- /* 儀器*/
- var $sock;
- /* 建構子*/
- function smtp($relay_host = "", $smtp_port = 25, $auth = false, $用戶,$pass)
- {
- $this->debug = false;
- $this->smtp_port = $smtp_port;
- $this->relay_host = $relay_host;
- $this- >超時= 30; //用於fsockopen()
- $this->auth = $auth; //auth
- $this->user = $user;
- $this->pass = $pass;
- $this->host_name = "localhost"; //用於HELO指令
- $this->log_file = "";
- $this->sock = false;
- }
- /* 主要函數 */
- function sendmail ($to , $from, $subject = "", $body = "", $mailtype= "", $cc = "", $bcc = "", $additional_headers = "")
- {
- $mail_from = $this->get_address($this->strip_comment($from));
- $body = ereg_replace("(^|( ))(.)", "1.3", $body);
- $header .= "MIME-Version:1.0 ";
- if ($mailtype == "HTML") {
- $header .= "Content-Type:text/html ";
- }
- $header .= "食譜:" . $ 至 . " ";
- if ($cc != "") {
- $header .= "抄送: " . $抄送。 ">; ";
- $header .= "主題:" . $主題。來自Redhat (PHP/" . phpversion() . ") ";
- list($msec, $sec) =explode(" ", microtime() );
- $header .= "訊息ID: if ($cc != "") {
- $TO = array_merge ($TO,explode(",",$this->strip_comment($cc)));
- }
- if ($bcc != "") {
- $TO = array_merge($TO ,explode(",", $this->strip_comment($bcc)));
- }
- $sent = true;
- foreach ($TO as $rcpt_to) {
- $rcpt_to = $this->get_address($rcpt_to);
- if (!$this->smtp_sockopen($rcpt_to) ) {
- $this->log_write("錯誤:無法發送電子郵件至" 。$rcpt_to . " ");
- $sent = false;
- 繼續;
- }
- if ( $this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
- $this->log_write(" 電子郵件已發送至; ");
- } else {
- $this-> ;log_write("錯誤:無法傳送電子郵件至; ");
- $sent = false;
- }
- fclose($this->sock);
- $this->log_write("與遠端斷開主機連線");
- }
- return $sent;
- }
/* 真空函數*/
- function smtp_send($helo, $from, $to, $header, $body = "")
- {
- if (!$this- >smtp_putcmd("HELO" , $helo)) {
- return $this->smtp_error("發送HELO 指令");
- }
- // auth
- if ($this->auth) {
- if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
- return $this->smtp_error("發送HELO 指令");
- } }
- if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
- return $this->smtp_error("傳送HELO 指令");
- }
- }
- if (!$this->smtp_putcmd("MAIL", "FROM:;")) {
- return $this->smtp_error("從命令發送郵件");
- }
- if (!$this->smtp_putcmd("RCPT", "TO:;")) {
- return $this->smtp_error("發送RCPT TO 指令");
- }
- if (!$this->smtp_putcmd("DATA")) {
- return $this->smtp_error("發送 DATA 指令");
- }
- if (!$this->smtp_message ($header, $body)) {
- return $this->smtp_error("正在傳送訊息");
- }
- if (!$this->smtp_eom()) {
- return $ this->smtp_error("正在發送;;.;; [EOM]");
- }
- if (!$ this->smtp_putcmd("QUIT" )) {
- return $this->smtp_error("發送退出命令");
- }
- return true;
- }
- function smtp_sockopen( $address)
- {
- if ($this->relay_host == "") {
- return $this->smtp_sockopen_mx($address);
- } else {
- return $this->smtp_sockopen_relay();
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- function smtp_sockopen_relay()
- {
- $this->log_write("嘗試" . $this->relay_host . " :" . $this->smtp_port . " ");
- $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
- if (!($this->sock && $this- >smtp_ok())) {
- $this->log_write(" 錯誤:無法連接到中繼主機" 。$this->relay_host 。" ");
- $this->log_write( "錯誤:" . $errstr . " (" . $errno . ") ");
- return false;
- }
- $this- >log_write("連接到中繼主機 " . $this->relay_host 。 " ");
- return true;;
- }
- function smtp_sockopen_mx($address)
- {
- $域 = ereg_replace("^.@([^@] )$", " 1", $address);
- if (!@getmxrr($domain, $MXHOSTS)) {
- $this->; log_write("錯誤:無法解析 MX "" . $domain . "" ");
- return false;
- }
- foreach ($MXHOSTS as $host) {
- $this->log_write ($MXHOSTS as $host) {
- $this->log_write ($MXHOSTS as $host) {
- $this->log_write ( "嘗試" . $host ":" . $this->smtp_port . " ");
- $this->sock=@fsockopen($host, $this->smtp_port, $errno, $errstr, $this - >time_out);
- if (!($this->sock && $this->smtp_ok())) {
- $this->log_write("警告:無法連接到mx 主機" . $主機. " ");
- $this->log_write("錯誤: " . $errstr . " (" . $errno . ") ");
- 繼續;
- }
- $this - >log_write("已連接到mx 主機" . $主機。" ");
- return true;
- }
- $this->log_write("錯誤:無法連接到任何mx 主機(" . implode) (", ", $MXHOSTS) . ") ") ;
- 回傳false;
- }
- function smtp_message($header, $body)
- {
- fputs($this- >sock) , $header . $body);
- $this->smtp_debug(">; " .str_replace(" ", " " . ">; ", $header .">; " . $body . " >; "));
- return true;
- }
- function smtp_eom()
- {
- fputs($this->sock, " . ");
- $this-> smtp_debug(". [ EOM] ");
- return $this->smtp_ok();
- }
- function smtp_ok()
- {
- $response = str_replace(" ", "", fgets($ this->sock, 512));
- $this->smtp_debug($response . " ");
- if (!ereg("^[23]", $response)) {
- fputs($this->sock, "QUIT ");
- 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; }
- function log_write($message)
- {
- $this->smtp_debug($message);
- if ($this->log_file == "") {
- return true;
- }
- $message = date("M d H:i:s ") 。 get_current_user() 。 “[”。 getmypid() 。 ”]:“。 $message;
- if (!@ file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
- $this-> ;smtp_debug("警告: 无法打开日志文件 "" . $this->log_file . "" ");
- return false;;
- }
- 集群($fp, LOCK_EX);
- fputs($fp, $message);
- fclose ($fp);
- return true;
- }
- function strip_comment($address)
- {
- $comment = "([ ^()]*)";
- while ( ereg($comment, $address)) {
- $address = ereg_replace($comment, "", $address);
- }
- return $地址;
- }
- function get_address($ address)
- {
- $address = ereg_replace("([ ]) ", "", $address);
- $address = ereg_replace(" ^.*;.*$" , "1", $address);
- return $address;
- }
- function smtp_debug($message)
- {
- if ($this->debug) {
- echo $訊息 . ";";
- }
- }
- }
- ?>
複製程式碼
|
複製程式碼