bestudeer deze klasse eens, moet het mee lukken.. su6
<?php
/************************************
author : Eric Kerst
class : MIME_mail
description : class for mail distribution
version : 1.0
date (la) : 12/13/00 10:54AM
updates:
-
known problems/bugs:
-
future updates:
-
**************************************/
define("GMT", "+0200");
define("LF", "\r\n");
define("TAB", "\t");
//define("LF", "<br>");
define("BODY", md5("BODY"));
define("WARNING", "This message is in MIME format. The first part should be readable text, \r\nwhile the remaining parts are likely unreadable without MIME-aware tools.");
class MIME_mail {
// Declaration of public variables
var $From;
var $To;
var $Cc;
var $Bcc;
var $Subject;
var $Body;
var $Email = "";
// Declaration of private variables
var $Mimeparts = array();
// Constructor
function MIME_mail($From, $To, $Cc="", $Bcc="", $Subject="No subject", $Body) {
//Func - Constructs a MIME mail
//Pre - $From=true, $To=true, $Body=true
//Post - MIME_mail=true
// Assign given values to public variables
$this->From = $From;
$this->To = $To;
$this->Cc = $Cc;
$this->Bcc = $Bcc;
$this->Subject = $Subject;
if ($Body =="") $this->Body = "no content"; else $this->Body = $Body;
}
// Methods
function Attach($Data, $Description="", $Contenttype, $Encoding="base64", $Fname="") {
//Func - Creates attachment and puts it on the Mimeparts stack
//Pre - $Data=true, if only data is entered, it we'll be coded in base64
//Post - true (Mimeparts[+1]) or false
// When no data found return false
if (empty($Data)) return false;
// If description is not empty, prepare it for insert in attachmentheader
if (!($Description=="")) $Descheader = "Content-Description: ".$Description.LF;
// Check which encoding type is used
switch ($Encoding) {
// When qp is found, rename the encoding "qouted-printable"
case "qp" :$Encoding="quoted-printable";
$Emsg=$Data;
break;
// When encodig is BASE64, encode it with php func. base64_enc
case "base64" : $Emsg = base64_encode($Data);
break;
}
// Split message in chunks to match RFC 2045 semantics
$Emsg = chunk_split($Emsg);
// Check which contenttype is used
switch ($Contenttype) {
// When $Contenttype == text the type is text/plain and charset=us-ascii
case "text": $Contenttype="text/plain";
$Charset = "charset=\"iso-8859-1\"";
break;
// Same as above, but now with html
case "html": $Contenttype="text/html";
$Charset= "charset=\"iso-8859-1\"";
break;
// On default we asume it's a file
default: $Charset = "name=\"$Fname\"";
$Cdisp = "Content-Disposition: attachment;";
$Filename = "filename=\"$Fname\"";
}
// creation of attachment header
$Msg = "Content-Type: ".$Contenttype.";".LF.TAB.$Charset.LF;
$Msg.= "Content-Transfer-Encoding: ".$Encoding.LF;
// when encoding is base64
if ($Encoding=="base64")
$Msg.= $Descheader.$Cdisp.LF.TAB.$Filename.LF.LF;
else $Msg.= LF;
$Msg.= $Emsg.LF;
// Put $Msg on Mimeparts stack
if ($Description==BODY)
$this->Mimeparts[0] = $Msg;
else $this->Mimeparts[(count($this->Mimeparts)+1)] = $Msg;
// return amount of parts in stack
return count($this->Mimeparts);
}
function Build_Message() {
//Func - Builds the message, private function
//Pre -
//Post -
// Assign empty string to Msg variable
$Msg = "";
// Create unique boundary string
$Boundary = "PM".chr(rand(65, 91))."------".md5(uniqid(rand()));
// Count amount of Mimeparts that exists in Mimeparts array
$Mparts = count($this->Mimeparts);
// Create random value to be sure a unique X-Sent header is created
srand((double)microtime()*1000000);
$Randval = rand();
// XMail parts, need to be added after MIME headers
$XMail ="X-Mailer: AMPM".LF;
$XMail.="X-Send: ".$Randval.LF;
// An attachment list is found (>0), therefore MIME message header has to be
// Multipart/Mixed
if ($Mparts > 0) {
$Ver = "MIME-Version: 1.0".LF;
$Type = "Content-Type: multipart/mixed;".LF.TAB."boundary=\"".$Boundary."\"".LF;
$Warning = LF.WARNING.LF;
// Add text message to Mimepart stack.
if (!empty($this->Body)) {
$this->Attach($this->Body, BODY, "text", "qp");
$Mparts++;
}
// Create mail from MIME-parts
for ($I=0;$I<$Mparts;$I++)
if (!empty($this->Mimeparts[$I]))
$Msg.= LF."--".$Boundary.LF.$this->Mimeparts[$I];
$Msg.= "--".$Boundary."--".LF;
$Msg = $Ver.$Type.$XMail.$Warning.$Msg;
} else if (!empty($this->Body)) {
$Ver = "MIME-Version: 1.0".LF;
$Contenttype="Content-Type: text/plain;".LF.TAB."charset=\"iso-8859-1\"".LF;
$Msg.= $Ver.$Contenttype.$XMail.LF.$this->Body.LF;
}
if (strlen($Msg) == 0) $Msg = LF.LF;
return $Msg;
}
function Gen_Mail($Force=false){
//Func - Generates the MIME-mail messages using the private Build_Message function
//Pre -
//Post -
// Returns Email if already processed.
if (!empty($this->Email) && (!$Force)) return $this->Email;
$Email = "";
if (!empty($this->From)) $Email ="From: ".$this->From.LF;
if (!empty($this->To)) $Email.="To: ".$this->To.LF;
if (!empty($this->Cc)) $Email.="Cc: ".$this->Cc.LF;
if (!empty($this->Bcc)) $Email.="Bcc: ".$this->Bcc.LF;
$Email.="Subject: ".$this->Subject.LF;
$Email.="Date: ".date("D, j M Y G:i:s")." ".GMT.LF;
// Add attachments
$Email.= $this->Build_Message();
$this->Email = $Email;
//
return $this->Email;
}
}
?>
-- keep it clean