隨筆-60  評論-35  文章-15  trackbacks-0
          功能齊全的發送PHP郵件類

          PHP--下面這個類的功能則很強大,不但能發html格式的郵件,還可以發附件
          <?php
          class Email {
          //---設置全局變量
          var $mailTo = ""; // 收件人
          var $mailCC = ""; // 抄送
          var $mailBCC = ""; // 秘密抄送
          var $mailFrom = ""; // 發件人
          var $mailSubject = ""; // 主題
          var $mailText = ""; // 文本格式的信件主體
          var $mailHTML = ""; // html格式的信件主體
          var $mailAttachments = ""; // 附件
          /* 函數setTo($inAddress) :用于處理郵件的地址 參數 $inAddress
          為包涵一個或多個字串,email地址變量,使用逗號來分割多個郵件地址
          默認返回值為true
          **********************************************************/
          function setTo($inAddress){
          //--用explode()函數根據”,”對郵件地址進行分割
          $addressArray = explode( ",",$inAddress);
          //--通過循環對郵件地址的合法性進行檢查
          for($i=0;$i<count($addressArray);$i++){ if($this->checkEmail($addressArray[$i])==false) return false; }
          //--所有合法的email地址存入數組中
          $this->mailTo = implode($addressArray, ",");
          return true; }
          /**************************************************
          函數 setCC($inAddress) 設置抄送人郵件地址
          參數 $inAddress 為包涵一個或多個郵件地址的字串,email地址變量,
          使用逗號來分割多個郵件地址 默認返回值為true
          **************************************************************/
          function setCC($inAddress){
          //--用explode()函數根據”,”對郵件地址進行分割
          $addressArray = explode( ",",$inAddress);
          //--通過循環對郵件地址的合法性進行檢查
          for($i=0;$i<count($addressArray);$i++){ if($this->checkEmail($addressArray[$i])==false) return false; }
          //--所有合法的email地址存入數組中
          $this->mailCC = implode($addressArray, ",");
          return true; }
          /***************************************************
          函數setBCC($inAddress) 設置秘密抄送地址 參數 $inAddress 為包涵一個或多
          個郵件地址的字串,email地址變量,使用逗號來分割多個郵件地址 默認返回值為
          true
          ******************************************/
          function setBCC($inAddress){
          //--用explode()函數根據”,”對郵件地址進行分割
          $addressArray = explode( ",",$inAddress);
          //--通過循環對郵件地址的合法性進行檢查
          for($i=0;$i<count($addressArray);$i++)
          { if($this->checkEmail($addressArray[$i])==false)
          return false;
          }
          //--所有合法的email地址存入數組中
          $this->mailBCC = implode($addressArray, ",");
          return true;
          }
          /*****************************************************************
          函數setFrom($inAddress):設置發件人地址 參數 $inAddress 為包涵郵件
          地址的字串默認返回值為true
          ***************************************/
          function setFrom($inAddress){
          if($this->checkEmail($inAddress)){
          $this->mailFrom = $inAddress;
          return true;
          } return false; }
          /**********************
          函數 setSubject($inSubject) 用于設置郵件主題參數$inSubject為字串,
          默認返回的是true
          *******************************************/
          function setSubject($inSubject){
          if(strlen(trim($inSubject)) > 0){
          $this->mailSubject = ereg_replace( "n", "",$inSubject);
          return true; }
          return false; }
          /****************************************************
          函數setText($inText) 設置文本格式的郵件主體參數 $inText 為文本內容默
          認返回值為true
          ****************************************/
          function setText($inText){
          if(strlen(trim($inText)) > 0){
          $this->mailText = $inText;
          return true; }
          return false;
          }
          /**********************************
          函數setHTML($inHTML) 設置html格式的郵件主體參數$inHTML為html格式,
          默認返回值為true
          ************************************/
          function setHTML($inHTML){
          if(strlen(trim($inHTML)) > 0){
          $this->mailHTML = $inHTML;
          return true; }
          return false; }
          /**********************
          函數 setAttachments($inAttachments) 設置郵件的附件 參數$inAttachments
          為一個包涵目錄的字串,也可以包涵多個文件用逗號進行分割 默認返回值為true
          *******************************************/
          function setAttachments($inAttachments){
          if(strlen(trim($inAttachments)) > 0){
          $this->mailAttachments = $inAttachments;
          return true; }
          return false; }
          /*********************************
          函數 checkEmail($inAddress) :這個函數我們前面已經調用過了,主要就是
          用于檢查email地址的合法性
          *****************************************/
          function checkEmail($inAddress){
          return (ereg( "^[^@ ]+@([a-zA-Z0-9-]+.)+([a-zA-Z0-9-]{2}|net|com|gov|mil|org|edu|int)$",$inAddress));
          }
          /*************************************************
          函數loadTemplate($inFileLocation,$inHash,$inFormat) 讀取臨時文件并且
          替換無用的信息參數$inFileLocation用于定位文件的目錄
          $inHash 由于存儲臨時的值 $inFormat 由于放置郵件主體
          ***********************************************************/
          function loadTemplate($inFileLocation,$inHash,$inFormat){
          /* 比如郵件內有如下內容: Dear ~!UserName~,
          Your address is ~!UserAddress~ */
          //--其中”~!”為起始標志”~”為結束標志
          $templateDelim = "~";
          $templateNameStart = "!";
          //--找出這些地方并把他們替換掉
          $templateLineOut = ""; //--打開臨時文件
          if($templateFile = fopen($inFileLocation, "r")){
          while(!feof($templateFile)){
          $templateLine = fgets($templateFile,1000);
          $templateLineArray = explode($templateDelim,$templateLine);
          for( $i=0; $i<count($templateLineArray);$i++){
          //--尋找起始位置
          if(strcspn($templateLineArray[$i],$templateNameStart)==0){
          //--替換相應的值
          $hashName = substr($templateLineArray[$i],1);
          //--替換相應的值
          $templateLineArray[$i] = ereg_replace($hashName,(string)$inHash[$hashName],$hashName);
          }
          }
          //--輸出字符數組并疊加
          $templateLineOut .= implode($templateLineArray, "");
          } //--關閉文件fclose($templateFile);
          //--設置主體格式(文本或html)
          if( strtoupper($inFormat)== "TEXT" )
          return($this->setText($templateLineOut));
          else if( strtoupper($inFormat)== "HTML" )
          return($this->setHTML($templateLineOut));
          } return false;
          }
          /*****************************************
          函數 getRandomBoundary($offset) 返回一個隨機的邊界值
          參數 $offset 為整數 – 用于多管道的調用 返回一個md5()編碼的字串
          ****************************************/
          function getRandomBoundary($offset = 0){
          //--隨機數生成
          srand(time()+$offset);
          //--返回 md5 編碼的32位 字符長度的字串
          return ( "----".(md5(rand()))); }
          /********************************************
          函數: getContentType($inFileName)用于判斷附件的類型
          **********************************************/
          function getContentType($inFileName){
          //--去除路徑
          $inFileName = basename($inFileName);
          //--去除沒有擴展名的文件
          if(strrchr($inFileName, ".") == false){
          return "application/octet-stream";
          }
          //--提區擴展名并進行判斷
          $extension = strrchr($inFileName, ".");
          switch($extension){
          case ".gif": return "image/gif";
          case ".gz": return "application/x-gzip";
          case ".htm": return "text/html";
          case ".html": return "text/html";
          case ".jpg": return "image/jpeg";
          case ".tar": return "application/x-tar";
          case ".txt": return "text/plain";
          case ".zip": return "application/zip";
          default: return "application/octet-stream";
          }
          return "application/octet-stream";
          }
          /**********************************************
          函數formatTextHeader把文本內容加上text的文件頭
          *****************************************************/
          function formatTextHeader(){ $outTextHeader = "";
          $outTextHeader .= "Content-Type: text/plain;
          charset=us-asciin";
          $outTextHeader .= "Content-Transfer-Encoding: 7bitnn";
          $outTextHeader .= $this->mailText. "n";
          return $outTextHeader;
          } /************************************************
          函數formatHTMLHeader()把郵件主體內容加上html的文件頭
          ******************************************/
          function formatHTMLHeader(){
          $outHTMLHeader = "";
          $outHTMLHeader .= "Content-Type: text/html;
          charset=us-asciin";
          $outHTMLHeader .= "Content-Transfer-Encoding: 7bitnn";
          $outHTMLHeader .= $this->mailHTML. "n";
          return $outHTMLHeader;
          }
          /**********************************
          函數 formatAttachmentHeader($inFileLocation) 把郵件中的附件標識出來
          ********************************/
          function formatAttachmentHeader($inFileLocation){
          $outAttachmentHeader = "";
          //--用上面的函數getContentType($inFileLocation)得出附件類型
          $contentType = $this->getContentType($inFileLocation);
          //--如果附件是文本型則用標準的7位編碼
          if(ereg( "text",$contentType)){
          $outAttachmentHeader .= "Content-Type: ".$contentType. ";n";
          $outAttachmentHeader .= ' name="'.basename($inFileLocation). '"'. "n";
          $outAttachmentHeader .= "Content-Transfer-Encoding: 7bitn";
          $outAttachmentHeader .= "Content-Disposition: attachment;n";
          $outAttachmentHeader .= ' filename="'.basename($inFileLocation). '"'. "nn";
          $textFile = fopen($inFileLocation, "r");
          while(!feof($textFile)){
          $outAttachmentHeader .= fgets($textFile,1000);
          }
          //--關閉文件 fclose($textFile);
          $outAttachmentHeader .= "n";
          }
          //--非文本格式則用64位進行編碼
          else{ $outAttachmentHeader .= "Content-Type: ".$contentType. ";n";
          $outAttachmentHeader .= ' name="'.basename($inFileLocation). '"'. "n";
          $outAttachmentHeader .= "Content-Transfer-Encoding: base64n";
          $outAttachmentHeader .= "Content-Disposition: attachment;n";
          $outAttachmentHeader .= ' filename="'.basename($inFileLocation). '"'. "nn";
          //--調用外部命令uuencode進行編碼
          exec( "uuencode -m $inFileLocation nothing_out",$returnArray);
          for ($i = 1; $i<(count($returnArray)); $i++){
          $outAttachmentHeader .= $returnArray[$i]. "n";
          }
          } return $outAttachmentHeader;
          }
          /******************************
          函數 send()用于發送郵件,發送成功返回值為true
          ************************************/
          function send(){
          //--設置郵件頭為空
          $mailHeader = "";
          //--添加抄送人
          if($this->mailCC != "")
          $mailHeader .= "CC: ".$this->mailCC. "n";
          //--添加秘密抄送人
          if($this->mailBCC != "")
          $mailHeader .= "BCC: ".$this->mailBCC. "n";
          //--添加發件人
          if($this->mailFrom != "")
          $mailHeader .= "FROM: ".$this->mailFrom. "n";
          //---------------------------郵件格式------------------------------
          //--文本格式
          if($this->mailText != "" && $this->mailHTML == "" && $this->mailAttachments == ""){
          return mail($this->mailTo,$this->mailSubject,$this->mailText,$mailHeader);
          }
          //--html或text格式
          else if($this->mailText != "" && $this->mailHTML != "" && $this->mailAttachments == ""){
          $bodyBoundary = $this->getRandomBoundary();
          $textHeader = $this->formatTextHeader();
          $htmlHeader = $this->formatHTMLHeader();
          //--設置 MIME-版本
          $mailHeader .= "MIME-Version: 1.0n";
          $mailHeader .= "Content-Type: multipart/alternative;n";
          $mailHeader .= ' boundary="'.$bodyBoundary. '"';
          $mailHeader .= "nnn";
          //--添加郵件主體和邊界
          $mailHeader .= "--".$bodyBoundary. "n";
          $mailHeader .= $textHeader;
          $mailHeader .= "--".$bodyBoundary. "n";
          //--添加html標簽
          $mailHeader .= $htmlHeader;
          $mailHeader .= "n--".$bodyBoundary. "--";
          //--發送郵件
          return mail($this->mailTo,$this->mailSubject, "",$mailHeader);
          }
          //--文本加html加附件
          else if($this->mailText != "" && $this->mailHTML != "" && $this->mailAttachments != ""){
          $attachmentBoundary = $this->getRandomBoundary();
          $mailHeader .= "Content-Type: multipart/mixed;n";
          $mailHeader .= ' boundary="'.$attachmentBoundary. '"'. "nn";
          $mailHeader .= "This is a multi-part message in MIME format.n";
          $mailHeader .= "--".$attachmentBoundary. "n";
          $bodyBoundary = $this->getRandomBoundary(1);
          $textHeader = $this->formatTextHeader();
          $htmlHeader = $this->formatHTMLHeader();
          $mailHeader .= "MIME-Version: 1.0n";
          $mailHeader .= "Content-Type: multipart/alternative;n";
          $mailHeader .= ' boundary="'.$bodyBoundary. '"';
          $mailHeader .= "nnn";
          $mailHeader .= "--".$bodyBoundary. "n";
          $mailHeader .= $textHeader;
          $mailHeader .= "--".$bodyBoundary. "n";
          $mailHeader .= $htmlHeader;
          $mailHeader .= "n--".$bodyBoundary. "--";
          //--獲取附件值
          $attachmentArray = explode( ",",$this->mailAttachments);
          //--根據附件的個數進行循環
          for($i=0;$i<count($attachmentArray);$i++){
          //--分割 $mailHeader .= "n--".$attachmentBoundary. "n";
          //--附件信息
          $mailHeader .= $this->formatAttachmentHeader($attachmentArray[$i]);
          }
          $mailHeader .= "--".$attachmentBoundary. "--";
          return mail($this->mailTo,$this->mailSubject, "",$mailHeader);
          }
          return false;
          }
          }
          ?>


          使用方法:
          <?
          Include “email.class”

          $mail->setTo("a@a.com"); //收件人
          $mail-> setCC("b@b.com,c@c.com"); //抄送
          $mail-> setCC("d@b.com,e@c.com"); //秘密抄送
          $mail->setFrom(“f@f.com”);//發件人
          $mail->setSubject(“主題”) ; //主題
          $mail->setText(“文本格式”) ;//發送文本格式也可以是變量
          $mail->setHTML(“html格式”) ;//發送html格式也可以是變量
          $mail->setAttachments(“c:a.jpg”) ;//添加附件,需表明路徑
          $mail->send(); //發送郵件
          posted on 2007-01-24 12:56 Q系列類、方法、變量…… 閱讀(180) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 赤峰市| 外汇| 榆社县| 安远县| 苍南县| 峨眉山市| 门头沟区| 樟树市| 宣汉县| 永年县| 鹤岗市| 新泰市| 乐陵市| 舟曲县| 贵阳市| 叙永县| 永州市| 安岳县| 丰顺县| 大悟县| 吉安市| 三都| 汶川县| 义乌市| 泾川县| 新建县| 凉城县| 小金县| 张家口市| 宽城| 米易县| 曲阳县| 阳新县| 会泽县| 慈溪市| 饶平县| 绵阳市| 师宗县| 博湖县| 花莲县| 托克逊县|