Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I know I can download emails using a client, but is there a way to download emails straight from the terminal? I'm trying to fetch some emails from my gmail to later do some analysis on certain emails.

EDIT: Thank you for all the answers. Right now, I found a way to do it with getmail. But I encountered a small issue. It downloads the emails and attachments, but it prints everything in text. Is there a way to actually retrieve said attachment?

share|improve this question
up vote 3 down vote accepted

Gmail provides an IMAP service through which you can use any client that supports that protocol to fetch your e-mails. How that client makes them available on the disk can vary.

By far the most widely-used command-line IMAP client has to be fetchmail. You can install it easily in Ubuntu, then google for specific configurations to use with gmail.

One thing with fetchmail is that it slurps emails from the remote server and then pipes them through your local MTA. So you'd need to have a locally-configured MTA for this setup to work. The MTA would presumably then append the emails to /var/mail/$USER, and you can then read this file to do your processing.

Another tool that may work is offlineimap. I haven't used it; setup may be easier than with fetchmail.

As for your need to save attachments included in emails, what you need is to install mpack. Then, run the munpack command on the .txt file for each email (yes, the one that contains all the gibberish from the attachments). It will decode and save the attachments in individual files:

$ munpack mail.txt 
Invoice.PDF (application/pdf)
my-picture.png (image/png)
share|improve this answer
Thanks for the answer! I found another way to do it with getmail, which is very simple, but now I'm having issues with retrieving attachments as the file itself. Do you know of anything that can be of help? – Shelby. S Jul 27 '12 at 15:11
attachments are mime-encoded into each email. You need to decode them in order to view them. Maybe mimeview from the mailutils package could help. Never used it though, YMMV. – roadmr Jul 27 '12 at 15:14
Well I'm not trying to view the attachment, I'm trying to download the attachment, I'm not particularly interested in the contents, but I do want to keep a copy of the file. – Shelby. S Jul 27 '12 at 15:18
Well, you need to decode them in order to get a copy of the file :) – roadmr Jul 27 '12 at 16:11

(The question was about generally accessing email via the terminal, before the question poser specified getmail, but attachments are easily saved in Mutt also: see my note below)

You could use the program Mutt, which is available in the repositories. I have found that Mutt is highly configurable and fairly straightforward once you get used to it. The following instructions refer to setting it up for gmail using the imap and smtp protocols (make sure imap is enabled in your gmail account settings ).

It takes a little while to set up, but here's basically the quickest way to do it:

  • Make a directory called .mutt and within that create another one called cache
  • Next create a file called .muttrc to store your settings, although this does not go in the .mutt folder, but just in your home folder. Then enter the following settings in there; for the password I have left it "" so that you are prompted, as it's not a good idea to put your password in a plain text file like .muttrc. (Note: these settings are based on the Linux Format article on Mutt, LXF134, August 2010, pp. 96-97.)

    set from = ""
    set realname = "Your Name"
    set imap_user = ""
    set imap_pass = ""
    set folder = "imaps://"
    set spoolfile = "+INBOX"
    set postponed = "+[Gmail]/Drafts"
    set trash = "imaps://[Gmail]/Trash"
    set header_cache=~/.mutt/cache/headers
    set message_cachedir=~/.mutt/cache/bodies
    set smtp_url = "smtp://"
    set smtp_pass = ""
    set move = no
    set imap_keepalive = 900
    set sort=threads
    set sort_aux=date

Now with your settings saved in .muttrc, just type mutt in the terminal and enter your password when prompted. I have tested these settings and they work fine; look around for more settings to customise your Mutt experience, as macros can be used with the program to give it extra functionality. See the Mutt manual for this and more, which is also installed when you get the program.

To open attachments, select the message with the attachment, press v, select the attachment and press enter to view it in your default application. To save the attachment instead press s to save and enter a file name and location and press enter.

Here's a screenshot of Mutt

share|improve this answer
Thank you for the answer. Quick question, how does this method retrieve attached files? Does it save it as plain text, or? – Shelby. S Jul 27 '12 at 15:19
If your message contains an attachment, hit V and the attachment menu will be started; press enter to view the attachment in your default application if the attachment is not in plain text. – user76204 Jul 27 '12 at 15:25
Is there a way to let it automatically download all attachments? If it's a docx files then let it download the docx file and let it sit in a directory. Or would that be asking too much? – Shelby. S Jul 27 '12 at 15:27
Yes-press v to select the message containing the attachment, then select the attachment itself, press s to save it and choose a file name and location and press enter. – user76204 Jul 27 '12 at 15:28
Ah, I see, thanks! One more question though, how would I automate this? It feels like we're using a lot of user interaction, could I run this through a cron? – Shelby. S Jul 27 '12 at 15:32

You could use OfflineIMAP.

OfflineIMAP traditionally operates by maintaining a hierarchy of mail folders in Maildir format locally. Your own mail reader will read mail from this tree, and need never know that the mail comes from IMAP. OfflineIMAP will detect changes to the mail folders on your IMAP server and your own computer and bi-directionally synchronize them, copying, marking, and deleting messages as necessary.

It's in the universe repository, so you may need to enable that first, then install:


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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