Quick Search:
 
 PHP Code: Email attachments with PHP mail() Jump to:  
Category: >> PHP Code >> Email attachments with PHP mail()  

<< lastnext >>

Snippet Name: Email attachments with PHP mail()

Description: Class to send a file as an attachment with the PHP mail() function.

Also see:
» Send iCal Email Meeting Requests using...
» Email with attachments class
» Fake email addresses

Comment: (none)

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

<?PHP
 
CLASS mime_mail 
{ 
VAR $parts; 
VAR $to; 
VAR $from; 
VAR $headers; 
VAR $subject; 
VAR $body; 
 
  /* 
  *     void mime_mail() 
  *     class constructor 
  */          
FUNCTION mime_mail() 
  { 
  $this->parts = ARRAY(); 
  $this->to =  ""; 
  $this->from =  ""; 
  $this->subject =  ""; 
  $this->body =  ""; 
  $this->headers =  ""; 
  } 
 
  /* 
  *     void add_attachment(string message, [string name], [string ctype]) 
  *     Add an attachment to the mail object 
  */ 
FUNCTION add_attachment($message, $name =  "", $ctype =  "application/octet-stream") 
  { 
  $this->parts[] = ARRAY ( 
                           "ctype" => $ctype, 
                           "message" => $message, 
                           "encode" => $encode, 
                           "name" => $name 
                          ); 
  } 
 
/* 
*      void build_message(array part= 
*      Build message parts of an multipart mail 
*/ 
FUNCTION build_message($part) 
{ 
$message = $part[ "message"]; 
$message = CHUNK_SPLIT(BASE64_ENCODE($message)); 
$encoding =  "base64"; 
RETURN  "Content-Type: ".$part[ "ctype"]. 
                        ($part[ "name"]? "; name = \"".$part[ "name"]. "\"" :  ""). 
                         "\nContent-Transfer-Encoding: $encoding\n\n$message\n"; 
} 
 
/* 
*      void build_multipart() 
*      Build a multipart mail 
*/ 
FUNCTION build_multipart() 
{ 
$boundary =  "b".MD5(UNIQID(TIME())); 
$multipart =  "Content-Type: multipart/mixed; boundary = $boundary\n\nThis is a MIME 
encoded message.\n\n--$boundary"; 
 
FOR($i = SIZEOF($this->parts)-1; $i >= 0; $i--) 
    { 
    $multipart .=  "\n".$this->build_message($this->parts[$i]). "--$boundary"; 
    } 
RETURN $multipart.=  "--\n"; 
} 
 
/* 
*      void send() 
*      Send the mail (last class-function to be called) 
*/ 
FUNCTION send() 
{ 
$mime =  ""; 
IF (!EMPTY($this->from)) 
    $mime .=  "From: ".$this->from. "\n"; 
IF (!EMPTY($this->headers)) 
    $mime .= $this->headers. "\n"; 
 
IF (!EMPTY($this->body)) 
    $this->add_attachment($this->body,  "",  "text/plain");    
$mime .=  "MIME-Version: 1.0\n".$this->build_multipart(); 
MAIL($this->to, $this->subject,  "", $mime); 
} 
};  // end of class 
 
?>
 


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