Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access GMail (IMAP Email) from my Shell/Python script to download a zip file attached to an email and process it?

I have to process a file everyday. This file is sent to my Email once everyday. If I can get to this email once every day and download the attachment, that had be awesome. Is it even remotely possible to do such a thing?

Thanks!

like image 746
ThinkCode Avatar asked Jun 08 '10 18:06

ThinkCode


3 Answers

Please see How can I download all emails with attachments from Gmail? for a practical example.

like image 153
Tarantula Avatar answered Nov 16 '22 17:11

Tarantula


This is certainly possible. Check out imaplib in Python's standard library; with it doing what you want should be quite straightforward. Also, you can process zip files directly in Python using the zipfile library.

like image 23
taleinat Avatar answered Nov 16 '22 17:11

taleinat


Your best bet is to create an IMAP Folder for your daily emails to be sent to and then create a filter in GMail to send those files there. Your Python script can then check ONLY that folder on some interval and assume that whatever ends up in there is the file you want.

A quick search yielded sooo many results for IMAP fetching examples in Python, I'll leave that part up to you, but I will say that libgmail looks pretty neat.

like image 1
Mark Bowytz Avatar answered Nov 16 '22 18:11

Mark Bowytz