<?php
	if (!isset($MIMEMail_included)) {
	
	$MIMEMail_included = 1;
	
	// This code was written to make it easier to send MIME email. If you
	// find it useful, thank me privately. tlack@modernmethod.com
	// Bug reports to tlack@modernmethod.com
	
	// This is based largely on http://renoir.vill.edu/~ylee/mailfile.html

	// Some notes:
	// - This sucks up N bytes of memory while processing file attachments,
	//   where N is the size of the file.
	// - If your PHP3 installation isn't recent enough to feature
	//   base64_encode, you're doomed.
	// - If your PHP3 installation isn't recent enough to feature 
	//   chunk_split, change the "chunk_split" line in _encodeFile to use
	//   my_chunk_split instead.
	
	class mime_email {
	
		var $subject;
		var $to;
		var $from;
		var $headers = "";
		var $body = "";
		var $boundary;
		
		function mime_email($to, $from, $subject, $mainbody)
		{
			$this->to = $to;
			$this->from = $from;
			$this->subject = $subject;
			$this->body = $mainbody;
			$this->boundary = "%_======_next_part____" . getmypid() . (double)(microtime() * 10000);
			$this->insert_header("From: $from");
			$this->insert_header("MIME-Version: 1.0");
			$this->insert_header("Content-Type: multipart/alternative; boundary=\"$this->boundary\"");
			$this->insert_header("Content-Transfer-Encoding: 7bit");
		}
		
		// Insert a raw line into the headers
		// Do not append \r\n .. we do this for you
		function insert_header($header)
		{
			$this->headers .= $header . "\n";
		}
		
		// Insert a mime text attachment
		function attach_printable($type, $text)
		{
			$this->body .= "\n\n";
			$this->body .= "--$this->boundary\n";
			$this->body .= "Content-Type: $type; charset=\"iso-8859-1\"\n";
			#$this->body .= "Content-Transfer-Encoding: quoted-printable\n\n";
			#$this->body .= qp_enc($text);
			$this->body .= "\n";
			$this->body .= $text;
		}
		
		// Attach a file to the message, and recommend that the filename
		// $remotefilename be assigned on the other end.
		function attach_file($type, $localfilename, $remotefilename)
		{
			$this->body .= "\n";
			$this->body .= "--$this->boundary\n";
			$this->body .= "Content-type: $type; name=\"$remotefilename\"\n";
			$this->body .= "Content-transfer-encoding: base64\n";
			$this->body .= "Content-disposition: attachment; filename=\"$remotefilename\"\n\n";
			$this->body .= $this->_encodeFile($localfilename) . "\n";
		}
		
		// This taken from
		// http://renoir.vill.edu/~ylee/mailfile.txt
		function _encodeFile($fname)
		{
			$encoded = "";
			if (is_readable($fname)) {
				$f = fopen($fname, "r");
				$contents = fread($f, filesize($fname));
				$encoded = chunk_split(base64_encode($contents));
				fclose($f);
			}
			return $encoded;
		}
		
		function send()
		{
			$this->body .= "\n--$this->boundary--";
			mail($this->to, $this->subject, $this->body, $this->headers);
		}
	}
	
	// This taken from http://renoir.vill.edu/~ylee/mailfile.txt
	function my_chunk_split($str)
	{
        $stmp = $str;
        $len = strlen($stmp);
        $out = "";
        while ($len > 0) {
                if ($len >= 76) {
                        $out = $out . substr($stmp, 0, 76) . "\r\n";
                        $stmp = substr($stmp, 76);
                        $len = $len - 76;
                }
                else {
                        $out = $out . $stmp . "\r\n";
                        $stmp = ""; $len = 0;
                }
        }
        return $out;
	}

	function qp_enc($input = "quoted-printable encoding test string", $line_max = 76) {
		$hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
		$lines = preg_split("/(?:\r\n|\r|\n)/", $input);
		$eol = "\r\n";
		$escape = "=";
		$output = "";

		while( list(, $line) = each($lines) ) {
			//$line = rtrim($line); // remove trailing white space -> no =20\r\n necessary
			$linlen = strlen($line);
			$newline = "";
			for($i = 0; $i < $linlen; $i++) {
				$c = substr($line, $i, 1);
				$dec = ord($c);
				if ( ($dec == 32) && ($i == ($linlen - 1)) ) { 
				// convert space at eol only
					$c = "=20";
				} elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { 
					// always encode "\t", which is *not* required
					$h2 = floor($dec/16); $h1 = floor($dec%16);
					$c = $escape.$hex["$h2"].$hex["$h1"];
				}
				if ( (strlen($newline) + strlen($c)) >= $line_max ) { 
					// CRLF is not counted
					$output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
					$newline = "";
				}
			$newline .= $c;
			} // end of for
			$output .= $newline.$eol;
		}
		return trim($output);
	} 
	
	}
?>