How can I download attachments from emails through terminal using getmail?
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)
(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 calledcache
-
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 = "[email protected]" set realname = "Your Name" set imap_user = "[email protected]" set imap_pass = "" set folder = "imaps://imap.gmail.com:993" set spoolfile = "+INBOX" set postponed = "+[Gmail]/Drafts" set trash = "imaps://imap.gmail.com/[Gmail]/Trash" set header_cache=~/.mutt/cache/headers set message_cachedir=~/.mutt/cache/bodies set smtp_url = "smtp://[email protected]: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
.