tdited Posted May 15, 2013 Share Posted May 15, 2013 I am trying to extract csv attachments from emails that have been piped to a php script. The pipe is working, and for the most part the php script is working as well. Unless someone sends the csv file from a source such as an apple iphone. Then the script begins on the wrong line to start the data extraction. Can anyone help me fine tune this to work cleaner?? OR is there simply a better way to extract an attachment from a piped email? Thanks! #!/usr/bin/php -q <?PHP ## sql connection credentials go here. $fd = fopen("php://stdin", "r"); if ($fd) { $t='0'; while (($fde = fgets($fd, 4096)) !== false) { ## Opens the email and saves each line into an array called $email ## $email[$t] = $fde; # $message .= $fde; $t++; } fclose($fd); for ($h='0'; $h<$t; $h++) { ## Splits each line by comma and checks position [0] for specific wording the first line in the csv file. saves line position once found ## $output = explode(",",$email[$h]); if ($output[0] == "item location") { $j = $h+2; # on line $j because that is where I found the item location } } $l='0'; for ($b=$j; $b<$t; $b++) { $pos2 = strrpos($email[$b], "_NextPart_"); if ($pos2 === false) { $pos = strrpos($email[$b], "="); if ($pos === false) { $themail[$l] = $email[$b]; } else { # Addresses a problem of = signs showing up. if I see an = sign combine the next two rows into one row and remove the equals sign. AND add 2 to $b instead of 1 $z=$b+1; $e = array('"', '='); $themail[$l] = "".str_replace($e, '', $email[$b])."".$email[$z].""; $b++; } } else { $b=$t; } $l++; } ## starting point is $j $i=0; for ($k=0; $k<$l; $k++) { ## Splits each line by comma and saves each item from each line to an array ## $output = explode(",",$themail[$k]); foreach ($output as $value) { $foutput[$i] = $value; $i++; } } $r='0'; if ($l > 50) { ## this is done to limit the number of rows being uploaded at one time to 50 max. For some reason, the script starts to get out of sync after 55 rows are processed. 50 seems appropriate anyway ## $l = 50; } for ($e='0'; $e<$l; $e++) { #echo "<br>Set $e <br>"; $r=$e*8; $s='0'; for ($w=$r; $w<$r+8; $w++) { $line[$s] = $foutput[$w]; ## upload the data here ## $s++; } ## sql SELECT statement looks for existing matching data, if it find it then it UPDATEs the line, if not then it INSERTs the line. ?> Quote Link to comment https://forums.phpfreaks.com/topic/278045-extract-csv-attachment-from-emails-piped-to-php/ Share on other sites More sharing options...
tdited Posted May 20, 2013 Author Share Posted May 20, 2013 Bump. Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/278045-extract-csv-attachment-from-emails-piped-to-php/#findComment-1431148 Share on other sites More sharing options...
Muddy_Funster Posted May 20, 2013 Share Posted May 20, 2013 I'm not sure exactly what process you are using here, but I imagine that if I were going to do it I would work with the MIME headers and boundaries. Each multipart email (one that contains more than a single form of data e.g. attachment) has each part of the multipart data wrapped inside it's own MIME boundary header, this defines, among other things, the datatype of that mart of the message. So I would be searching for the boundary string, then checking within each boundary for the "content-type='application/csv'" and probably the "content-disposition='attachment'" as well to identify the relevent data for the CSV file. I would then find the encoding used on that part of the MIME message and throw it at a CASE or nested IF that would decode using the relevant base, i.e. base64, depending on what the sender had used to encode in the first place. I don't know if any of that is what you are looking for, but it's the best I got Quote Link to comment https://forums.phpfreaks.com/topic/278045-extract-csv-attachment-from-emails-piped-to-php/#findComment-1431152 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.