Saturday, October 24, 2009

Email Parsing and download attachment with IMAP in PHP

Last couple of days, i was working on a script where i need to download an attachment from a windows mail server and then read the contents inside the attachment and save it in database.

Tasks:
1- Read all unseen messages in the inbox.
2- Search for a specific subject.
3- Download the attachment(zip file) to local PC.
4- Unzip the file and extract the file inside it.
5- Read its contents inside the file.
6- Save the contents to a table(MySQL database).
7- Delete the extracted file after saving entries into table.
8- Move the zip file to a backup directory.
9- Do calculations on the data saved in the table.

Actually i had to do this for one of my project, where the sensors gateway will send mail to the windows server each hour, and i have to read the mail from a Linux machine.

As most of the work is completed on these scripts, i tested it under cron on local PC, and is working like a charm. So i am thinking to share the scripts with the community, maybe would help someone in the near future.

I have written 3 PHP scripts(pure PHP) with some MySQL tables to use for data saving.
I will start in the next post from the 1st script which is mail_parsing.php. I used PHP IMAP function to access the mail server.

Until then have a good time.
See you in the next post.

ANL

2 comments:

Liviu B,. said...

Hello!
I search your website but couldn't find any example script for what you did.. I need it for a project in school and I could really use it. I am stuck at the part where I extract the attachment from the email and open it in order to get the data from it (chapters 3-5).Can You help me??
Thanks in advance

M M Arif said...

Hi Liviu,
Sorry i did not update blog about these posts later on. The reason was busy life.
I am out station for a site visit for 2 days. I will for sure help you with this. Leave your email address here(after i read it, will delete your email from here). I will contact you ASAP.

Regards
Arif