SMTP命令郵件投遞(無身份認證)

本類參考了網上關於PHP的MIME  MAIL和SMTP發送協議的文章,本類可以在*NUX或WIN平臺下並且無需SMTP服務器,直接通過郵件專遞方式送到郵件接收方的郵件服務器中。

      需要注意的是在發送大尺寸郵件時,建議在服務器允許MAIL()發送的情況下,儘量開啓'usemail'=>true,因爲用PHP的SOCKE 方式發送效率相對於MAIL()函數來說要低;另外要說明的是由於採用郵件專遞的方式郵件沒有本地暫存發送,爲了可靠性和避免長時間等待的過時問題,所以 在沒有啓用PHP的MAIL函數時,本類沒有添加支持用通過分號分隔的多帳號同時發送的功能。

     本類在nease.net的Linux+php5虛擬主機空間(該空間不能使用PHP自帶的MAIL函數發郵件,寫此類當時就是爲了在這個空間下發送郵件)及win2003+IIS6+php4.3上測試成功。

簡單範例:

<?php
    require("mailer.class.php");
    $mailer=new  mailer();
    $mailer->mConfig['mailform']="[email protected]";
    $isended=$mailer->mail("[email protected]","the  subject","hello","From: [email protected]");
    if($isended)
    {
         echo "true";
    }
    else
    {
        echo "false";
    }
?>

類文件: mailer.class.php

<?php 
/** 
* Project:     Dowebs.Lib: the PHP compiling template  engine 
* File:        mailer.lib.php 
* 
* This library is opensource  software; 
* @link http://www.dowebs.net/ 
* @copyright  2004-2005 dowebs.net studio. 
* @author Xie Hengduo [email protected] 
* @package System  
* @version 1.0.0.0 
*/ 
class mailer{ 
    var $mConfig=array(  
        'dns'    =>"202.96.104.16",                //設置解域MX記錄的域名服務器  
        'mailform'    =>"[email protected]",//發送郵件的帳號  
        'charset'    =>"utf-8",                    //HTML附件編碼 
         'usemail'    =>false                        //是否使用PHP中的MAIL函數 
    );  
    var $dns_repl_domain=""; 
    var $arrMX    =array(); 
    var  $error    ="";
    var $aattach    =array(); 
    var $xheaders    =array(); 
    var  $ctencoding    = "7bit"; 
    var $priorities = array( '1 (Highest)', '2  (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)' ); 
    var $content_type='';  
     
    function mailer() 
    { 
         
    } 
      
