I have a mailbot script that reads in an email from a .forward file and process the message according to some business rules. I am trying to process attachments with this script, but when I save the file after the read, the file gets corrupted.
When I open the file, it appears that all the information was transferred correctly. It's missing something that I just can't find.
Perhaps someone has done this already and can suggest what I am doing wrong.
Thanks,
Andrew Pasetti
----mailbot script---
#!/usr/local/php/bin/php
<?
// Grab the entire message first.
$stdin=fopen("php://stdin","r");
while (!feof($stdin))
$message.=fread($stdin,1024);
fclose($stdin);
// Make sure EOLs are \r\n
$message=str_replace("\n","\r\n",$message);
// grab the boundary marker
if (ereg('boundary=\"=_[a-zA-Z0-9].[a-zA-Z0-9]\"',$message,$regs))
{
list(,$boundary) = explode("=",$regs[0],2);
$boundary = str_replace('"',"",$boundary);
// grab the attachment headers based on the boundary
$headers = explode("--".$boundary,$message);
// loop through each header, except for the first one
// which is just normal email headers.
for ($i=1;$i<count($headers);$i++)
{
$headerList=explode("\r\n",str_replace("\r\n\t"," ",$headers[$i]));
list(,$contentDisposition) = explode(":",$headerList[3]);
// if the header is an attachment, attempt to store it as a file.
if (ereg("attachment",$contentDisposition))
{
list(,$fileName) = explode("=",$contentDisposition);
$fileName = str_replace("\"","",$fileName);
$attachment = "";
for ($j=4;$j<count($headerList);$j++)
{
$attachment .= $headerList[$j];
}
$fp = fopen("../../transactions/andy/$fileName","w+");
fwrite($fp,$attachment);
fclose($fp);
}
}
}
?>
----end mailbot script---
---sample email with attachments---
From: <somebody>
To: <somebody>
Date: 7/28/03 10:27AM
Subject: something
unimportant headers ommitted
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="=_E7B9F0A7.ADCCA05E"
This is a MIME message. If you are reading this text, you may want to
consider changing to a mail reader or gateway that understands how to
properly handle MIME multipart messages.
--=_E7B9F0A7.ADCCA05E
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
blam
--=_E7B9F0A7.ADCCA05E
Content-Type: image/gif; name="desc.gif"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="desc.gif"
R0lGODlhDgAMAPcAAISEhMbGxv//////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////yH5BAEAAAEALAAAAAAOAAwA
AAgvAAMIHEiwoMGDCBMCWMhwoUGHAwUIOAiA4ESKFQNIVCjwIsKFGxMGAOBRpEmRAQEAOw==
--=_E7B9F0A7.ADCCA05E
Content-Type: image/gif; name="asc.gif"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="asc.gif"
R0lGODlhDgAMAPcAAISEhMbGxv//////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////yH5BAEAAAEALAAAAAAOAAwA
AAgvAAMIHEiwoMGDBgEIQDgQgMKFDAEIhHjQ4USKBSUOxNhQ48WEDkNaZEiypEmBAQEAOw==
--=_E7B9F0A7.ADCCA05E--
--end sample email--