Using Mutt with Gmail (POP3 over SSL)
This page is a guide to using the email client mutt, in conjunction with fetchmail, procmail and msmtp, to send, receive and read email under Linux using a Gmail account as a relay. It is different from the myriad of other "Mutt and Gmail" guides as it still uses the POP3 over SSL method, and has propounded this technique for many years. If you need to ask why anybody would want to put such an effort into an undertaking like this, and did not simply use the web interface of Gmail or even mutt with IMAP, perhaps this page is not for you. To use another's words:
Mutt is not designed to suit everyone, including those without basic knowledge about the Mail Concept, or those unwilling to perform configuration. Mutt is even not designed for the mass of "average users", although it works well for them, too. For those who prefer a client to "just work somehow" rather than give dedicated best performance, other mail programs are probably better suited.
Perhaps you already feel the lure of the incredibly powerful Linux console applications? If so read on and please use the email link at the base of this page to let me know how you profited, or otherwise, from the information on this page.
Contents of the Page
This is a fairly complex page that I believe would benefit from reading from top to bottom but I include some navigation here for those who would like to sample rather than consume:
- Downloading the Mail Receiving mail from Gmail.
- Sending the Mail Setting up the Mail Sending Agent (MSA) msmtp.
- Reading the Mail Setting up the console Mail User Agent (MUA) mutt.
I will not be dealing with the specifics of downloading and installing the required software for this page as there are too many distro-specific issues there. But obviously you will need to have installed OpenSSL, fetchmail, procmail, msmtp and mutt; all fairly standard Linux programs. Whether this is done by using your distro's version or compiling from source the end result should be the same. But before moving on to the actual setup I need to introduce my new friend John.
Introducing John ...
To avoid confusion in editing the many configuration files involved in this setup I will describe the setup of mutt and gmail for my new friend John, who has been created especially for this guide. John's details are as follows:
Gmail Address: email@example.com Gmail Password: rover Computer Username: john
John's details will always be in italics, bold and in red to remind you, Gentle Reader, to substitute your details for his. Hopefully this will lessen the confusion that I have unintentionally created with older versions of this page!
Downloading the Mail
Downloading the mail is perhaps the most complex part of this exercise but take it one step at a time, Gentle Reader, and it becomes quite logical. First step is to download and setup the required SSL certificates, then to setup fetchmail and finally to setup procmail.
Source the SSL Certificates
I formerly advocated a more manual method of setting the required SSL cert but in the interests of both expediency as well as a desire to avoid the vagaries of SSL certs and their support or otherwise by some organisations I am now advocating the simple installation of the certificate pack native to your particular Linus distro. For Slackware 64 (my distro) this can be found in the Slackware tree at:
If a full installation of Slackware has not been done use
to do the needful and this should leave the required package in the
root@illium/home/andrew# find /etc -iname ca-certificates.crt /etc/ssl/certs/ca-certificates.crt root@illium/home/andrew#
This is the path that I will use for the rest of this page but be aware that the path may different for your particular Linux distro.
Setting up fetchmail
Fetchmail is fairly easy to use and setup. You will need to create
$HOME/.fetchmailrc and then add the following
information for accessing the Gmail server:
poll pop.gmail.com with proto POP3 user 'john.example' there with password 'rover' is 'john' here mda "/usr/bin/procmail -d %T" options no keep ssl sslproto tls1 sslcertck # The path below is for Slackware and may differ on # your distro: sslcertpath /etc/ssl/certs/
It is almost too obvious to point out, Gentle Reader, that you will need to substitute your own details for password, username, email address etc. There remains a final touch, since the username and password are openly in this file you should make the file readable only by the file owner:
$ chmod 600 ~/.fetchmailrc
Now would be a good to time also to make sure you have POP forwarding enabled in your Gmail account. You will find this in: Settings - Forwarding and POP at the web interface of Gmail. Note as well that you cannot remove mail from Gmail servers via POP3 but you can choose to have your messages archived, kept or deleted once they have been downloaded via POP3. This is a Gmail setting hidden in Settings - Forwarding and POP: "When messages are accessed with POP..."
Setting up procmail
procmail is the final link in the chain for downloading mail. Before
setting it up there is a little bit of outstanding business to attend to:
~/.bashrc making the obvious substitution for username:
# Sets the Mail Environment Variable MAIL=/var/spool/mail/john && export MAIL
procmail will now know the location of the default mail spool and will
deliver all mail to there that has not been sorted to other locations.
But the creation of a
$HOME/.procmailrc is still required
and I give an example below. I have also added a sample filtering recipe
for the mutt user mailing list to give the very beginning of what
can be a complex process:
PATH=/bin:/usr/bin:/usr/local/bin VERBOSE=off DEFAULT=/var/spool/mail/john MAILDIR=$HOME/mail LOGFILE=$HOME/.procmaillog # Recipes below this comment: :0: * ^TOmutt-user mutt
The single filtering recipe means that procmail delivers all email
addressed to "mutt-user" directly to
All other mail goes to the default location
/var/spool/mail/username as specified by
~/.bashrc, Gentle Reader, I have demonstrated above how
To add this to
~/.procmailrc with the DEFAULT setting. For
the definitive guide to further recipes, and guidance on much more complex
recipes, don't forget to run
man procmailex, it all starts
to make sense after a while.
Sending the Mail
Although I am somewhat tempted by postfix, in particular as Slackware has
removed sendmail as default and placed postfix instead, msmtp is a better
choice for the simple task of sending mail via Gmail. A single configuration
file is required for msmtp:
$HOME/.msmtprc and the following
section gives the required details to access Gmail and reference the required
account default host smtp.gmail.com port 587 from firstname.lastname@example.org tls on tls_starttls on # The path below is for Slackware and may differ on # your distro: tls_trust_file /etc/ssl/certs/ca-certificates.crt auth on user john.example password rover logfile ~/.msmtp.log
I need not mention again, Gentle Reader, that there should be some fairly obvious changes here to substitute your own username, password and email address? And then the final touch, since the username and password are openly in this file, you should make the file readable only by the file owner:
$ chmod 600 ~/.msmtprc
msmtp is a great program that has many features that quite frankly I am still exploring, feel free to point out anything that I have missed, there is an email link at the base of this page for that purpose. But now finally to Mutt:
Reading the Mail
The information on this page has been comprehensively tested with
Mutt 1.8.3 (2017-05-23). Mutt is driven by the file
and I spent some time building this file from scratch but for you, Gentle
Reader, I include here a more basic version, similar to the one
I started from. Some parts of this, such as aliases and colors, are sourced
from their own file so don't forget to create these files.
#======================================================# # Boring details set realname = "john" set from = "email@example.com" set use_from = yes set envelope_from ="yes" # Use a signature set signature="~/.signature" # Use msmtp rather than sendmail. Check that # the path is correct for your system: set sendmail="/usr/bin/msmtp" # If not set in ~/.bashrc: set spoolfile = /var/spool/mail/john #======================================================# # Folders set folder="$HOME/mail" # Local mailboxes stored here set record="+sent" # Where to store sent messages set postponed="+postponed" # Where to store draft messages set mbox_type=mbox # Mailbox type set move=no # Don't move mail from spool #======================================================# # Watch these mailboxes for new mail, useful only if # Procmail or Maildrop is used to sort mail. mailboxes ! +slrn +fetchmail +mutt set sort_browser=alpha # Sort mailboxes by alpha(bet) #======================================================# # What to show and order of headers ignore * unignore Date: From: User-Agent: X-Mailer X-Operating-System To: \ Cc: Reply-To: Subject: Mail-Followup-To: hdr_order Date: From: User-Agent: X-Mailer X-Operating-System To: \ Cc: Reply-To: Subject: Mail-Followup-To: #======================================================# # which editor do you want to use? # vim of course! set editor="vim -c 'set tw=70 et' '+/^$' " set edit_headers=yes # See the headers when editing #======================================================# # Aliases set alias_file = ~/mail/mutt_aliases # In their own file source ~/mail/mutt_aliases # Source them set sort_alias=alias # Sort alphabetically #======================================================# # Colours: defaults are a little bleak so experiment! source ~/mutt/mutt_colors # In their own file #======================================================# # Lists: An example using the mutt-users list: lists mutt-users subscribe mutt-users set followup_to=yes # Sets 'Mail-Followup-To' header set honor_followup_to=yes fcc-hook mutt-user +mutt # See your own posts using fcc #======================================================# # Odds and ends set markers # mark wrapped lines of text in the pager with a + set smart_wrap # Don't wrap mid-word set pager_context=5 # Retain 5 lines of previous page when scrolling. set status_on_top # Status bar on top. push <show-version> # Shows mutt version at startup
I have been taken to task somewhat by one reader of this page who felt there should be a little more information about Mutt and colors and so ...
Coloring in Mutt
If your terminal supports color, and I believe it is a rarity these days for this not to be case, you can color almost any aspect of the Mutt window. All the fine details are in the Mutt manual: "Section 3: Configuration 8: Using color and mono video attributes". But I can tell you, Gentle Reader, that the basic usage is:
color object foreground background
This can be made a lot more complicated but a simple start is a good start. The basic colors are white, black, green, magenta, blue, cyan, yellow, red and default but you can also prefix a foreground color with "bright" to make the color bold. Now I personally use a Terminal with a white background so my own colors are:
#---- Mutt Colors for White Background ------- color hdrdefault black default color quoted red default color signature brightblack default color indicator brightwhite red color attachment black default color error red default color message blue default color search brightwhite magenta color status brightyellow blue color tree red default color normal blue default color tilde green default color bold brightyellow default color markers red default
There should also be a file called
as part of your Mutt installation which is intended for Terminals with
white backgrounds, this might also furnish a starting point as it did once
for me. Just to balance out the equation a little I will also give the colors
for those who prefer a black background. This example is taken directly
from another sample file
colors.linux which should be installed
along with Mutt:
#---- Mutt Colors for Black Background ------- color hdrdefault blue black color quoted blue black color signature blue black color attachment red black color message brightred black color error brightred black color indicator black red color status brightgreen blue color tree white black color normal white black color markers red black color search white black color tilde brightmagenta black color index blue black ~F color index red black "~N|~O"
If you don't like either of these feel free to experiment a little and come up with your own, most Mutt users will alter these basic colors. I prefer mine simple but if you pull out the manual you will see that you can spend many hours getting it exactly as you want. But now finally to check the mail:
Finally it is reward time as you open Mutt, type
open a shell prompt, type
fetchmail -v and start reading the
mail! My parting gift to you, Gentle Reader, is a little macro that was
written for me by a generous person on the mutt-user mailing list that
will actually do this for you when you simply press "I". Place
the following in your
macro index,pager I '<shell-escape> fetchmail -v<enter>'
~/.muttrc is intentionally a little basic, although
I suspect that it will cover most needs anyway. Don't be afraid however
to spend hour after hour painstakingly crafting your own to
produce that perfect setup as mutt encourages and rewards such efforts!
And in conclusion...
I wish you all the best with one of the truly great Linux console programs! Please send me an email, using Mutt of course, to let me know if you have found this page at all useful and as well to suggest any corrections that you feel should be made. If you are feeling generous perhaps you could also assist me keep this page alive by assisting with the hosting bills for this site, if not please feel free to utilise this page in any way you see fit and remember: "Have Fun!".