Search the Community
Showing results for tags 'piped emails'.
-
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. ?>
- 2 replies
-
- csv
- piped emails
-
(and 1 more)
Tagged with: