home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / emailclass.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  17.3 KB  |  448 lines

  1. Email Class 
  2.  
  3. This class is used for sending emails. These emails can be Plain Text, HTML, or Both. Other uses include file Attachments and email Templates(from a file). 
  4.  
  5.  
  6.  
  7. <?php
  8. /*******************************************************************************
  9.     Name:            Email
  10.     Description:    This class is used for sending emails.  
  11.             These emails can be
  12.             Plain Text, HTML, or Both. Other uses include file 
  13.             Attachments and email Templates(from a file).
  14.     Testing:
  15.         test_email.php3:
  16.  
  17.         $mail->setTo("myEmail@yo.com");
  18.         $mail->send();
  19.  
  20.                         
  21.     Changelog:
  22.     Date        Name            Description
  23.     ----------- -----------     ------------------------------------------------
  24.     10/21/1999    R.Chambers        created
  25. *******************************************************************************/
  26. /*******************************************************************************
  27.     Issues:
  28.         no error reporting
  29.         can only send HTML with TEXT
  30.         can only send attachements with HTML and TEXT
  31. *******************************************************************************/
  32. /*******************************************************************************
  33.     Function Listing:
  34.         setTo($inAddress)
  35.         setCC($inAddress)
  36.         setBCC($inAddress)
  37.         setFrom($inAddress)
  38.         setSubject($inSubject)
  39.         setText($inText)
  40.         setHTML($inHTML)
  41.         setAttachments($inAttachments)
  42.         checkEmail($inAddress)
  43.         loadTemplate($inFileLocation,$inHash,$inFormat)
  44.         getRandomBoundary($offset)
  45.         getContentType()
  46.         formatTextHeader()
  47.         formatHTMLHeader()
  48.         formatAttachmentHeader($inFileLocation)
  49.         send()
  50. *******************************************************************************/
  51.  
  52. class Email
  53. {
  54.     //---Global Variables
  55.     var $mailTo                = "";                        // array of To addresses
  56.     var $mailCC                = "";                        // copied recipients
  57.     var $mailBCC            = "";                        // hidden recipients
  58.     var $mailFrom            = "";                        // from address
  59.     var $mailSubject        = "";                        // email subject
  60.     var $mailText            = "";                        // plain text message
  61.     var $mailHTML            = "";                        // html message
  62.     var $mailAttachments    = "";                        // array of attachments
  63.  
  64. /*******************************************************************************
  65.     Function:        setTo($inAddress)
  66.     Description:    sets the email To address
  67.     Arguments:        $inAddress as string
  68.                     separate multiple values with comma
  69.     Returns:        true if set
  70. *******************************************************************************/
  71.     function setTo($inAddress){
  72.         //--split addresses at commas
  73.         $addressArray = explode(",",$inAddress);
  74.         //--loop through each address and exit on error
  75.         for($i=0;$i<count($addressArray);$i++){
  76.             if($this->checkEmail($addressArray[$i])==false) return false;
  77.         }
  78.         //--all values are OK so implode array into string
  79.         $this->mailTo = implode($addressArray,",");
  80.         return true;
  81.     }
  82. /*******************************************************************************
  83.     Function:        setCC($inAddress)
  84.     Description:    sets the email cc address
  85.     Arguments:        $inAddress as string
  86.                     separate multiple values with comma
  87.     Returns:        true if set
  88. *******************************************************************************/
  89.     function setCC($inAddress){
  90.         //--split addresses at commas
  91.         $addressArray = explode(",",$inAddress);
  92.         //--loop through each address and exit on error
  93.         for($i=0;$i<count($addressArray);$i++){
  94.             if($this->checkEmail($addressArray[$i])==false) return false;
  95.         }
  96.         //--all values are OK so implode array into string
  97.         $this->mailCC = implode($addressArray,",");
  98.         return true;
  99.     }
  100. /*******************************************************************************
  101.     Function:        setBCC($inAddress)
  102.     Description:    sets the email bcc address
  103.     Arguments:        $inAddress as string
  104.                     separate multiple values with comma
  105.     Returns:        true if set
  106. *******************************************************************************/
  107.     function setBCC($inAddress){
  108.         //--split addresses at commas
  109.         $addressArray = explode(",",$inAddress);
  110.         //--loop through each address and exit on error
  111.         for($i=0;$i<count($addressArray);$i++){
  112.             if($this->checkEmail($addressArray[$i])==false) return false;
  113.         }
  114.         //--all values are OK so implode array into string
  115.         $this->mailBCC = implode($addressArray,",");
  116.         return true;
  117.     }
  118. /*******************************************************************************
  119.     Function:        setFrom($inAddress)
  120.     Description:    sets the email FROM address
  121.     Arguments:        $inAddress as string (takes single email address)
  122.     Returns:        true if set
  123. *******************************************************************************/
  124.     function setFrom($inAddress){
  125.         if($this->checkEmail($inAddress)){
  126.             $this->mailFrom = $inAddress;
  127.             return true;
  128.         }
  129.         return false;
  130.     }
  131. /*******************************************************************************
  132.     Function:        setSubject($inSubject)
  133.     Description:    sets the email subject
  134.     Arguments:        $inSubject as string
  135.     Returns:        true if set
  136. *******************************************************************************/
  137.     function setSubject($inSubject){
  138.         if(strlen(trim($inSubject)) > 0){
  139.             $this->mailSubject = ereg_replace("\n","",$inSubject);
  140.             return true;
  141.         }
  142.         return false;
  143.     }
  144. /*******************************************************************************
  145.     Function:        setText($inText)
  146.     Description:    sets the email text
  147.     Arguments:        $inText as string
  148.     Returns:        true if set
  149. *******************************************************************************/
  150.     function setText($inText){
  151.         if(strlen(trim($inText)) > 0){
  152.             $this->mailText = $inText;
  153.             return true;
  154.         }
  155.         return false;
  156.     }
  157. /*******************************************************************************
  158.     Function:        setHTML($inHTML)
  159.     Description:    sets the email HMTL
  160.     Arguments:        $inHTML as string
  161.     Returns:        true if set
  162. *******************************************************************************/
  163.     function setHTML($inHTML){
  164.         if(strlen(trim($inHTML)) > 0){
  165.             $this->mailHTML = $inHTML;
  166.             return true;
  167.         }
  168.         return false;
  169.     }
  170. /*******************************************************************************
  171.     Function:        setAttachments($inAttachments)
  172.     Description:    stores the Attachment string
  173.     Arguments:        $inAttachments as string with directory included
  174.                     separate multiple values with comma
  175.     Returns:        true if stored
  176. *******************************************************************************/
  177.     function setAttachments($inAttachments){
  178.         if(strlen(trim($inAttachments)) > 0){
  179.             $this->mailAttachments = $inAttachments;
  180.             return true;
  181.         }        
  182.         return false;
  183.     }
  184. /*******************************************************************************
  185.     Function:        checkEmail($inAddress)
  186.     Description:    checks for valid email
  187.     Arguments:        $inAddress as string
  188.     Returns:        true if valid
  189. *******************************************************************************/
  190.     function checkEmail($inAddress){
  191.         return (ereg( "^[^@ ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$",$inAddress));
  192.     }
  193. /*******************************************************************************
  194.     Function:        loadTemplate($inFileLocation,$inHash,$inFormat)
  195.     Description:    reads in a template file and replaces hash values
  196.     Arguments:        $inFileLocation as string with relative directory
  197.                     $inHash as Hash with populated values
  198.                     $inFormat as string either "text" or "html"
  199.     Returns:        true if loaded
  200. *******************************************************************************/
  201.     function loadTemplate($inFileLocation,$inHash,$inFormat){
  202.         /*
  203.         template files have lines such as:
  204.             Dear ~!UserName~,
  205.             Your address is ~!UserAddress~
  206.         */
  207.         //--specify template delimeters
  208.         $templateDelim = "~";
  209.         $templateNameStart = "!";
  210.         //--set out string
  211.         $templateLineOut = "";
  212.         //--open template file
  213.         if($templateFile = fopen($inFileLocation,"r")){
  214.             //--loop through file, line by line
  215.             while(!feof($templateFile)){
  216.                 //--get 1000 chars or (line break internal to fgets)
  217.                 $templateLine = fgets($templateFile,1000);
  218.                 //--split line into array of hashNames and regular sentences
  219.                 $templateLineArray = explode($templateDelim,$templateLine);
  220.                 //--loop through array 
  221.                 for( $i=0; $i<count($templateLineArray);$i++){
  222.                     //--look for $templateNameStart at position 0
  223.                     if(strcspn($templateLineArray[$i],$templateNameStart)==0){
  224.                         //--get hashName after $templateNameStart
  225.                         $hashName = substr($templateLineArray[$i],1);
  226.                         //--replace hashName with acual value in $inHash
  227.                         //--(string) casts all values as "strings"
  228.                         $templateLineArray[$i] = ereg_replace($hashName,(string)$inHash[$hashName],$hashName);
  229.                     }
  230.                 }
  231.                 //--output array as string and add to out string
  232.                 $templateLineOut .= implode($templateLineArray,"");        
  233.             }
  234.             //--close file        
  235.             fclose($templateFile);
  236.             //--set Mail body to proper format
  237.             if( strtoupper($inFormat)=="TEXT" ) return($this->setText($templateLineOut));
  238.             else if( strtoupper($inFormat)=="HTML" ) return($this->setHTML($templateLineOut));
  239.         }
  240.         return false;
  241.     }
  242. /*******************************************************************************
  243.     Function:        getRandomBoundary($offset)
  244.     Description:    returns a random boundary
  245.     Arguments:        $offset as integer - used for multiple calls
  246.     Returns:        string
  247. *******************************************************************************/
  248.     function getRandomBoundary($offset = 0){
  249.         //--seed random number generator
  250.         srand(time()+$offset);
  251.         //--return md5 32 bits plus 4 dashes to make 38 chars
  252.         return ("----".(md5(rand())));
  253.     }
  254. /*******************************************************************************
  255.     Function:        getContentType($inFileName)
  256.     Description:    returns content type for the file type
  257.     Arguments:        $inFileName as file name string (can include path)
  258.     Returns:        string
  259. *******************************************************************************/
  260.     function getContentType($inFileName){
  261.         //--strip path
  262.         $inFileName = basename($inFileName);
  263.         //--check for no extension
  264.         if(strrchr($inFileName,".") == false){
  265.             return "application/octet-stream";
  266.         }
  267.         //--get extension and check cases
  268.         $extension = strrchr($inFileName,".");
  269.         switch($extension){
  270.             case ".gif":    return "image/gif";
  271.             case ".gz":        return "application/x-gzip";
  272.             case ".htm":    return "text/html";
  273.             case ".html":    return "text/html";
  274.             case ".jpg":    return "image/jpeg";
  275.             case ".tar":    return "application/x-tar";
  276.             case ".txt":    return "text/plain";
  277.             case ".zip":    return "application/zip";
  278.             default:        return "application/octet-stream";
  279.         }
  280.         return "application/octet-stream";
  281.     }
  282. /*******************************************************************************
  283.     Function:        formatTextHeader
  284.     Description:    returns a formated header for text
  285.     Arguments:        none
  286.     Returns:        string
  287. *******************************************************************************/
  288.     function formatTextHeader(){
  289.         $outTextHeader = "";
  290.         $outTextHeader .= "Content-Type: text/plain; charset=us-ascii\n";
  291.         $outTextHeader .= "Content-Transfer-Encoding: 7bit\n\n";
  292.         $outTextHeader .= $this->mailText."\n";
  293.         return $outTextHeader;
  294.     }
  295. /*******************************************************************************
  296.     Function:        formatHTMLHeader
  297.     Description:    returns a formated header for HTML
  298.     Arguments:        none
  299.     Returns:        string
  300. *******************************************************************************/
  301.     function formatHTMLHeader(){
  302.         $outHTMLHeader = "";
  303.         $outHTMLHeader .= "Content-Type: text/html; charset=us-ascii\n";
  304.         $outHTMLHeader .= "Content-Transfer-Encoding: 7bit\n\n";
  305.         $outHTMLHeader .= $this->mailHTML."\n";
  306.         return $outHTMLHeader;
  307.     }
  308. /*******************************************************************************
  309.     Function:        formatAttachmentHeader($inFileLocation)
  310.     Description:    returns a formated header for an attachment
  311.     Arguments:        $inFileLocation as string with relative directory
  312.     Returns:        string
  313. *******************************************************************************/
  314.     function formatAttachmentHeader($inFileLocation){
  315.         $outAttachmentHeader = "";
  316.         //--get content type based on file extension
  317.         $contentType = $this->getContentType($inFileLocation);
  318.         //--if content type is TEXT the standard 7bit encoding
  319.         if(ereg("text",$contentType)){
  320.             //--format header
  321.             $outAttachmentHeader .= "Content-Type: ".$contentType.";\n";
  322.             $outAttachmentHeader .= ' name="'.basename($inFileLocation).'"'."\n";
  323.             $outAttachmentHeader .= "Content-Transfer-Encoding: 7bit\n";
  324.             $outAttachmentHeader .= "Content-Disposition: attachment;\n";    //--other: inline
  325.             $outAttachmentHeader .= ' filename="'.basename($inFileLocation).'"'."\n\n";
  326.             $textFile = fopen($inFileLocation,"r");
  327.             //--loop through file, line by line
  328.             while(!feof($textFile)){
  329.                 //--get 1000 chars or (line break internal to fgets)
  330.                 $outAttachmentHeader .= fgets($textFile,1000);
  331.             }
  332.             //--close file        
  333.             fclose($textFile);
  334.             $outAttachmentHeader .= "\n";
  335.         }
  336.         //--NON-TEXT use 64-bit encoding
  337.         else{
  338.             //--format header
  339.             $outAttachmentHeader .= "Content-Type: ".$contentType.";\n";
  340.             $outAttachmentHeader .= ' name="'.basename($inFileLocation).'"'."\n";
  341.             $outAttachmentHeader .= "Content-Transfer-Encoding: base64\n";
  342.             $outAttachmentHeader .= "Content-Disposition: attachment;\n";    //--other: inline
  343.             $outAttachmentHeader .= ' filename="'.basename($inFileLocation).'"'."\n\n";
  344.             //--call uuencode - output is returned to the return array
  345.             exec("uuencode -m $inFileLocation nothing_out",$returnArray);
  346.             //--add each line returned
  347.             for ($i = 1; $i<(count($returnArray)); $i++){ 
  348.                 $outAttachmentHeader .= $returnArray[$i]."\n";
  349.             }
  350.         }    
  351.         return $outAttachmentHeader;
  352.     }
  353. /*******************************************************************************
  354.     Function:        send()
  355.     Description:    sends the email
  356.     Arguments:        none
  357.     Returns:        true if sent
  358. *******************************************************************************/
  359.     function send(){
  360.         //--set  mail header to blank
  361.         $mailHeader = "";
  362.         //--add CC
  363.         if($this->mailCC != "") $mailHeader .= "CC: ".$this->mailCC."\n";
  364.         //--add BCC
  365.         if($this->mailBCC != "") $mailHeader .= "BCC: ".$this->mailBCC."\n";
  366.         //--add From
  367.         if($this->mailFrom != "") $mailHeader .= "FROM: ".$this->mailFrom."\n";
  368.  
  369.         //---------------------------MESSAGE TYPE-------------------------------
  370.         //--TEXT ONLY
  371.         if($this->mailText != "" && $this->mailHTML == "" && $this->mailAttachments == ""){
  372.             return mail($this->mailTo,$this->mailSubject,$this->mailText,$mailHeader);
  373.         }        
  374.         //--HTML AND TEXT
  375.         else if($this->mailText != "" && $this->mailHTML != "" && $this->mailAttachments == ""){
  376.             //--get random boundary for content types
  377.             $bodyBoundary = $this->getRandomBoundary();
  378.             //--format headers
  379.             $textHeader = $this->formatTextHeader();
  380.             $htmlHeader = $this->formatHTMLHeader();
  381.             //--set MIME-Version
  382.             $mailHeader .= "MIME-Version: 1.0\n";
  383.             //--set up main content header with boundary
  384.             $mailHeader .= "Content-Type: multipart/alternative;\n";
  385.             $mailHeader .= ' boundary="'.$bodyBoundary.'"';
  386.             $mailHeader .= "\n\n\n";
  387.             //--add body and boundaries
  388.             $mailHeader .= "--".$bodyBoundary."\n";
  389.             $mailHeader .= $textHeader;
  390.             $mailHeader .= "--".$bodyBoundary."\n";
  391.             //--add html and ending boundary
  392.             $mailHeader .= $htmlHeader;
  393.             $mailHeader .= "\n--".$bodyBoundary."--";
  394.             //--send message
  395.             return mail($this->mailTo,$this->mailSubject,"",$mailHeader);
  396.         }
  397.         //--HTML AND TEXT AND ATTACHMENTS
  398.         else if($this->mailText != "" && $this->mailHTML != "" && $this->mailAttachments != ""){
  399.             
  400.             //--get random boundary for attachments
  401.             $attachmentBoundary = $this->getRandomBoundary();
  402.             //--set main header for all parts and boundary
  403.             $mailHeader .= "Content-Type: multipart/mixed;\n";
  404.             $mailHeader .= ' boundary="'.$attachmentBoundary.'"'."\n\n";
  405.             $mailHeader .= "This is a multi-part message in MIME format.\n";
  406.             $mailHeader .= "--".$attachmentBoundary."\n";
  407.             
  408.             //--TEXT AND HTML--
  409.             //--get random boundary for content types
  410.             $bodyBoundary = $this->getRandomBoundary(1);
  411.             //--format headers
  412.             $textHeader = $this->formatTextHeader();
  413.             $htmlHeader = $this->formatHTMLHeader();
  414.             //--set MIME-Version
  415.             $mailHeader .= "MIME-Version: 1.0\n";
  416.             //--set up main content header with boundary
  417.             $mailHeader .= "Content-Type: multipart/alternative;\n";
  418.             $mailHeader .= ' boundary="'.$bodyBoundary.'"';
  419.             $mailHeader .= "\n\n\n";
  420.             //--add body and boundaries
  421.             $mailHeader .= "--".$bodyBoundary."\n";
  422.             $mailHeader .= $textHeader;
  423.             $mailHeader .= "--".$bodyBoundary."\n";
  424.             //--add html and ending boundary
  425.             $mailHeader .= $htmlHeader;
  426.             $mailHeader .= "\n--".$bodyBoundary."--";
  427.             //--send message
  428.             //--END TEXT AND HTML
  429.             
  430.             //--get array of attachment filenames
  431.             $attachmentArray = explode(",",$this->mailAttachments);
  432.             //--loop through each attachment
  433.             for($i=0;$i<count($attachmentArray);$i++){
  434.                 //--attachment separator
  435.                 $mailHeader .= "\n--".$attachmentBoundary."\n";
  436.                 //--get attachment info
  437.                 $mailHeader .= $this->formatAttachmentHeader($attachmentArray[$i]);
  438.             }
  439.             $mailHeader .= "--".$attachmentBoundary."--";
  440.             return mail($this->mailTo,$this->mailSubject,"",$mailHeader);
  441.         }
  442.         return false;
  443.     }
  444. }
  445. ?>
  446.  
  447.  
  448.