email.inc
上传用户:xuanqunsh
上传日期:2007-01-04
资源大小:58k
文件大小:15k
源码类别:

WEB邮件程序

开发平台:

PHP

  1. <?php
  2. # ---------------------------------------------------------------
  3. # phpop
  4. # A WWW based POP3 basic mail user agent (MUA)
  5. # Copyright (C) 1999  Padraic Renaghan
  6. # Licensed under terms of GNU General Public License
  7. # (see http://www.renaghan.com/phpop/source/LICENSE)
  8. # ---------------------------------------------------------------
  9. # $Id: email.inc,v 1.4 2000/04/12 23:23:20 prenagha Exp $
  10. # ---------------------------------------------------------------
  11. # This class modified from the original at
  12. # http://phpbuilder.px.sklar.com/code-pretty.html?code_id=233
  13. #
  14. /*******************************************************************************
  15.   Name:      Email.inc
  16.   Description:  This class is used for sending emails.  
  17.       These emails can be
  18.       Plain Text, HTML, or Both. Other uses include file 
  19.       Attachments and email Templates(from a file).
  20.   Example Usage:
  21.     $mail->setTo("myEmail@yo.com");
  22.     $mail->send();
  23. *******************************************************************************/
  24. class email_class
  25. {
  26.   //---Global Variables
  27.   var $mailTo        = "";           // array of To addresses
  28.   var $mailCC        = "";           // copied recipients
  29.   var $mailBCC      = "";            // hidden recipients
  30.   var $mailFrom      = "";           // from address
  31.   var $mailSubject  = "";            // email subject
  32.   var $mailAddlHdrs  = "";           // email additional headers
  33.   var $mailText      = "";           // plain text message
  34.   var $mailHTML      = "";           // html message
  35.   var $mailAttachments  = "";        // array of attachments
  36.   var $mailAttachmentsNice  = "";    // array of attachments nice names
  37.   var $mimeBoundary = "";            // Boundary to separate mime parts
  38. /*******************************************************************************
  39.   Function:    setTo($inAddress)
  40.   Description:  sets the email To address
  41.   Arguments:    $inAddress as string
  42.           separate multiple values with comma
  43.   Returns:    true if set
  44. *******************************************************************************/
  45.   function setTo($inAddress){
  46.     //--split addresses at commas
  47.     $addressArray = explode(",",$inAddress);
  48.     //--loop through each address and exit on error
  49.     for($i=0;$i<count($addressArray);$i++){
  50.       if($this->checkEmail($addressArray[$i])==false) return false;
  51.     }
  52.     //--all values are OK so implode array into string
  53.     $this->mailTo = implode($addressArray,",");
  54.     return true;
  55.   }
  56. /*******************************************************************************
  57.   Function:    setCC($inAddress)
  58.   Description:  sets the email cc address
  59.   Arguments:    $inAddress as string
  60.           separate multiple values with comma
  61.   Returns:    true if set
  62. *******************************************************************************/
  63.   function setCC($inAddress){
  64.     //--split addresses at commas
  65.     $addressArray = explode(",",$inAddress);
  66.     //--loop through each address and exit on error
  67.     for($i=0;$i<count($addressArray);$i++){
  68.       if($this->checkEmail($addressArray[$i])==false) return false;
  69.     }
  70.     //--all values are OK so implode array into string
  71.     $this->mailCC = implode($addressArray,",");
  72.     return true;
  73.   }
  74. /*******************************************************************************
  75.   Function:    setBCC($inAddress)
  76.   Description:  sets the email bcc address
  77.   Arguments:    $inAddress as string
  78.           separate multiple values with comma
  79.   Returns:    true if set
  80. *******************************************************************************/
  81.   function setBCC($inAddress){
  82.     //--split addresses at commas
  83.     $addressArray = explode(",",$inAddress);
  84.     //--loop through each address and exit on error
  85.     for($i=0;$i<count($addressArray);$i++){
  86.       if($this->checkEmail($addressArray[$i])==false) return false;
  87.     }
  88.     //--all values are OK so implode array into string
  89.     $this->mailBCC = implode($addressArray,",");
  90.     return true;
  91.   }
  92. /*******************************************************************************
  93.   Function:    setFrom($inAddress)
  94.   Description:  sets the email FROM address
  95.   Arguments:    $inAddress as string (takes single email address)
  96.   Returns:    true if set
  97. *******************************************************************************/
  98.   function setFrom($inAddress){
  99.     if($this->checkEmail($inAddress)){
  100.       $this->mailFrom = $inAddress;
  101.       return true;
  102.     }
  103.     return false;
  104.   }
  105. /*******************************************************************************
  106.   Function:    setSubject($inSubject)
  107.   Description:  sets the email subject
  108.   Arguments:    $inSubject as string
  109.   Returns:    true if set
  110. *******************************************************************************/
  111.   function setSubject($inSubject){
  112.     if(strlen(trim($inSubject)) > 0){
  113.       $this->mailSubject = ereg_replace("n","",$inSubject);
  114.       return true;
  115.     }
  116.     return false;
  117.   }
  118. /*******************************************************************************
  119.   Function:    setAddlHdrs($inAddlHdrs)
  120.   Description:  sets the email additional headers
  121.   Arguments:    $inAddlHdrs as string
  122.   Returns:    true if set
  123. *******************************************************************************/
  124.   function setAddlHdrs($inAddlHdrs){
  125.     if(strlen(trim($inAddlHdrs)) > 0){
  126.       $this->mailAddlHdrs = $inAddlHdrs;
  127.       return true;
  128.     }
  129.     return false;
  130.   }
  131. /*******************************************************************************
  132.   Function:    setText($inText)
  133.   Description:  sets the email text
  134.   Arguments:    $inText as string
  135.   Returns:    true if set
  136. *******************************************************************************/
  137.   function setText($inText){
  138.     if(strlen(trim($inText)) > 0){
  139.       $this->mailText = $inText;
  140.       return true;
  141.     }
  142.     return false;
  143.   }
  144. /*******************************************************************************
  145.   Function:    setHTML($inHTML)
  146.   Description:  sets the email HMTL
  147.   Arguments:    $inHTML as string
  148.   Returns:    true if set
  149. *******************************************************************************/
  150.   function setHTML($inHTML){
  151.     if(strlen(trim($inHTML)) > 0){
  152.       $this->mailHTML = $inHTML;
  153.       return true;
  154.     }
  155.     return false;
  156.   }
  157. /*******************************************************************************
  158.   Function:    setAttachments($inAttachments)
  159.   Description:  stores the Attachment string
  160.   Arguments:    $inAttachments as string with directory included
  161.                 $inAttachmentsNiceName as string without directory and nice extension.
  162.     the nice name is used because if you use the PHP upload filename, you loose
  163.     the nice extension which helps us build the correct mime body.
  164.           separate multiple values with comma
  165.   Returns:    true if stored
  166. *******************************************************************************/
  167.   function setAttachments($inAttachmentsFileName, $inAttachmentsNiceName){
  168.     if(strlen(trim($inAttachmentsFileName)) > 0){
  169.       $this->mailAttachments     = $inAttachmentsFileName;
  170.       $this->mailAttachmentsNice = $inAttachmentsNiceName;
  171.       return true;
  172.     }    
  173.     return false;
  174.   }
  175. /*******************************************************************************
  176.   Function:    checkEmail($inAddress)
  177.   Description:  checks for valid email
  178.   Arguments:    $inAddress as string
  179.   Returns:    true if valid
  180. *******************************************************************************/
  181.   function checkEmail($inAddress){
  182.     return (ereg( "^[^@ ]+@([a-zA-Z0-9-]+.)+([a-zA-Z0-9-]{2}|net|com|gov|mil|org|edu|int)$",$inAddress));
  183.   }
  184. /*******************************************************************************
  185.   Function:    setRandomBoundry($offset)
  186.   Description:  sets a random boundary
  187.   Returns:    string
  188. *******************************************************************************/
  189.   function setRandomBoundary(){
  190.     srand(time());
  191.     $this->mimeBoundary = ("------MIME-PART-BOUNDARY-" . md5(rand()));
  192.   }
  193.   function getStartBoundary(){
  194.     return ("--" . $this->mimeBoundary . "n");
  195.   }
  196.   function getEndBoundary(){
  197.     return ("--" . $this->mimeBoundary . "--" . "n");
  198.   }
  199. /*******************************************************************************
  200.   Function:    getContentType($inFileName)
  201.   Description:  returns content type for the file type
  202.   Arguments:    $inFileName as file name string (can include path)
  203.   Returns:    string
  204. *******************************************************************************/
  205.   function getContentType($inFileName){
  206.     //--strip path
  207.     $inFileName = basename($inFileName);
  208.     //--check for no extension
  209.         if(strrchr($inFileName,".") == false){
  210.       return "application/octet-stream";
  211.     }
  212.     //--get extension and check cases
  213.         $extension = strrchr($inFileName,".");
  214.         switch($extension){
  215.             case ".doc":  return "application/msword";
  216.             case ".gif":  return "image/gif";
  217.             case ".gz":   return "application/x-gzip";
  218.             case ".htm":  return "text/html";
  219.             case ".html": return "text/html";
  220.             case ".jpg":  return "image/jpeg";
  221.             case ".tar":  return "application/x-tar";
  222.             case ".txt":  return "text/plain";
  223.             case ".zip":  return "application/zip";
  224.             default:    return "application/octet-stream";
  225.         }
  226.     return "application/octet-stream";
  227.   }
  228. /*******************************************************************************
  229.   Function:    formatTextHeader
  230.   Description:  returns a formated header for text
  231.   Arguments:    none
  232.   Returns:    string
  233. *******************************************************************************/
  234.   function formatTextHeader(){
  235.     $outTextHeader = "";
  236.     $outTextHeader .= "Content-Type: text/plain; charset=us-asciin";
  237.     $outTextHeader .= "Content-Transfer-Encoding: 7bitnn";
  238.     $outTextHeader .= $this->mailText."n";
  239.     return $outTextHeader;
  240.   }
  241. /*******************************************************************************
  242.   Function:    formatHTMLHeader
  243.   Description:  returns a formated header for HTML
  244.   Arguments:    none
  245.   Returns:    string
  246. *******************************************************************************/
  247.   function formatHTMLHeader(){
  248.     $outHTMLHeader = "";
  249.     $outHTMLHeader .= "Content-Type: text/html; charset=us-asciin";
  250.     $outHTMLHeader .= "Content-Transfer-Encoding: 7bitnn";
  251.     $outHTMLHeader .= $this->mailHTML."n";
  252.     return $outHTMLHeader;
  253.   }
  254. /*******************************************************************************
  255.   Function:    formatAttachmentHeader($inFileLocation)
  256.   Description:  returns a formated header for an attachment
  257.   Arguments:    $inFileLocation as string with relative directory
  258.   Returns:    string
  259. *******************************************************************************/
  260.   function formatAttachmentHeader($inFileLocation, $inNiceFileName){
  261.     $outAttachmentHeader = "";
  262.     //--get content type based on file extension
  263.     $contentType = $this->getContentType($inNiceFileName);
  264.     //--get contents of attachment file
  265.     $aFile = fopen($inFileLocation,"rb");
  266.     $aFileContents = fread($aFile, 2000000);
  267.     fclose($aFile);
  268.     //--if content type is TEXT the standard 7bit encoding
  269.     if(ereg("text",$contentType)){
  270.       //--format header
  271.       $outAttachmentHeader .= "Content-Type: ".$contentType.";n";
  272.       $outAttachmentHeader .= ' name="'.basename($inNiceFileName).'"'."n";
  273.       $outAttachmentHeader .= "Content-Transfer-Encoding: 7bitn";
  274.       $outAttachmentHeader .= "Content-Disposition: inline;n";  //--other: inline
  275.       $outAttachmentHeader .= ' filename="'.basename($inNiceFileName).'"'."nn";
  276.       $outAttachmentHeader .= $aFileContents."n";
  277.     }
  278.     //--NON-TEXT use 64-bit encoding
  279.     else{
  280.       //--format header
  281.       $outAttachmentHeader .= "Content-Type: ".$contentType.";n";
  282.       $outAttachmentHeader .= ' name="'.basename($inNiceFileName).'"'."n";
  283.       $outAttachmentHeader .= "Content-Transfer-Encoding: base64n";
  284.       $outAttachmentHeader .= "Content-Disposition: inline;n";  //--other: inline
  285.       $outAttachmentHeader .= ' filename="'.basename($inNiceFileName).'"'."nn";
  286.       $outAttachmentHeader .= chunk_split (base64_encode($aFileContents));
  287.       $outAttachmentHeader .= "n";
  288.     }  
  289.     return $outAttachmentHeader;
  290.   }
  291. /*******************************************************************************
  292.   Function:    send()
  293.   Description:  sends the email
  294.   Arguments:    none
  295.   Returns:    true if sent
  296. *******************************************************************************/
  297.   function send(){
  298. #
  299. # make sure we have something to send.
  300. #
  301.     if  (empty($this->mailText) 
  302.      &&  empty($this->mailHTML) 
  303.      &&  empty($this->mailAttachments) ) {
  304.       return false;
  305.     }
  306.     //--set  mail header to blank
  307.     $mailHeader = "";
  308.     //--add CC
  309.     if($this->mailCC != "") $mailHeader .= "cc: ".$this->mailCC."n";
  310.     //--add BCC
  311.     if($this->mailBCC != "") $mailHeader .= "bcc: ".$this->mailBCC."n";
  312.     //--add From
  313.     if($this->mailFrom != "") $mailHeader .= "From: ".$this->mailFrom."n";
  314.     //--add Addl Headers
  315.     if(!empty($this->mailAddlHdrs)) $mailHeader .= $this->mailAddlHdrs."n";
  316. #
  317. # if there are any attachments or html in the message,
  318. # then we need to use MIME. Otherwise we'll just send
  319. # old-fashioned text.
  320. #
  321.     if($this->mailText != "" && $this->mailHTML == "" && $this->mailAttachments == ""){
  322.       return mail($this->mailTo,$this->mailSubject,$this->mailText,$mailHeader);
  323.     }    
  324.     
  325.     else {
  326. // see RFC2045, 2046, 2049  for MIME Specifications
  327. // http://www.faqs.org/rfcs/rfc2045.html
  328.       
  329.       //--get random boundary for deliniating mime parts
  330.       $this->setRandomBoundary();
  331.       //--ADDL MIME MESSAGE HEADERS
  332.       $mailHeader .= "MIME-Version: 1.0n";
  333.       $mailHeader .= "Content-Type: multipart/mixed;n";
  334.       $mailHeader .= ' boundary="'.$this->mimeBoundary.'"'."nn";
  335.       // MESSAGE BODY START
  336.       $mailHeader .= "This is a multi-part message in MIME format...n";
  337.       
  338.       //--TEXT PART
  339.       if (!empty($this->mailText)) {
  340.         $mailHeader .= $this->getStartBoundary();
  341.         $mailHeader .= $this->formatTextHeader();
  342.       }
  343.       //--HTML PART
  344.       if (!empty($this->mailHTML)) {
  345.         $mailHeader .= $this->getStartBoundary();
  346.         $mailHeader .= $this->formatHTMLHeader();
  347.       }
  348.       //--ATTACHMENT PARTS
  349.       if (!empty($this->mailAttachments)) {
  350.         //--get array of attachment filenames
  351.         $attachmentArray     = explode(",",$this->mailAttachments);
  352.         $attachmentArrayNice = explode(",",$this->mailAttachmentsNice);
  353.         //--loop through each attachment
  354.         for($i=0;$i<count($attachmentArray);$i++){
  355.           //--Add each attachment
  356.           $mailHeader .= $this->getStartBoundary();
  357.           $mailHeader .= $this->formatAttachmentHeader($attachmentArray[$i], $attachmentArrayNice[$i]);
  358.         }
  359.       }
  360.       // FINAL MIME Boundry
  361.       $mailHeader .= $this->getEndBoundary();
  362.       // Send the message
  363.       return mail($this->mailTo,$this->mailSubject,"",$mailHeader);
  364.     }
  365.   }
  366. }
  367. ?>