Quick Search:
CODE
Oracle PL/SQL Code Library
JOBS
Find Or Post Oracle Jobs
FORUM
Oracle Discussion & Chat
 PHP Code: Email attachments with PHP mail() Jump to:  
Category: >> PHP Code >> Email attachments with PHP mail() Bookmark and Share

<< 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 w/ 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 
 
?>
 


Free
Oracle Magazine
Subscriptions
and Oracle White Papers


SQL University.net courses meet the most demanding needs of the business world for advanced education in a cost-effective manner. SQL University.net courses are available immediately for IT professionals and can be taken without disruption of your workplace schedule or processes.

Compared to traditional travel-based training, SQL University.net saves time and valuable corporate resources, allowing companies to do more with less. That's our mission, and that's what we deliver.

Click here to find out more
 
Home      :      Code Library      :      Sponsors      :      Privacy      :      Terms of Use      :      Contact Us 86 users online    © 2009 psoug.org

PSOUG LOGIN
Username: 
Password: 
Forgot your password?