    //兼容的mail函數 
    function  mail($to,$subject,$message,$additional_headers='') 
    { 
         if($this->mConfig['usemail']) 
        { 
            return  mail($to,$subject,$message,$additional_headers); 
        } 
        else  
        { 
            if($this->queryMailStmp($to)) 
            {  
                $this->error="2 收件人郵箱地址非法"; 
                return  false; 
            } 
            //連接服務器 
             $is_sockopen=false; 
            $hostname=""; 
             foreach($this->arrMX as $value) 
            { 
                if($fp  = fsockopen ($value, 25, $errno, $errstr, 60)) 
                {  
                    $hostname=$value; 
                     $is_sockopen=true; 
                    break; 
                }  
            } 
            if (!$is_sockopen){$this->error="3  聯接服務器失敗";return false;}
            //HELO 
            fputs($fp,"HELO {$hostname}\r\n");  
            $lastmessage = fgets($fp,1024); 
             if(substr($lastmessage,0,3) != 220 ){$this->error="4 錯誤信息$lastmessage";return  false;} 
            //FROM: 
            fputs( $fp,"MAIL  FROM:<{$this->mConfig['mailform']}>"."\r\n"); 
             $lastmessage = fgets ($fp,1024); 
            if (substr($lastmessage,0,3) !=  250){$this->error="5 錯誤信息$lastmessage";return false;} 
            //TO:  
            fputs( $fp,"RCPT TO:<$to>"."\r\n"); 
             $lastmessage = fgets ($fp,1024); 
            if (substr($lastmessage,0,3) !=  250){$this->error="6 錯誤信息$lastmessage";return false;} 
             while(true){ 
                $lastmessage = fgets($fp,512);  
                if((substr($lastmessage,3,1) !=  "-")||(empty($lastmessage)))break; 
            } 
            //DATA  
            fputs($fp,"DATA\r\n"); 
            $lastmessage = fgets  ($fp,1024); 
            if (substr($lastmessage,0,3) !=  354){$this->error="7 錯誤信息$lastmessage";return false;} 
             $message="\n".$message; 
            if($additional_headers!='')  
            { 
                $message =  $additional_headers."\r\n".$message; 
            } 
            //處理To頭  
            $head="To: $to\r\n"; 
            $message = $head.$message;  
            //處理Subject頭 
            $head="Subject: $subject\r\n";  
            $message = $head.$message; 
            //加上結束串  
            $message .= "\r\n.\r\n"; 
            //發送信息 
             fputs($fp, $message); 
            fputs($fp,"QUIT\r\n"); 
             fclose($fp); 
            return true; 
        } 
    }
    //郵址初步校驗 
    function is_valid_email ($email = "") 
    {  
        return preg_match('/^[.\w-]+@([\w-]+\.)+[a-zA-Z]{2,6}$/',$email);  
    }
    function queryMailStmp($mailaddr) 
    { 
         if($this->is_valid_email($mailaddr)) 
        { 
             $this->mxlookup(substr(strstr($mailaddr,'@'),1)); 
        } 
         else 
        { 
            return false; 
        } 
    } 
     //取得MX記錄 
    function mxlookup($domain) 
    { 
         if(function_exists('getmxrr')) 
        { 
             getmxrr($domain,$this->arrMX); 
        } 
        else 
        {  
            ########################## 
            $dot_pos = 0; $temp =  ""; 
            $QNAME=""; 
             while($dot_pos=strpos($domain,".")) 
            { 
                 $temp   = substr($domain,0,$dot_pos); 
                $domain =  substr($domain,$dot_pos+1); 
                $QNAME .=  chr(strlen($temp)).$temp; 
            } 
            $QNAME .=  chr(strlen($domain)).$domain.chr(0); 
            ###########################  
            $dns_packet =  
chr(0).chr(1).chr(1).chr(0).chr(0).chr(1).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).$QNAME.chr(0).chr(15).chr(0).chr(1);  
            $dns_socket = fsockopen("udp://{$this->mConfig['dns']}", 53);  
            fwrite($dns_socket,$dns_packet,strlen($dns_packet));  
            $this->dns_reply = fread($dns_socket,1); 
             $bytes = stream_get_meta_data($dns_socket); 
            $this->dns_reply  .= fread($dns_socket,$bytes['unread_bytes']); 
             fclose($dns_socket); 
            $this->cIx=6; 
             $this->Total_Accounts=""; 
            for($i=0;$i<2;$i++){  
                 $this->Total_Accounts.=ord(substr($this->dns_reply,$this->cIx,1));  
                $this->cIx++; 
            } 
             $this->cIx+=4; 
             $this->parse_data($this->dns_repl_domain); 
             $this->cIx+=7; 
             for($ic=1;$ic<=$this->Total_Accounts;$ic++) { 
                $QTYPE =  ord($this->gdi($this->cIx)); 
                if($QTYPE!==15){  
                    print("<font color='red' size='4'  
face='verdana'><B>[No MX Records 
returned]</b></font>");  
                    die(); 
                } 
                 $this->cIx+=9; 
                $mxPref =  ord($this->gdi($this->cIx)); 
                 $this->parse_data($curmx); 
                $this->arrMX[] =$curmx;  
                $this->cIx+=3; 
            } 
        } 
    }  
    function gdi(&$cIx,$bytes=1) { 
        $this->cIx++;  
        return(substr($this->dns_reply, $this->cIx-1, $bytes));  
    } 
    function parse_data(&$retval) { 
        $arName =  array(); 
        $byte = ord($this->gdi($this->cIx)); 
         while($byte!==0) { 
            if($byte==192) { //compressed     
                $tmpIx = $this->cIx; 
                $this->cIx =  ord($this->gdi($cIx)); 
                $tmpName = $retval;  
                $this->parse_data($tmpName); 
                $retval  = $retval.".".$tmpName; 
                $this->cIx = $tmpIx+1;  
                return; 
            } 
            $retval="";  
            $bCount = $byte; 
            for($b=0;$b<$bCount;$b++) {  
                $retval .= $this->gdi($this->cIx); 
            }  
            $arName[]=$retval; 
            $byte =  ord($this->gdi($this->cIx)); 
        } 
         $retval=join(".",$arName); 
    } 
     
