Reputation: 468
I am using pipe to program to send emails to a script. Using this script, I can sucessfully save the entire email as a .txt file on my server. The only thing left to do, is figure out how to save any attachment that the email comes in with. (This email address is only given to one trusted source, so security is not an issue)
Working code that saves entire email as .txt file:
$fd = fopen("php://stdin", "r");
$email = ""; // This will be the variable holding the data.
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);
/* Saves the data into a file */
$fdw = fopen("/home/lmshost22/public_html/pipemail.txt", "w+");
fwrite($fdw, $email);
fclose($fdw);
Can anyone help me with code that simply extracts the attachment (will ALWAYS be a .csv file) and saves it on my server?
Here is the code that the .txt file shows for the attachment:
------=_NextPart_000_0133_01CE0E98.061E7400
Content-Type: application/vnd.ms-excel;
name="leads.csv"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
filename="leads.csv"
ApplicationDetailId,DateCreated,VehicleInfoId,FirstName,MiddleName,LastNa=
me,Suffix,Street,City,State,ZipCode,Email,HomePhoneArea,HomePhonePrefix,H=
omePhoneSuffix,CellArea,CellPrefix,CellSuffix,WorkPhoneArea,WorkPhonePreF=
ix,WorkPhoneSuffix,AmountBorrow,IsVehiclePaidOff,IsVehicleSalvaged,OweAmo=
unt,TitleOwnership,IsInBankruptcy,IsInCreditCounseling,HearOfUs,VehicleIn=
foId,Year,Make,Model,Trim,Miles,Engine,DriveTrain,Transmission,Options,Bo=
ok,ClassCode,Door,FuelType,BodyStyle
4523,2/18/2013 2:56:33 PM,4524,James,,Pruitt,,7900 =
Carmelita,Atascadero,CA,93422,,702,=3D"353",=3D"9760",=3D"",=3D"",=3D"",=3D=
"",=3D"",=3D"",=3D"2500.0000",True,False,0.0000,No =
one,False,False,Google,4524,2001,=3D"Toyota",Tacoma =
Xtracab,PreRunner,200000,V6 3.4 =
Liter,2WD,Automatic,199443~199448~199471~199480~199508~4234190~,0.0000,1,=
0,Gas,Pickup
------=_NextPart_000_0133_01CE0E98.061E7400--
Upvotes: 0
Views: 1997
Reputation: 1
here is my entire PHP file to extract a csv file(email with .csv attachment is piped to this script). The key is you have to decode the text that makes up the file, it is between the boundaries mentioned above.
<?
$home_path = '/home/USERNAME/public_html/DIRECTORY/';
$filename = "FILENAME.TXT"; // TO SAVE THE CONTENTS OF CSV FILE AS TEXT
$sock = fopen ("php://stdin", 'r');
$email = '';
while (!feof($sock)) {
$email .= fread($sock, 10240);
}
fclose($sock);
$encoding1 = explode ("------=_", $email);
$encoding2 = explode ("------=_", $encoding1[2]);
$encoding3 = $encoding2[0];
$encoding4 = explode (".csv", $encoding3);
$encoding5 = explode ("\n\n", $encoding4[2]);
$encoding = base64_decode($encoding5[1]);
$encoding = $encoding . "---" . strlen($encoding);
$fdw = fopen($home_path . $filename . ".txt", "w+");
fwrite($fdw, $encoding);
fclose($fdw);
?>
Upvotes: 0
Reputation: 32242
Attachments imply that the message is MIME/Multipart and has a boundary
header set which will delimit the various message parts. eg:
Content-Type: multipart/mixed;
boundary="b1_bb1b331cd6dafa1dc6a19e3b2e090b07"
So you want to find that first via something like:
preg_match('/boundary="(.*)"/', $message, $matches);
The message is divided by --
concatenated with the boundary, followed by headers specific to that part, a blank line, and then the data. eg:
--b1_bb1b331cd6dafa1dc6a19e3b2e090b07
Content-Type: application/pdf; name="Invoice-37272.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="Invoice-37272.pdf"
// base64 data here //
And the message is terminated with '--' . $boundary . '--'
, eg: --b1_bb1b331cd6dafa1dc6a19e3b2e090b07--
So you can use those two bits of info to break up your messages into their constituent parts and find/save the attachment.
Upvotes: 1