Science and technology

Send and obtain Gmail from the Linux command line

I am a Mutt person. I like viewing and composing emails within the comfort of my Linux terminal. With a light-weight and minimal shopper like Mutt, I do know that I can have my e mail accessible no matter system specs or web entry. And as a result of I’ve a Linux terminal open as a rule, my e mail shopper basically has no footprint on my desktop actual property. It’s hidden away in a terminal tab or multiplexer pane, so I can ignore it once I do not want it however get to it rapidly once I do want it.

A generally perceived downside with Mutt is that the majority of us use hosted e mail accounts today and work together with precise e mail protocols solely superficially. Mutt (and ELM earlier than it) was created again in less complicated instances, when checking e mail was a name to uucp and a look at /var/mail. However, it is tailored properly to growing expertise and works nicely with all types of recent protocols like POP, IMAP, and even LDAP, so you should use Mutt even if you happen to’re utilizing Gmail as your e mail host.

Because it is comparatively uncommon to run your personal e mail server at this time, and since Gmail is quite common, this tutorial assumes you are utilizing Mutt with Gmail. If you are involved about e mail privateness, contemplate opening an account with ProtonMail or Tutanota, each of which give totally encrypted e mail. Tutanota has many open source components, and ProtonMail gives an IMAP bridge for paid customers in order that you do not have to work round accessing your e mail exterior a browser. However, many firms, colleges, and organizations do not run their very own e mail providers and simply use Gmail, so you will have a Gmail account whether or not you need one or not.

If you might be running your own email server, establishing Mutt is even simpler than what I reveal on this article, so simply dive proper in.

Install Mutt

On Linux, you may set up Mutt out of your distribution’s software program repository after which create a .mutt listing to carry its configuration information:

$ sudo dnf set up mutt
$ mkdir ~/.mutt

On macOS, use MacPorts or Homebrew. On Windows, use Chocolatey.

Mutt is a mail person agent (MUA), which means its job is to learn, compose, and ship e mail to an outbound mail spool. It’s the job of another software or service to truly switch a message to or from a mail server (though there’s a variety of integration with Mutt in order that it looks as if it is doing all of the work even when it is not.) Understanding this separation of duties may help configuration make a bit of extra sense.

It additionally explains why you have to have helper purposes (along with Mutt) relying on what service you might want to talk with. For this text, I exploit IMAP in order that my native copy of e mail and my e mail supplier’s distant copy of mail stay synchronized. Should you determine to make use of POP as a substitute, that configuration is even simpler to configure and may be carried out with none exterior instruments. IMAP integration, nonetheless, requires OfflineIMAP, a Python software accessible from its GitHub repository.

Eventually, you can set up it with the python3 -m pip command, however as of this writing, you have to set up OfflineIMAP manually as a result of it is nonetheless being ported from Python 2 to Python three.

OfflineIMAP requires imaplib2, which can be in heavy growth, so I choose doing a handbook set up of that, as nicely. The course of is similar: clone the supply code repository with Git, develop into the listing, and set up with pip.

First, set up the rfc6555 dependency:

$ python3 -m pip set up --user rfc6555

Next, set up imaplib2 from supply:

$ git clone git@github.com:jazzband/imaplib2.git
$ pushd imaplib2.git
$ python3 -m pip set up --upgrade --user .
$ popd

Finally, set up OfflineIMAP from supply:

$ git clone git@github.com:OfflineIMAP/offlineimap3.git
$ pushd offlineimap3.git
$ python3 -m pip set up --upgrade --user .
$ popd

If you are utilizing Cygwin on Windows, then you have to additionally set up Portalocker.

Configure OfflineIMAP

OfflineIMAP reads the configuration file ~/.offlineimaprc by default. A template for this file, named offlineimap.conf, is included within the Git repository you cloned to put in OfflineIMAP. Move the instance file to your own home listing:

$ mv offlineimap3.git/offlineimap.conf ~/.offlineimaprc

Open the file in your favourite textual content editor and skim via it. It’s a well-commented file, and it is good to get aware of the choices accessible.

Here’s my .offlineimaprc for instance, with feedback eliminated for brevity. Some values could also be barely totally different for you, however this offers you an affordable concept of what your finish product should appear to be:

[general]
ui = ttyui
accounts = %your-gmail-username%
pythonfile = ~/.mutt/password_prompt.py
fsync = False

[Account %your-gmail-username%]
localrepository = %your-gmail-username%-Local
remoterepository = %your-gmail-username%-Remote
status_backend = sqlite
postsynchook = notmuch new

[Repository %your-gmail-username%-Local]
kind = Maildir
localfolders = ~/.mail/%your-gmail-username%-gmail.com
nametrans = lambda folder: 'drafts':  '[Gmail]/Drafts',
                            'despatched':    '[Gmail]/Sent Mail',
                            'flagged': '[Gmail]/Starred',
                            'trash':   '[Gmail]/Trash',
                            'archive': '[Gmail]/All Mail',
                            .get(folder, folder)

[Repository %your-gmail-username%-Remote]
maxconnections = 1
kind = Gmail
remoteuser = %your-gmail-username%@gmail.com
remotepasseval = '%your-gmail-API-password%'
## remotepasseval = get_api_pass()
sslcacertfile = /and many others/ssl/certs/ca-bundle.crt
realdelete = no
nametrans = lambda folder: .get(folder, folder)
folderfilter = lambda folder: folder not in ['[Gmail]/Trash',
                                             '[Gmail]/Important',
                                             '[Gmail]/Spam',
                                             ]

