PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
terji
Forum Commoner
Posts: 37 Joined: Tue May 14, 2002 5:27 pm
Location: Denmark
Post
by terji » Tue Sep 02, 2003 4:59 am
One challenge usually just leads to the next!
How do I attach multible files to an e-mail?
I can already send multible files - but their contents get altered with jibberish at the end of the file. I assume I'm not separating them correctly. Does anyone have an example script for me? Or can you comment my code?
Code: Select all
/***********************************************
* Loop through all the files to attach. *
***********************************************/
foreach($this->arrFileContent as $key => $value)
{
$strMessage .= "--" . $this->strMimeBoundary . "\r\n";
$strMessage .= "Content-type: text/" . $this->strType . "; charset="iso-8859-1"" . "\r\n";
$strMessage .= "\r\n" . $strContent . "\r\n\r\n";
$strMessage .= "--" . $this->strMimeBoundary . "\r\n";
$strMessage .= "Content-Type: application/octet-stream; name="" . $key . """ . "\r\n";
$strMessage .= "Content-Transfer-Encoding: base64" . "\r\n";
$strMessage .= "Content-Description: " . $key . "\r\n";
$strMessage .= "\r\n" . $value . "\r\n\r\n";
$strMessage .= "--" . $this->strMimeBoundary . "--";
}
terji
Forum Commoner
Posts: 37 Joined: Tue May 14, 2002 5:27 pm
Location: Denmark
Post
by terji » Wed Sep 03, 2003 7:12 am
Here is the class that I'm making. It fails every now and then when it has multible attached files. Can anyone see why?
Code: Select all
<?php
/*********************************************************************************
* ----------------------------------------------------------------------------- *
* Example: *
* Just include the MailBuilder.php class and use these class methods: *
* *
* $objMail = new MailBuilder(); *
* $objMail->to("you@server.com"); *
* $objMail->to("your@mama.net"); *
* $objMail->cc("your@uncle.net"); *
* $objMail->bcc("your@boss.org"); *
* $objMail->subject("MailBuilder"); *
* $objMail->htmlMessage("<html><head> ... "); *
* ... or $objMail->textMessage("Keep it simple!"); *
* $objMail->attach("..path\\to\\file.txt"); *
* $objMail->attach("..another\\file.txt"); *
* $objMail->receipt(true); *
* $objMail->send(); *
* *
*********************************************************************************/
class MailBuilder
{
/*****************************************************************************
* Miscellaneous variables. *
*****************************************************************************/
var $arrTo = array();
var $arrCc = array();
var $arrBcc = array();
var $strFrom = "";
var $strReplyTo = "";
var $strSubject = "";
var $arrFileContent = array();
var $strType = "";
var $strMimeBoundary = "";
var $strMailRoot = "";
var $boolReceipt = false;
/*****************************************************************************
* Constructor. *
* Initiates the object with a sender and a reply to address. The path to *
* the operating platform's mailroot is also set, so we have an alternate *
* way to send the message. *
* Also creates a unique MIME Boundary for content separation and the *
* opportunity to recieve a receipt when the reciever has read the e-mail. *
*****************************************************************************/
function MailBuilder()
{
$this->strFrom = "admin@" . $_SERVERї"SERVER_NAME"] . ".com";
$this->strReplyTo = "admins@com.com";
$this->strMailRoot = "C:\\Inetpub\\mailroot\\Pickup"e;;
$this->strMimeBoundary = md5(uniqid(time()));
}
/*****************************************************************************
* Add a recipient to the recipients array. *
*****************************************************************************/
function to($strTo)
{
$this->arrToї] = $strTo;
}
/*****************************************************************************
* Add a recipient to the Cc recipients array. *
*****************************************************************************/
function cc($strCc)
{
$this->arrCcї] = $strCc;
}
/*****************************************************************************
* Add a recipient to the Bcc recipients array. *
*****************************************************************************/
function bcc($strBcc)
{
$this->arrBccї] = $strBcc;
}
/*****************************************************************************
* Add a subject to the e-mail. *
*****************************************************************************/
function subject($strSubject)
{
$this->strSubject = $strSubject;
}
/*****************************************************************************
* Use textMessage(...) if you want to send a message in plain text. *
*****************************************************************************/
function textMessage($strTextMessage)
{
$this->strTextMessage = $strTextMessage;
$this->strHtmlMessage = "";
$this->strType = "plain";
}
/*****************************************************************************
* Use htmlMessage(...) if you want to send a message in html. *
*****************************************************************************/
function htmlMessage($strHtmlMessage)
{
$this->strHtmlMessage = $strHtmlMessage;
$this->strTextMessage = "";
$this->strType = "html";
}
/*****************************************************************************
* Attach upp to multible files. *
*****************************************************************************/
function attach($strFileLocation)
{
$strFileName = basename($strFileLocation);
$resFilePointer = fopen($strFileLocation, "rb");
$strFileContent = fread($resFilePointer, filesize($strFileLocation));
fclose($resFilePointer);
$this->arrFileContentї$strFileName] = chunk_split(base64_encode($strFileContent));
}
/*********************************************************************************
* Request a read receipt from the reciever. *
*********************************************************************************/
function receipt($boolRequest)
{
$this->boolReceipt = $boolRequest;
}
/*****************************************************************************
* The send() function collects the necessary data needed to send the mail *
* and sends it with PHP's mail(...) function. *
* If this fails it tries to dump the mail as a file into the mailroot and *
* gives the server mailing capabilities the responsibility for sending the *
* e-mail. *
*****************************************************************************/
function send($strMailMode = "")
{
$strRecipients = implode(",", $this->arrTo);
$strSubject = $this->strSubject;
$strMessage = $this->getMessage();
$strHeaders = $this->getHeaders();
if($strMailMode == "MAILROOT")
{
$file = tempnam($this->strMailRoot, "np");
$resMailFile = fopen($file, "w");
fwrite($resMailFile, $strHeaders . $strMessage);
fclose($resMailFile);
}
else
{
if(!mail($strRecipients, $strSubject, $strMessage, $strHeaders))
{
$file = tempnam($this->strMailRoot, "np");
$resMailFile = fopen($file, "w");
fwrite($resMailFile, $strHeaders . $strMessage);
fclose($resMailFile);
}
}
}
/*****************************************************************************
* Returns the body of the message in a formatted string. Adds the MIME *
* Boundary if there are attached files. *
*****************************************************************************/
function getMessage()
{
$strContent = "";
$strMessage = "";
if($this->strTextMessage != "")
{
$strContent = $this->strTextMessage;
}
elseif($this->strHtmlMessage != "")
{
$strContent = $this->strHtmlMessage;
}
if(count($this->arrFileContent) > 0)
{
$intCounter = 1;
/*********************************************************************
* The message. *
*********************************************************************/
$strMessage .= "--" . $this->strMimeBoundary . "\r\n";
$strMessage .= "Content-type: text/" . $this->strType . "; charset="iso-8859-1"" . "\r\n";
$strMessage .= "\r\n" . $strContent . "\r\n\r\n";
/*********************************************************************
* Loop through all the files to attach. *
*********************************************************************/
foreach($this->arrFileContent as $key => $value)
{
$strMessage .= "--" . $this->strMimeBoundary . "\r\n";
$strMessage .= "Content-Transfer-Encoding: base64" . "\r\n";
$strMessage .= "Content-Description: " . $key . "\r\n";
$strMessage .= "\r\n" . $value . "\r\n\r\n";
if(count($this->arrFileContent) == $intCounter)
{
$strMessage .= "--" . $this->strMimeBoundary . "--";
}
else
{
$strMessage .= "--" . $this->strMimeBoundary . "\r\n";
}
$intCounter++;
}
}
else
{
$strMessage = $strContent;
}
return $strMessage;
}
/*****************************************************************************
* Returns a formatted string with the headers. Also adds the To, Cc, Bcc, *
* From, and Reply-To data. *
*****************************************************************************/
function getHeaders()
{
$strHeaders = "MIME-version: 1.0\r\n";
if(count($this->arrFileContent) > 0)
{
$strHeaders .= "Content-Type: multipart/mixed; boundary="" . $this->strMimeBoundary . """ . "\r\n";
}
else
{
$strHeaders .= "Content-type: text/" . $this->strType . "; charset="iso-8859-1"\r\n";
}
$strHeaders .= "Subject: " . $this->strSubject . "\r\n";
/*************************************************************************
* Request a read receipt after reciever has read the e-mail. *
*************************************************************************/
if($this->boolReceipt)
{
$strHeaders .= "Disposition-Notification-To: " . $this->strReplyTo . "\r\n";
}
/*************************************************************************
* Get all the recipients in a comma separated string. *
*************************************************************************/
if(count($this->arrTo) > 0)
{
$strTo = implode(",", $this->arrTo);
$strHeaders .= "To: " . $strTo . "\r\n";
}
/*************************************************************************
* Get all the Cc recipients in a comma separated string. *
*************************************************************************/
if(count($this->arrCc) > 0)
{
$strCc = implode(",", $this->arrCc);
$strHeaders .= "Cc: " . $strCc . "\r\n";
}
/*************************************************************************
* Get all the Bcc recipients in a comma separated string. *
*************************************************************************/
if(count($this->arrBcc) > 0)
{
$strBcc = implode(",", $this->arrBcc);
$strHeaders .= "Bcc: " . $strBcc . "\r\n";
}
/*************************************************************************
* Set the sender and the Reply-To address. *
*************************************************************************/
$strHeaders .= "From: " . $this->strFrom . "\r\n";
$strHeaders .= "Reply-To: " . $this->strReplyTo . "\r\n";
return $strHeaders;
}
}
?>