Jump to content

mail(...) function -> sending a binary file


gregaryb
 Share

Recommended Posts

I have done this with text file before but I can't gt it to work with a binary file.

 

I am trying to send a .xls file and this is what I am getting when I open up the file attachment in MS Excel:

 

UEsDBBQAAAAIACtT8ERHkkSyWAEAAPAEAAATAAAAW0NvbnRlbnRfVHlwZXNdLnhtbK2UTU7DMBCF 95wi8hYlblkghJp2QWEJlSgHMPakserYlmf6d3smaQsIiUDVbmJF9nvf+Hns0WTbuGwNCW3wpRgW A5GB18FYvyjF2/wpvxMZkvJGueChFDtAMRlfjea7CJix2GMpaqJ4LyXqGhqFRYjgeaYKqVHEv2kh o9JLtQB5MxjcSh08gaecWg8xHr0wP1kD2UwlelYNY+TWSWI32H+HBfuJ7GEvbNmlUDE6qxVx4XLt zQ9qHqrKajBBrxqWFJ3NdesifwUi7Rzg2SiMCZTBGoAaV+xNj+QpVGrlKHvcsvs+8wQOT+MdwixY 2a3B2sY+Qv+GftetzwyC9dOkNtxKPaFvQlq+h7C8dOztWDTK+r5DZ/EshYiSUWcXAG1yBkwe2RIS 2a9j72XrkOB0+LEJWvU/iYe0u2hQdsPwwrF/+v911WqVwLxS4ua4+I377n2sQ3YP1vgDUEsDBBQA AAAIACtT8EQXtjc46QAAAEsCAAALAAAAX3JlbHMvLnJlbHOtks1qwzAMgO97CqN7o7SFMUadXsqg tzKyB9Bs5YcklrG9LX37eYexBbrSw46WpU+fhHb7eRrVO4fYi9OwLkpQ7IzY3rUaXuqn1QOomMhZ GsWxhjNH2Fd3u2ceKeWa2PU+qgxxUUOXkn9EjKbjiWIhnl3+aSRMlPIztOjJDNQybsryHsNvBlQL pjpaDeFot6Dqs+db2NI0veGDmLeJXbrQAnlO7CzblQ+5PqQ+D6NqCi0nDVbMKYcjkvdFRgNeNtrc bvT3tDhxIkuJ0Ejg6z5fGdeE1v+5omXGj8084oeE4VVk+HbBxQ1Un1BLAwQUAAAACAArU/BEhCSx VukAAAC5AgAAGgAAAHhsL19yZWxzL3dvcmtib29rLnhtbC5yZWxzrZLBasMwEETv/Qqx91p2Wkop

kXMpgVxb9wOEtLZMbEloN23991UbSBwIoQefxKzYmcdI6833OIhPTNQHr6AqShDoTbC97xR8NNv7

 

