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?
3 Answers
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) 4 (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
.muttand within that create another one calledcache Next create a file called
.muttrcto store your settings, although this does not go in the.muttfolder, 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://imap.gmail.com:993" set spoolfile = "+INBOX" set postponed = "+[Gmail]/Drafts" set trash = "imaps://" set header_cache=~/.mutt/cache/headers set message_cachedir=~/.mutt/cache/bodies set smtp_url = "smtp://:587/" 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.
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: