Thursday, September 15, 2016

How to Extract Attachments from Gmail and Save to Files

This is a follow-up post from my previous post on how to retrieve messages from Gmail.

In order to save attached files, you just need to modify ~/.procmailrc file to read something like:
# First Recipe
:0HB
* ^(From).*(SOMEBODY)
* filename=
* ^Content-Disposition: attachment;
| munpack -q -C /WHERE/TO/SAVE

# Second Recipe
:0
/dev/null

The first recipe will filter out those mails from SOMEBODY with attachment(s) and save the file(s) to /WHERE/TO/SAVE folder. Make sure that you have mpack installed on the system:
$ sudo apt-get install -y mpack

The second recipe will simply discard all messages afterward. Therefore, nothing but the attachments will be saved locally.

No comments:

Post a Comment