Here is my code:

							$strEmailBody = "<html><body><table border="0" width="800px" cellspacing="0" cellpadding="0">".											"	<tr>".											"		<td width="25%" rowspan="5" valign="middle" style="border-style:solid;border-width:thin;border-color:#000000;">".											"		<p align="right"><b>BILLING DETAILS:</b></td>".											"		<td colspan="3" style="border-style:solid solid none none;border-width:thin;border-color:#000000;padding-left:5px;" align="left">".$_SESSION["GivenNames"]." ".$_SESSION["Surname"]."</td>".											"	</tr>".											"	<tr>".											"		<td colspan="3" style="border-style:none solid none none;border-width:thin;border-color:#000000;padding-left:5px;" align="left">".$_SESSION["Address"]."</td>".											"	</tr>".											"	<tr>".											"		<td width="9%" align="right">".											"		<b>Phone:</b></td>".											"		<td colspan="2" style="border-style:none solid none none;border-width:thin;border-color:#000000;" align="left">".$_SESSION["Phone"]."</td>".											"	</tr>".											"	<tr>".											"		<td width="9%" align="right">".											"		<b>Mobile:</b></td>".											"		<td colspan="2" style="border-style:none solid none none;border-width:thin;border-color:#000000;" align="left">".$_SESSION["Mobile"]."</td>".											"	</tr>".											"	<tr>".											"		<td width="9%" align="right" style="border-style:none none solid none;border-width:thin;border-color:#000000;">".											"		<b>Email:</b></td>".											"		<td colspan="2" style="border-style:none solid solid none;border-width:thin;border-color:#000000;" align="left">".$_SESSION["Email"]."</td>".											"	</tr>".											"</table>".											"<br>".											"<table border="0" width="800px" cellspacing="0" cellpadding="0">".											"	<tr>".											"		<td width="25%" rowspan="5" align="right" valign="middle" style="border-style:solid;border-width:thin;border-color:#000000;">".											"		<b>DELIVERY DETAILS:</b></td>".											"		<td colspan="3" style="border-style:solid solid none none;border-width:thin;border-color:#000000;padding-left:5px;" align="left">".$_SESSION["RecGivenNames"]." ".$_SESSION["RecSurname"]."</td>".											"	</tr>".											"	<tr>".											"		<td colspan="3" style="border-style:none solid none none;border-width:thin;border-color:#000000;padding-left:5px;" align="left">".$_SESSION["RecAddress"]."</td>".											"	</tr>".											"	<tr>".											"		<td width="9%" align="right">".											"		<b>Phone:</b></td>".											"		<td colspan="2" style="border-style:none solid none none;border-width:thin;border-color:#000000;" align="left">".$_SESSION["RecPhone"]."</td>".											"	</tr>".											"	<tr>".											"		<td width="9%" height="24" align="right">".											"		<b>Mobile:</b></td>".											"		<td colspan="2" style="border-style:none solid none none;border-width:thin;border-color:#000000;" height="24" align="left">".$_SESSION["RecMobile"]."</td>".											"	</tr>".											"	<tr>".											"		<td width="9%" align="right" style="border-style:none none solid none;border-width:thin;border-color:#000000;">".											"		<b>Email:</b></td>".											"		<td colspan="2" style="border-style:none solid solid none;border-width:thin;border-color:#000000;" align="left">".$_SESSION["RecEmail"]."</td>".											"	</tr>".											"</table>".											"<br>".											"<table border="0" width="800px" cellspacing="0" cellpadding="0" bordercolor="#000000">".											"	<tr>".											"		<td width="49%" align="left" style="border-style:solid none solid solid;border-width:thin;border-color:#000000;"><b>ITEM DESCRIPTION</b></td>".											"		<td width="17%" align="right" style="border-style:solid none solid solid;border-width:thin;border-color:#000000;">".											"		<b>UNIT PRICE</b></td>".											"		<td width="17%" align="right" style="border-style:solid none solid solid;border-width:thin;border-color:#000000;">".											"		<b>QUANTITY</b></td>".											"		<td width="17%" align="right" style="border-style:solid solid solid solid;border-width:thin;border-color:#000000;">".											"		<b>SUB_TOTAL</b></td>".											"	</tr>".							$fTotal = 0;													for ($nI = 0; $nI < count($_SESSION["arrayShoppingCart"]); $nI++)							{								$arrayCartItem = $_SESSION["arrayShoppingCart"][$nI];															$nCategoryIndex = $arrayCartItem["CategoryIndex"];								$strCategory = $_SESSION["arrayCategories"][$nCategoryIndex][0];								$nInventoryIndex = $arrayCartItem["ItemIndex"];								$arrayInventoryItem = $_SESSION["arrayInventory"][$strCategory][$nInventoryIndex];								$strEmailBody .= "	<tr>".												 "		<td width="44%" style="border-style:none none none solid;border-width:thin;border-color:#000000;">".$arrayInventoryItem["Description"]."</td>".												 "		<td width="16%" align="right" style="border-style:none none none solid;border-width:thin;border-color:#000000;">".sprintf("$%.2f", $arrayInventoryItem["Price"])."</td>".												 "		<td width="16%" align="right" style="border-style:none none none solid;border-width:thin;border-color:#000000;">".$arrayCartItem["Quantity"]."</td>".												 "		<td width="16%" align="right" style="border-style:none solid none solid;border-width:thin;border-color:#000000;">".sprintf("$%.2f", $arrayInventoryItem["Price"] * $arrayCartItem["Quantity"])."</td>".												 "	</tr>";												$fTotal += $arrayInventoryItem["Price"] * $arrayCartItem["Quantity"];						   	}										   	$strEmailBody .= "	<tr>".											 "		<td width="76%" align="right" colspan="3" style="border-style:solid solid none solid;border-width:thin;border-color:#000000;">".											 "		<b>SUB-TOTAL</b></td>".											 "		<td width="16%" align="right" style="border-style:solid solid none solid;border-width:thin;border-color:#000000;">".sprintf("$%.2f", $fTotal)."</td>".											 "	</tr>".											 "	<tr>".											 "		<td width="76%" align="right" colspan="3"  style="border-style:solid solid none solid;border-width:thin;border-color:#000000;">".											 "		<b>POSTAGE & HANDLING</b></td>".											 "		<td width="16%" align="right"  style="border-style:solid solid none solid;border-width:thin;border-color:#000000;">".sprintf("$%.2f", $_SESSION["PostageHandling"])."</td>".											 "	</tr>".											 "	<tr>".											 "		<td width="76%" colspan="3" align="right"  style="border-style:solid solid solid solid;border-width:thin;border-color:#000000;"><b>TOTAL</b></td>".											 "		<td width="16%" align="right"  style="border-style:solid solid solid none;border-width:thin;border-color:#000000;">".sprintf("$%.2f", $fTotal + $_SESSION["PostageHandling"])."</td>".											 "	</tr>".											 "</table></body></html>";							mail($_SESSION["BusinessEmail"], "Order from ".$_SESSION["BusinessName"], $strEmailBody, getHeader($strEmailBody));						}

