views:

62

answers:

1

I am looking for a way to monitor a Linux mbox email account, when an email arrives I would like to download an attachment from the email and save the attachment (CSV file) so that it may be used by a PHP script. What would be the best way of going about this? I have looked at PHP's IMAP functions but this does not appear to be the most appropriate method when a simple bash script may be all that is required?

+1  A: 

For this situation I pipe the email to a PHP script and let the PHP script parse the email. You get instant results versus waiting for a cronjob to pull emails down

$stdin = fopen('php://stdin', 'r');
while (!feof($stdin))
{
$input .= fread($stdin, 8192);
}

now you have the entire email in $input and you can use the boundries to extract the base64 encoded information and then file_put_contents("/tmp/file.csv",base64_decode($extracted_file_contents))

make sure you chmod +x

Geek Num 88
Thanks, this looks perfect - when you say pipe the email, do you have a particular method, do you amend the mailbox settings somehow to call a script, my server runs Plesk so the mailbox is handled by qmail?
Swanny
I use this with qmail - you just have to add a .qmail file to the user directory and put in |/var/www/htdocs/script.php
Geek Num 88
If you don't have CLI access there should be a way in plesk to forward emails to a script
Geek Num 88
Excellent, will check this out. Thanks.
Swanny