<?php

/* mimemailer.php
 * Simple class for sending E-mail messages in MIME-format with attachments, version 1.0.
 *
 * (c) 2008 Kipmans
 *
 * Released under "THE BEER-WARE LICENSE" (Revision 42):
 * <kipmans@gmail.com> wrote this file. As long as you retain this notice you can 
 * do whatever you want with this stuff. If we meet some day, and you think this 
 * stuff is worth it, you can buy me a beer in return.
 */

Interface MimeMailer_interface
{
    public function    
__construct();
    public function    
set_subject($subject);
    public function    
set_message($message$content_type "text/plain");
    public function    
add_attachment($filename);
    public function    
send($address);
}


class 
MimeMailer implements MimeMailer_interface
{
    private    
$subject;
    private    
$message;
    private    
$content_type;
    private    
$attachments;
    
    
    public function 
__construct()
    {
        
$this->subject "";
        
$this->message "";
        
$this->content_type "text/plain";
        
$this->attachments = array();
    }
    
    
    public function 
set_subject($subject)
    {
        
$this->subject $subject;
    }
    
    
    public function 
set_message($message$content_type "text/plain")
    {
        
$this->message $message;
        
$this->content_type $content_type;
    }
    
    
    public function 
add_attachment($filename)
    {
        
$this->attachments[] = $filename;
    }
    
    
    public function 
send($address)
    {
        
$boundary "__BOUNDARY_"uniqid();
        
        
$headers "MIME-version: 1.0\r\n".
                
"Content-type: multipart/mixed; boundary=\""$boundary ."\"";
        
        
$message "This is a multipart message in MIME format. If you see this message, please upgrade ".
                
"your e-mail client to a MIME-compliant version.\r\n".
                
"--"$boundary ."\r\n".
                
"Content-type: "$this->content_type ."\r\n".
                
"Content-transfer-encoding: 7bit\r\n\r\n".
                
$this->message ."\r\n--"$boundary;
                
        foreach(
$this->attachments as $a)
        {
            
$message .= "\r\nContent-type: application/octet-stream\r\n".
                    
"Content-transfer-encoding: base64\r\n".
                    
"Content-disposition: attachment; filename=\""end(explode("/"$a)) ."\"\r\n\r\n".
                    
chunk_split(base64_encode(file_get_contents($a))). "\r\n".
                    
"--"$boundary;
        }
        
        
$message .= "--\r\n\r\n";
        
        
mail($address$this->subject$message$headers);
    }
}

?>