Script to parse emails for attachments

2019-08-24 17:58发布

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条回答
Fickle 薄情
2楼-- · 2019-08-24 18:19

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

查看更多
登录 后发表回答