Please help me figure out the right program to use to solve the following:

Everyday I have a report emailed as a PDF attachment to my inbox. The subject is the same, and it is from the same email address. I would like to be able to use a utility like fetchmail / getmail to reach into my inbox, match the subject and sender, and then download the attachment to a specific file.

Please help me identify the programs needed to:

  1. Connect to my Inbox
  2. Scan Subject Line and From Line
  3. Download attachment from email
  4. Delete from Inbox
  5. Send processed report to new person.

I run CentOS7, but any Linux should be fine for this task. Also, I'd like to use command line utilities for this task rather than Python or Ruby.

Additional information:

  • My email is stored on a Server on my LAN.
  • I think IMAP is what I want to use here (I'd like to keep the email on the server and only download the attachment)
  • I don't need to view the attachments, only download them into a directory.

Sorry to bother everyone with this question, but I've done research on this, but I can't seem to put the pieces of the puzzle together. Please let me know if there is any additional information you need.

  • Updated my question with the information you asked for.
    – cotton
    Mar 13, 2016 at 18:39
  • 2
    That's a lot of pieces. If you can reduce your question to one piece, you are more likely to get answers. And can't you do any part of this? And some details about the use case could be helpful. Mar 13, 2016 at 19:19
  • You didn't explain about your mailbox, but if you can use procmail and fetchmail, then see this post (kuther.net/howtos/…) for how to extract attachments from emails.
    – Rabin
    Mar 13, 2016 at 21:04
  • You say you'd "like to use command line utilities [...] rather than Python or Ruby". Is there a reason you're avoiding these/any scripting languages? Once you've written the script (using an existing IMAP library, such as imaplib) you should be able to run it like any other command line utility.
    – JigglyNaga
    Jul 3, 2018 at 11:54

2 Answers 2


I would suggest mutt. Install mutt, procmail and fetchmail. Mails will be stored here by default $HOME/Mail/ (your maildir; create). Help to setup Gmail in mutt. Setup fetchmail, procmail create an attachment directory

Add this to the $HOME/.procmailrc

ATTACH=`echo $HOME/Mail/Attachments`
:0 c
* ^To:.*me@email.com
* ^From.*notification@email.com
| munpack -q -C $ATTACH

$ATTACH directory will contain the filtered attachments (create). Specify SUBJECTNAME, To, From.


If you're into peeping somewhat deeper down the DIY well, you could do something with an expect script over an openssl process, by adapting the quite clear and straight-forward instruction of https://delog.wordpress.com/2011/05/10/access-imap-server-from-the-command-line-using-openssl/

That uses IMAP over SSL to access the mailbox and obtain the headers for some emails. You'll need to do the same, although only pick emails with special headers for collating their indexes, and then fetch their attachment parts into files.

The exact script would be around 8 IMAP commands with corresponding expect stanzas. You might need to refer to RFC3501 for these, especially for the variations to the FETCH command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.