     ############################################ 
    # MIME MAIL CODE 
     ############################################ 
    function From( $from="")  
    { 
        if($from=='') 
        { 
             $this->xheaders['From']=$this->mConfig['mailform']; 
        }  
        else 
        { 
            $this->xheaders['From'] =  $from; 
        } 
    }
    function mimemail($to,$subject,$message) 
    { 
         $this->boundary= "--" . md5( uniqid("myboundary") ); 
        if(  $this->mConfig['charset'] != "us-ascii" ) 
        { 
             $this->ctencoding = "8bit"; 
        } 
        $headers = "";  
        if( $this->mConfig['charset'] != "") { 
            //global  $contenttype; 
            $this->xheaders["Mime-Version"] = "1.0";  
            $this->xheaders["Content-Type"] = "{$this->content_type};  charset={$this->mConfig['charset']}"; 
             $this->xheaders["Content-Transfer-Encoding"] = $this->ctencoding;  
        } 
        $this->xheaders["X-Mailer"] = "Dowebs.net Mailer";  
        if( count( $this->aattach ) > 0 ) 
        {  
            $this->xheaders["Content-Type"] = "multipart/mixed;\n  boundary=\"{$this->boundary}\""; 
            $fullBody="This is a  multi-part message in MIME format.\n--{$this->boundary}\n"; 
             $fullBody.= "Content-Type: text/html;  
charset={$this->mConfig['charset']}\nContent-Transfer-Encoding:{$this->ctencoding}\n\n".message."\n";  
            $sep= chr(13) . chr(10); 
            $ata= array();  
            $k=0; 
            for( $i=0; $i < count(  $this->aattach); $i++ ) { 
                $filename =  $this->aattach[$i]; 
                $basename = basename($filename);  
                $ctype = $this->actype[$i];        // content-type  
                $disposition = $this->adispo[$i]; 
                 if(!file_exists( $filename)){$this->error="1 Class 
Mail, method attach : file  {$filename} can't be found";return false;} 
                $subhdr=  "--{$this->boundary}\nContent-Type: 
{$ctype};\n  name=\"{$basename}\";\nContent-Transfer-Encoding: 
base64\nContent-Disposition:  {$disposition};\n 
filename=\"{$basename}\"\n"; 
                $ata[$k++] =  $subhdr; 
                $linesz= filesize( $filename)+1;  
                $fp= fopen( $filename, 'r' ); 
                $ata[$k++]  = chunk_split(base64_encode(fread( $fp, $linesz))); 
                 fclose($fp); 
            } 
            $fullBody .= implode($sep, $ata);  
        } 
        else 
        { 
            $fullBody =  $message; 
        } 
        reset($this->xheaders); 
         while( list( $hdr,$value ) = each( $this->xheaders)) { 
             $headers .= "$hdr: $value\n"; 
        } 
         
        return  $this->mail($to,$subject,$fullBody,$headers); 
    } 
    function  Attach($filename,$filetype = "",$disposition = "inline") 
    {  
            if( $filetype == "" ) 
            { 
                 $filetype = "application/x-unknown-content-type"; 
            }  
            $this->aattach[] = $filename; 
             $this->actype[] = $filetype; 
            $this->adispo[] =  $disposition; 
    }
    function Content_type($contenttype) 
    { 
         $this->content_type=$contenttype; 
    } 
     
    function  Priority($priority) 
    { 
        if(!intval($priority))return false;  
        if(!isset($this->priorities[$priority-1]))return false;  
        $this->xheaders["X-Priority"] =  $this->priorities[$priority-1]; 
        return true; 
    }

} 
?>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章