There are two replaceable values on this file: %your-gmail-username% and %your-gmail-API-password%. Replace the primary along with your Gmail person title. That’s the a part of your e mail deal with on the left of the @gmail.com half. You should purchase the second worth from Google via a two-factor authentication (2FA) setup course of (though you needn’t use 2FA to examine e mail).

Set up 2FA for Gmail

Google expects its customers to make use of the Gmail web site for e mail, so if you try to entry your e mail exterior of Gmail’s interface, you are basically doing in order a developer (even if you happen to do not contemplate your self a developer). In different phrases, you are creating what Google considers an “app.” To receive a developer-level app password, you have to arrange 2FA; via that course of, you get an app password, which Mutt can use to log in exterior the same old browser interface.

For security, you can too add a restoration e mail deal with. To try this, go to Google’s Account Security page and scroll all the way down to Recovery e mail.

To arrange 2FA, return to the Account Security web page, and click on on 2-step Verification to activate and configure it. This requires a cell phone for setup.

After activating 2FA, you get a brand new Google Account Security choice: App passwords. Click on it to create a brand new app password for Mutt. Google generates the password for you, so copy it and paste it into your .offlineimaprc file within the place of the %your-gmail-API-password% worth.

Placing your API password in your .offlineimaprc file shops it in plain textual content, which may be harmful. For a protracted whereas, I did this and felt advantageous about it as a result of my dwelling listing is encrypted. However, within the curiosity of higher safety, I now encrypt my API password with GnuPG. That’s considerably past the scope of this text, however I’ve written an article demonstrating find out how to set up GPG password integration.

Enable IMAP in Gmail

There’s one last item earlier than you may say goodbye to the Gmail internet interface ceaselessly: You should allow IMAP entry to your Gmail account.

To do that, go to the Gmail internet interface, click on the “cog” icon within the upper-right nook, and choose See all settings. In Gmail Settings, click on the POP/IMAP tab, and allow the radio button subsequent to Enable IMAP. Save your settings.

Now Gmail is configured to provide you entry to your e mail exterior an online browser.

Configure Mutt

Now that you simply’re all arrange for Mutt, you will be pleased to study that configuring Mutt is the straightforward half. As with .bashrc.zshrc, and .emacs information, there are various examples of superb .muttrc information accessible on the web. For my configuration file, I borrowed choices and concepts from Kyle Rankin, Paul Frields, and lots of others, so I’ve abbreviated my .muttrc file to only the necessities within the curiosity of simplicity:

set ssl_starttls=sure
set ssl_force_tls=sure

set from='[email protected]'
set realname='Tux Example'

set folder = imaps://imap.gmail.com/
set spoolfile = imaps://imap.gmail.com/INBOX
set postponed="imaps://imap.gmail.com/[Gmail]/Drafts"
set smtp_url="smtp://smtp.gmail.com:25"
set transfer = no
set imap_keepalive = 900
set document="imaps://imap.gmail.com/[Gmail]/Sent Mail"

# Paths
set folder           = ~/.mail
set alias_file       = ~/.mutt/alias
set header_cache     = "~/.mutt/cache/headers"
set message_cachedir = "~/.mutt/cache/bodies"
set certificate_file = ~/.mutt/certificates
set mailcap_path     = ~/.mutt/mailcap
set tmpdir           = ~/.mutt/temp
set signature        = ~/.mutt/sig
set sig_on_top       = sure

# Basic Options
set wait_key = no
set mbox_type = Maildir
unset transfer               # gmail does that

# Sidebar Patch
set sidebar_visible = sure
set sidebar_width   = 16
coloration sidebar_new coloration221 coloration233

## Account Settings
# Default inbox
set spoolfile = "+example.com/INBOX"

# Mailboxes to point out within the sidebar.
mailboxes +INBOX
          +despatched
          +drafts

# Other particular folder
set postponed = "+example.com/drafts"

# navigation
macro index gi "<change-folder>=example.com/INBOX<enter>" "Go to inbox"
macro index gt "<change-folder>=example.com/sent" "View sent"

Nothing on this file requires altering, however contemplate changing the faux title Tux Example and the faux deal with instance.com with one thing that applies to you. Copy and paste this textual content right into a file and reserve it as ~/.mutt/muttrc.

Launch Mutt

Before launching Mutt, run offlineimap from a terminal to sync your pc with the distant server. The first run of this takes a very long time, so depart it working in a single day.

Once your account has synchronized, you may launch Mutt:

$ mutt

Mutt prompts you for permission to create the directories it wants to prepare your e mail exercise after which shows a view of your inbox.

Learn Mutt

Learning Mutt is a combination of exploring the appliance and discovering your favourite hacks to your .muttrc config. For instance, my config file integrates Emacs for composing messages, LDAP in order that I can search via contacts, GnuPG in order that I can encrypt and decrypt messages, hyperlink harvesting, HTML views, and way more. You could make Mutt something you need it to be (so long as you need it to be an e mail shopper), and the extra you experiment, the extra you uncover.

Most Popular

To Top