PHP email w/ attachments help needed
Posted: Sun Aug 08, 2004 8:37 pm
Okay any help would be greatly appreciated! I have an HTML form, mail.php and class.mail.php. I have gotten the attachment part to work, but pastes itself in the body of the email message. I want to be able to allow multiple attachments to be selected and for them not to show up, posted in the message.
Code: Select all
<?php
include('class.Email.php');
$Sender = $_POST['sender'];
$Recipiant = "some@one.com";
$Cc = $_POST['carbon'];
$Bcc = $_POST['blindcarbon'];
$attachedfile = $_POST['attachments'];
$textVersion = $_POST['textversion'];
$htmlVersion = "<font face='verdana' color='blue'><b>Hi</b></font>";
unset($msg);
$msg = new Email($Recipiant, $Sender, "A Test HTML Email!");
$msg->Cc = $Cc;
$msg->Bcc = $Bcc;
$msg->TextOnly = false;
$msg->Content = $htmlVersion;
$msg->Attach($attachments, "text/plain");
$SendSuccess = $msg->Send();
echo "HTML email w/attachment was ",
($SendSuccess ? "sent" : "not sent"), "<br>";
unset($msg);
?>Code: Select all
<?php
if(isset($GLOBALS["emailmsgclass_php"])) { return; } //** onlyinclude once.
$GLOBALS["emailmsgclass_php"] = 1; //** file was included.
define("EmailNewLine", "\r\n");
define("EmailXMailer", "PHP-EMAIL,v1.1 (William Fowler)");
define("DefaultCharset", "iso-8859-1");
class Email
{
var $To = null;
var $Cc = null;
var $Bcc = null;
var $From = null;
var $Subject = null;
var $Content = null;
var $Attachments;
var $Headers = null;
var $TextOnly = true;
var $Charset = null;
function Email($to=null, $from=null, $subject=null, $headers=null)
{
$this->To = $to;
$this->From = $from;
$this->Subject = $subject;
$this->Headers = $headers;
$this->Attachments = Array();
$this->Attachments["text"] = null;
$this->Attachments["html"] = null;
}
function SetMultipartAlternative($text=null, $html=null)
{
if(strlen(trim(strval($html))) == 0 || strlen(trim(strval($text))) == 0)
return false;
else
{
$this->Attachments["text"] = new EmailAttachment(null, "text/plain");
$this->Attachments["text"]->LiteralContent = strval($text);
$this->Attachments["html"] = new EmailAttachment(null, "text/html");
$this->Attachments["html"]->LiteralContent = strval($html);
return true; //** operation was successful.
}
}
function Attach($pathtofile, $mimetype=null)
{
$attachment = new EmailAttachment($pathtofile, $mimetype);
if(!$attachment->Exists())
return false;
else
{
$this->Attachments[] = $attachment; //** add the attachment to list.
return true; //** attachment successfully added.
}
}
function IsComplete()
{
return (strlen(trim($this->To)) > 0 && strlen(trim($this->From)) > 0);
}
function Send()
{
if(!$this->IsComplete()) //** message is not ready to send.
return false; //** no message will be sent.
$theboundary = "-----" . md5(uniqid("EMAIL"));
$headers = "Date: " . date("r", time()) . EmailNewLine .
"From: $this->From" . EmailNewLine;
if(strlen(trim(strval($this->Cc))) > 0)
$headers .= "CC: $this->Cc" . EmailNewLine;
if(strlen(trim(strval($this->Bcc))) > 0)
$headers .= "BCC: $this->Bcc" . EmailNewLine;
if($this->Headers != null && strlen(trim($this->Headers)) > 0)
$headers .= $this->Headers . EmailNewLine;
$isMultipartAlternative = ($this->Attachments["text"] != null &&
$this->Attachments["html"] != null);
$baseContentType = "multipart/" . ($isMultipartAlternative ?
"alternative" : "mixed");
$headers .= "X-Mailer: " . EmailXMailer . EmailNewLine .
"MIME-Version: 1.0" . EmailNewLine .
"Content-Type: $baseContentType; " .
"boundary="$theboundary"" . EmailNewLine . EmailNewLine;
if($isMultipartAlternative)
{
$thebody = "--$theboundary" . EmailNewLine .
$this->Attachments["text"]->ToHeader() . EmailNewLine .
"--$theboundary" . EmailNewLine .
$this->Attachments["html"]->ToHeader() . EmailNewLine;
}
else
{
$theemailtype = "text/" . ($this->TextOnly ? "plain" : "html");
if($this->Charset == null)
$this->Charset = DefaultCharset;
$thebody = "--$theboundary" . EmailNewLine .
"Content-Type: $theemailtype; charset=$this->Charset" .
EmailNewLine . "Content-Transfer-Encoding: 8bit" .
EmailNewLine . EmailNewLine . $this->Content .
EmailNewLine . EmailNewLine;
foreach($this->Attachments as $attachment)
{
if($attachment != null)
{
$thebody .= "--$theboundary" . EmailNewLine .
$attachment->ToHeader() . EmailNewLine;
}
}
}
$thebody .= "--$theboundary--";
return mail($this->To, $this->Subject, $thebody, $headers);
}
}
class EmailAttachment
{
var $FilePath = null;
var $ContentType = null;
var $LiteralContent = null;
function EmailAttachment($pathtofile=null, $mimetype=null)
{
if($mimetype == null || strlen(trim($mimetype)) == 0)
$this->ContentType = "application/octet-stream";
else
$this->ContentType = $mimetype;
$this->FilePath = $pathtofile; //** save the path to the file attachment.
}
function HasLiteralContent()
{
return (strlen(strval($this->LiteralContent)) > 0);
}
function GetContent()
{
if($this->HasLiteralContent())
return $this->LiteralContent;
else
{
if(!$this->Exists()) //** file does not exist.
return null; //** no content is available.
else
{
$thefile = fopen($this->FilePath, "rb");
$data = fread($thefile, filesize($this->FilePath));
fclose($thefile);
return $data;
}
}
}
function Exists()
{
if($this->FilePath == null || strlen(trim($this->FilePath)) == 0)
return false;
else
return file_exists($this->FilePath);
}
function ToHeader()
{
$attachmentData = $this->GetContent(); //** get content for the header.
if($attachmentData == null) //** no valid attachment content.
return null; //** no header can be generted.
$header = "Content-Type: $this->ContentType;";
if(!$this->HasLiteralContent())
{
$header .= " name="" . basename($this->FilePath) . """ . EmailNewLine .
"Content-Disposition: attachment; filename="" .
basename($this->FilePath) . """;
}
$header .= EmailNewLine;
$header .= "Content-Transfer-Encoding: base64" . EmailNewLine .
EmailNewLine;
$header .= chunk_split(base64_encode($attachmentData), 76, EmailNewLine) .
EmailNewLine;
return $header; //** return the headers generated by file.
}
}
?>Code: Select all
<html>
<head>
<title>Mail Form</title>
<body BACKGROUND=/im/blood.jpg>
<form method="post" action="mail.php">
<font color="black" face="verdana" size="4"><b>From: </b></font><input type="Text" name="sender"><br><br>
<font color="black" face="verdana" size="4"><b>Cc: </b></font><input type="Text" name="carbon"><br><br>
<font color="black" face="verdana" size="4"><b>Bcc: </b></font><input type="Text" name="blindcarbon"><br><br>
<font color="black" face="verdana" size="4"><b>Message: </b></font><TEXTAREA name="textversion" ROWS=20 COLS=60></TEXTAREA><br><br>
<ACTION="mailto:webzz@nc.rr.com" ENCTYPE="multipart/form-data">
<FONT size="2" color="white" face="Verdana">Attachment: </FONT><INPUT TYPE="file" NAME="attachments" MAXLENGTH=50 ALLOW="text/*" ><br><br>
<input type="Submit" name="submit" value="Send Mail">
<input type="reset" value="Reset"></form>
</HTML>