Quick Search:
 
 PHP Code: Email with attachments class Jump to:  
Category: >> PHP Code >> Email with attachments class  

<< lastnext >>

Snippet Name: Email with attachments class

Description: Class for sending mail with MIME attachments in multipart format using
external sendmail, mimencode and zip.

Also see:
» Send iCal Email Meeting Requests using...
» Email attachments with PHP mail()
» Fake email addresses

Comment: (none)

Language: PHP
Highlight Mode: PHP
Last Modified: March 16th, 2009

<?PHP
 
CLASS MAIL { 
    VAR $from; //The sender 
    VAR $to; //The recipient 
    VAR $subject; //The subject line 
    VAR $headers; //A hash of additional headers (headername => headervalue) 
    VAR $zipname; //The name of the file the attachments are zipped into 
                  //($zipname == false if attachments are to be sent 
                      //individually) 
    VAR $attachments; //An array of files to attach 
    VAR $body; //The body text of the message 
 
    //Mail constructor: initializes vars to default values. 'Nuff said. 
    FUNCTION MAIL() { 
        $this->from = ""; 
        $this->to = ""; 
        $this->subject = ""; 
        $this->headers = ARRAY(); 
        $this->zipname = FALSE; 
        $this->attachments = ARRAY(); 
        $this->body = ""; 
    } 
 
    //Auxiliary method, used to guess a file's MIME type 
    //based on its extension. Doesn't know about too many 
    //extensions right now 
    FUNCTION guessMIMEType($filename) { 
        //GUESS MIME TYPE 
        $filename = BASENAME($filename); 
        IF(STRRCHR($filename,".") == FALSE) { 
            RETURN("application/octet-stream"); 
        } 
 
        $ext = STRRCHR($filename,"."); 
        SWITCH($ext) { 
            CASE ".gif": 
                RETURN "image/gif"; 
                BREAK; 
            CASE ".gz": 
                RETURN "application/x-gzip"; 
            CASE ".htm": 
            CASE ".html": 
                RETURN "text/html"; 
                BREAK; 
            CASE ".jpg": 
                RETURN "image/jpeg"; 
                BREAK; 
            CASE ".tar": 
                RETURN "application/x-tar"; 
                BREAK; 
            CASE ".txt": 
                RETURN "text/plain"; 
                BREAK; 
            CASE ".zip": 
                RETURN "application/zip"; 
                BREAK; 
            DEFAULT: 
                RETURN "application/octet-stream"; 
                BREAK; 
        } 
    } 
 
    //Cute little convenience method. Supply it with a filename to 
    //zip attachments to, or supply it with false if attachments are 
    //sent individually 
    FUNCTION ZipAttachments($name) { 
        $this->zipname = $name; 
    } 
 
    //The workhorse method, does the actually sending of the mail. 
    //Doesn't check for errors so be careful! 
    FUNCTION Send($sendmail = "sendmail") { 
        IF($this->from == "") 
            $fp = POPEN($sendmail . " -i " . $this->to, "w"); 
        ELSE 
            $fp = POPEN($sendmail . " -i -f\"" . $this->from . "\" 
" . $this->to, "w"); 
 
        $mime_boundary = "-1747901728-1448367683-913849620=:4553"; 
 
        IF($fp == FALSE) 
            RETURN FALSE; 
 
        //Write subject header     
        FWRITE($fp,"Subject: " . $this->subject . "\n"); 
 
        //Write user-defined headers     
        RESET($this->headers); 
        WHILE(LIST($hdrname,$hdrval) = EACH($this->headers)) { 
            FWRITE($fp,$hdrname . ": " . $hdrval . "\n"); 
        } 
 
        //If there are attachments, this needs to be a MIME message 
        IF(COUNT($this->attachments) > 0) { 
            //Write MIME headers 
            FWRITE($fp,"MIME-Version: 1.0\n"); 
            FWRITE($fp,"Content-Type: multipart/mixed; BOUNDARY=\"" 
. $mime_boundary . "\"\n"); 
            FWRITE($fp,"\n"); 
            //Write dummy message body 
            FWRITE($fp,"  This message is in MIME format.  The 
first part should be readable text,\n"); 
            FWRITE($fp,"  while the remaining parts are likely 
unreadable without MIME-aware tools.\n"); 
            FWRITE($fp,"\n"); 
 
            //Write message text 
            FWRITE($fp,"--" . "$mime_boundary" . "\n"); 
            FWRITE($fp,"Content-Type: text/plain; charset=US- 
ASCII\n"); 
            FWRITE($fp,"\n"); 
            FWRITE($fp,$this->body); 
            FWRITE($fp,"\n"); 
 
            //Handle attachments 
            IF($this->zipname != FALSE) { //IF we've been told to 
                                                      //zip the attachments 
                FWRITE($fp,"--" . $mime_boundary . "\n"); 
                FWRITE($fp,"Content-Type: application/zip; name= 
\"". $this->zipname . "\"\n"); 
                FWRITE($fp,"Content-Transfer-Encoding: base64 
\n"); 
                  //fwrite($fp,"Content-ID: " . $content_ID . "\n"); 
                FWRITE($fp,"Content-Description:\n"); 
                FWRITE($fp,"\n"); 
                $cmdline = "zip - "; 
        WHILE(LIST($key, $attachment_name) =  EACH($this->attachments)) 
                    $cmdline .= "$attachment_name "; 
                $cmdline .= "| mimencode -b"; 
                $pp = POPEN($cmdline,"r"); 
                WHILE(!FEOF($pp)) { 
                    $data = FREAD($pp,4096); 
                    FWRITE($fp,$data); 
                } 
                PCLOSE($pp); 
            } 
            ELSE { //no need to zip the attachments, attach them 
                               //separately 
        WHILE(LIST($key, $attachment_name) = EACH($this->attachments)) { 
                FWRITE($fp,"--" . $mime_boundary . "\n"); 
      FWRITE($fp,"Content-Type: " . $this->guessMIMEType($attachment_name) . "; 
      name=\"". BASENAME($attachment_name) . "\"\n"); 
            FWRITE($fp,"Content-Transfer-Encoding: base64\n"); 
                    //fwrite($fp,"Content-ID: " . 
                                        //$content_ID . "\n"); 
                    FWRITE($fp,"Content-Description:\n"); 
                    FWRITE($fp,"\n"); 
                     $pp = POPEN("mimencode -b $attachment_name","r"); 
                    WHILE(!FEOF($pp)) { 
                        $data = FREAD($pp,4096); 
                        FWRITE($fp,$data); 
                    } 
                    PCLOSE($pp); 
                } 
            } 
 
            FWRITE($fp,"--" . $mime_boundary . "--\n"); 
        } 
        //No need for a MIME message, so it's an RFC822 message 
        ELSE { 
            FWRITE($fp,"\n"); 
            FWRITE($fp,$this->body); 
        } 
 
 
        PCLOSE($fp); 
    } 
} 
?>
 


 
   Home |    Search |    Code Library |    Sponsors |    Privacy |    Terms of Use |    Contact Us © 2003 - 2024 psoug.org