Function getHeader(...)

						function getHeader($strEmailBody)						{							// Get a random 32 bit number using time() as seed.							$nRandNum = md5(uniqid(time()));														$strEmailHeader = "From: ".$_SESSION["GivenNames"]." ".$_SESSION["Surname"]." <".$_SESSION["Email"].">rn";							$strEmailHeader .= "Reply-To: ".$_SESSION["Email"]."rn";							$strEmailHeader .= "MIME-Version: 1.0rn";							$strEmailHeader .= "Content-Type: multipart/mixed; boundary="".$nRandNum.""rnrn";							$strEmailHeader .= "This is a multi-part message in MIME format.rn";							$strEmailHeader .= "--".$nRandNum."rn";							$strEmailHeader .= "Content-type:text/html; charset=UTF-8rn";							$strEmailHeader .= $strEmailBody."rnrn";							$strEmailHeader .= "--".$nRandNum."rn";							$strEmailHeader .= "Content-Type: application/octet-stream; name="Invoice.xlsx"rn"; // use different content types here							$strEmailHeader .= "Content-Transfer-Encoding: 7bitrn";							$strEmailHeader .= "Content-Disposition: attachment; filename="Invoice.xls"rnrn";							$strEmailHeader .= getInvFileData($_SESSION["Surname"])."rnrn";							$strEmailHeader .= "--".$nRandNum."--";														return $strEmailHeader;						}

Function getInvFileData(...)

						function getInvFileData($strSurname)						{							$strInvFileName = "Invoice_".$strSurname.".xls";							echo "/*";							if (copy("BlankInvoice.xls", $strInvFileName))							{								echo "*/r";																try 								{									$objPHPExcel = PHPExcel_IOFactory::load($strInvFileName);									$objPHPExcel->setActiveSheetIndexByName("Invoice");												} 								catch(Exception $e) 								{									die('Error loading file "'.pathinfo($strInvFileName, PATHINFO_BASENAME).'": '.$e->getMessage());								}								addInvDetails($objPHPExcel->getActiveSheet());							    $objPHPExcelWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel2007");							    $objPHPExcelWriter->save($strInvFileName);							    								$_SESSION["BUFFER"] = file_get_contents($strInvFileName);								$_SESSION["BUFFER_SIZE"] = filesize($strInvFileName);							    							    //unlink($strInvFileName);							}							else							{								echo "*/r";								echo "document.write("<p><font size='10'>ERROR: 'BlankInvoice.xls' could not be found...</font></p><br>");r";							}							return chunk_split(base64_encode($_SESSION["BUFFER"]));						}

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...