How to attach mulitble files?

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

Post Reply
terji
Forum Commoner
Posts: 37
Joined: Tue May 14, 2002 5:27 pm
Location: Denmark

How to attach mulitble files?

Post by terji »

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

Here's the whole thing

Post by terji »

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
&#123;
	/*****************************************************************************
	 * 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()
	&#123;
		$this->strFrom = "admin@" . $_SERVER&#1111;"SERVER_NAME"] . ".com";
		$this->strReplyTo = "admins@com.com";
		$this->strMailRoot = "C:\\Inetpub\\mailroot\\Pickup&quote;;
		$this->strMimeBoundary = md5(uniqid(time()));
	&#125;
	
	/*****************************************************************************
	 * Add a recipient to the recipients array.                                  *
	 *****************************************************************************/
	function to($strTo)
	&#123;
		$this->arrTo&#1111;] = $strTo;
	&#125;

	/*****************************************************************************
	 * Add a recipient to the Cc recipients array.                               *
	 *****************************************************************************/
	function cc($strCc)
	&#123;
		$this->arrCc&#1111;] = $strCc;
	&#125;

	/*****************************************************************************
	 * Add a recipient to the Bcc recipients array.                              *
	 *****************************************************************************/
	function bcc($strBcc)
	&#123;
		$this->arrBcc&#1111;] = $strBcc;
	&#125;

	/*****************************************************************************
	 * Add a subject to the e-mail.                                              *
	 *****************************************************************************/
	function subject($strSubject)
	&#123;
		$this->strSubject = $strSubject;
	&#125;

	/*****************************************************************************
	 * Use textMessage(...) if you want to send a message in plain text.         *
	 *****************************************************************************/
	function textMessage($strTextMessage)
	&#123;
		$this->strTextMessage = $strTextMessage;
		$this->strHtmlMessage = "";
		$this->strType = "plain";
	&#125;

	/*****************************************************************************
	 * Use htmlMessage(...) if you want to send a message in html.               *
	 *****************************************************************************/
	function htmlMessage($strHtmlMessage)
	&#123;
		$this->strHtmlMessage = $strHtmlMessage;
		$this->strTextMessage = "";
		$this->strType = "html";
	&#125;

	/*****************************************************************************
	 * Attach upp to multible files.                                             *
	 *****************************************************************************/
	function attach($strFileLocation)
	&#123;
		$strFileName = basename($strFileLocation);
		$resFilePointer = fopen($strFileLocation, "rb");
		$strFileContent = fread($resFilePointer, filesize($strFileLocation));
		fclose($resFilePointer);
		$this->arrFileContent&#1111;$strFileName] = chunk_split(base64_encode($strFileContent));
	&#125;

	/*********************************************************************************
	 * Request a read receipt from the reciever.                                     *
	 *********************************************************************************/
	function receipt($boolRequest)
	&#123;
		$this->boolReceipt = $boolRequest;
	&#125;

	/*****************************************************************************
	 * 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 = "")
	&#123;
		$strRecipients = implode(",", $this->arrTo);
		$strSubject = $this->strSubject;
		$strMessage = $this->getMessage();
		$strHeaders = $this->getHeaders();

		if($strMailMode == "MAILROOT")
		&#123;
			$file = tempnam($this->strMailRoot, "np");
			$resMailFile = fopen($file, "w");
			fwrite($resMailFile, $strHeaders . $strMessage);
			fclose($resMailFile);
		&#125;
		else
		&#123;
			if(!mail($strRecipients, $strSubject, $strMessage, $strHeaders))
			&#123;
				$file = tempnam($this->strMailRoot, "np");
				$resMailFile = fopen($file, "w");
				fwrite($resMailFile, $strHeaders . $strMessage);
				fclose($resMailFile);
			&#125;	
		&#125;
	&#125;

	/*****************************************************************************
	 * Returns the body of the message in a formatted string. Adds the MIME      *
	 * Boundary if there are attached files.                                     *
	 *****************************************************************************/
	function getMessage()
	&#123;
		$strContent = "";
		$strMessage = "";

		if($this->strTextMessage != "")
		&#123;
			$strContent = $this->strTextMessage;
		&#125;
		elseif($this->strHtmlMessage != "")
		&#123;
			$strContent = $this->strHtmlMessage;
		&#125;

		if(count($this->arrFileContent) > 0)
		&#123;
			$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)
			&#123;	
				$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)
				&#123;
					$strMessage .= "--" . $this->strMimeBoundary . "--";
				&#125;
				else
				&#123;
					$strMessage .= "--" . $this->strMimeBoundary . "\r\n";
				&#125;

				$intCounter++;
			&#125;
		&#125;
		else
		&#123;
			$strMessage = $strContent;
		&#125;

		return $strMessage;
	&#125;

	/*****************************************************************************
	 * Returns a formatted string with the headers. Also adds the To, Cc, Bcc,   *
	 * From, and Reply-To data.                                                  *
	 *****************************************************************************/
	function getHeaders()
	&#123;
		$strHeaders = "MIME-version: 1.0\r\n";

		if(count($this->arrFileContent) > 0)
		&#123;
			$strHeaders .= "Content-Type: multipart/mixed; boundary="" . $this->strMimeBoundary . """ . "\r\n";
		&#125;
		else
		&#123;
			$strHeaders .= "Content-type: text/" . $this->strType . "; charset="iso-8859-1"\r\n";
		&#125;

		$strHeaders .= "Subject: " . $this->strSubject . "\r\n";

		/*************************************************************************
		 * Request a read receipt after reciever has read the e-mail.            *
		 *************************************************************************/
		if($this->boolReceipt)
		&#123;
			$strHeaders .= "Disposition-Notification-To: " . $this->strReplyTo . "\r\n";
		&#125;

		/*************************************************************************
		 * Get all the recipients in a comma separated string.                   *
		 *************************************************************************/
		if(count($this->arrTo) > 0)
		&#123;
			$strTo = implode(",", $this->arrTo);
			$strHeaders .= "To: " . $strTo . "\r\n";
		&#125;

		/*************************************************************************
		 * Get all the Cc recipients in a comma separated string.                *
		 *************************************************************************/
		if(count($this->arrCc) > 0)
		&#123;
			$strCc = implode(",", $this->arrCc);
			$strHeaders .= "Cc: " . $strCc . "\r\n";
		&#125;

		/*************************************************************************
		 * Get all the Bcc recipients in a comma separated string.               *
		 *************************************************************************/
		if(count($this->arrBcc) > 0)
		&#123;
			$strBcc = implode(",", $this->arrBcc);
			$strHeaders .= "Bcc: " . $strBcc . "\r\n";
		&#125;

		/*************************************************************************
		 * Set the sender and the Reply-To address.                              *
		 *************************************************************************/
		$strHeaders .= "From: " . $this->strFrom . "\r\n";
		$strHeaders .= "Reply-To: " . $this->strReplyTo . "\r\n";

		return $strHeaders;
	&#125;
&#125;

?>
Post Reply