Can someone please look at my code and tell me
what I've done wrong.
I have an html form which allows a user to upload
2 files of any type and enter in a message. This
message becomes the body part of the email.
Right now I'm trying to get 1 file to work, but
no such luck and another question I have is, what
happens if the file is a gif, tif, txt, or excel
spreadseet? What do I put instead of "image/jpeg"
and do I have to check for each type of file?
Any help would be appreciated.
Thanks.
<?php
/*******************************************************/
/ S T A R T O F C L A S S /
/*******************************************************/
class mime_mail
{
var $parts;
var $to;
var $from;
var $headers;
var $subject;
var $body;
/*************************************/
/* CLASS CONSTRUCTOR */
/*************************************/
function mime_mail()
{
$this->parts = array();
$this->to = "";
$this->from = "";
$this->headers = "";
$this->subject = "";
$this->body = "";
}
/*************************************/
/* ADD ATTACHMENT TO MAIL OBJECT */
/*************************************/
function add_attachment($message, $name="", $ctype="application/octet-stream")
{
$this->parts[] = array (
"ctype" => $ctype,
"message" => $message,
"encode" => $encode,
"name" => $name
);
}
/*************************************/
/* BUILD MSG PARTS OF 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";
}
/*************************************/
/* 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";
}
/*************************************/
/* SEND THE MAIL */
/*************************************/
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);
}
}
/*******************************************************/
/ E N D O F C L A S S /
/*******************************************************/
/****************************************/
/* READ IN FILE(S) TO ATTACH TO EMAIL */
/****************************************/
if ($File1 != "none")
{
$fp = fopen ($File1, "r");
$file_size = filesize ($File1);
$Attach1 = fread ($fp, $file_size);
fclose ($fp);
}
if ($File2 != "none")
{
$fp = fopen ($File2, "r");
$file_size = filesize ($File2);
$Attach2 = fread ($fp, $file_size);
fclose ($fp);
}
/****************************************/
/* SEND THE MAIL */
/****************************************/
$mail = new mime_mail();
$mail->from = "kathyc@bserv.com";
$mail->headers = "";
$mail->to = "kathyc@bserv.com";
$mail->subject = "Testing...";
$mail->body = Trim($TheBody);
$mail->add_attachment($Attach1, $File1, "image/jpeg");
$mail->send();
/*******************************************************/
/ E N D O F P R O G R A M /
/*******************************************************/
?>