Back to main page

The Quick-N-Dirty Guide to Using mutt with gmail

As gmail accounts become more ubiquitous, many find themselves using their gmail account for mailing lists or other places where they worry about spammers getting their email address. However, it can become a nuisance to open up a browser every time you wish to check your email. Also, at time of writing, gmail will only show mail that ~you~ send to a mailing list in your sent items box. You won't receive that mail in your inbox, according to their help center, in order to prevent clutter in the inbox. Still a gmail account can be useful.

This guide tells you how use gmail with mutt, the popular unix email client.

1.) Do you have mutt and gmail?

To find out if you have mutt, at a command prompt type
which mutt

As for gmail, if you don't have a friend with an extra gmail invite, then you probably aren't a real geek. Trust me, you can find one. If you don't have mutt then see our mutt article for more information.

2.) Installing

For help with installing take a look at our QND guide to installing software. If your distribution isn't covered, then you may have to install from source. Fear not, we have a QND guide for installing from source as well.

This article is actually making the assumption that you already have mutt installed and use it to send and receive email. This particular article uses getmail to fetch mail, but if you use fetchmail, hopefully, the procedure is similar enough so that you will be able to follow it. We are also going to install a lesser known program called msmtp. This is a small smtp client. The reason we are going to use it for our gmail account is because gmail uses SSL and many smtp clients don't handle that without a great deal of extra configuration.

Although msmtp isn't as popular as some other mail programs, many distributions have a package for it. If not, then the source code can be downloaded from its home page. We are going to make the assumption that you already have a program to send mail, and that you like it. We are only going to use msmtp to send mail through our gmail account.

When configuring msmtp be sure to include openssl support. ArchLinux includes this by default, in FreeBSD, if installing from ports one has to install it with

make -DWITH_OPENSSL install clean
If you later upgrade msmtp with portupgrade, don't forget to include it.
portupgrade -m -DWITH_OPENSSL msmtp

If installing from source, when configuring it, be sure to include the argument for openssl.

./configure --with-ssl=openssl

3.) Configuring getmail, mutt and msmtp

First we configure getmail. This is relatively simple. In our .getmail directory, we add a gmailrc file. In this case, the user name is john, his gmail address is john@gmail.com and his gmail password is 1234. Our gmailrc file looks like this
[options]
verbose = 0
delete = True

[retriever]
type = SimplePOP3SSLRetriever
server = pop.gmail.com
username = john
password = 1234

[destination]
type = MDA_external
path = /usr/local/bin/maildrop
unixfrom = True

In this case, the file is for a FreeBSD box which has either maildrop or procmail in /usr/local/bin. It may be different on your machine. (If you use procmail, simply substitute procmail for maildrop.) Next, log into your gmail account, go to settings, choose Forwarding and POP and enable pop downloading. If you have trouble with this, see their article about configuring pop downloading. It goes through the settings, complete with screenshots.

Once pop is enabled, we can test it by typing

getmail -v -rgmailrc

We should see that it polls the gmail pop server and retrieves any messages there.

Next we configure msmtp. We create .msmtprc in our home directory, and make its permissions read write for the owner. So first we create the file, using any text editor. It will read


#Gmail account

account gmail
host smtp.gmail.com
from john@gmail.com
auth on
tls on
tls_trust_file /etc/ssl/cert.pem
user john@gmail.com
password 1234
port 587

account default : gmail

The gmail home page says that one can use either port 465 or 587. However, others as well as myself have found that it only seems to work with port 587.

The tls_trust_file is relatively new. The msmtp home page covers it, but only gives instructions for Debian. One needs a cert.pem (sometimes called certs.pem, cacerts.pem or whatever) file. In FreeBSD the ca-roots port creates a /usr/local/share/certs/ca-root.crt which is symlinked to /etc/ssl/cert.pem. You can also use a ca-bundle.crt file included in /usr/local/share/examples/mutt/. However, the port maintainer quickly supplied a patch--now if you build msmtp with SSL or GNUTLS it also pulls in /usr/ports/security/ca-roots, which puts that ca-root.crt file in /usr/local/share/certs. (So, the above sample file is for FreeBSD).

You might have to dig around for it on your system. ArchLinux doesn't include it with their openssl package, but there will probably be a fix very soon. In the interim, if you trust these pages, you can use this copy of the FreeBSD cert.pem.
wget www.scottro.net/cert.pem

Put it somewhere, and have the tls_trust_file line point to it. For example, if you are user john, with a home directory of john, and just put the cert.pem file in your home directory, the line would read
tls_trust_file /home/john/cert.pem

The curl package also includes a /usr/share/curl/curl-ca-bundle.crt file that works. So you can always do a pacman -S curl and have your trust_file line point to curl's crt file. If you wish to create your own key file and self signed certs, you will have to do the following. (This is a digression, neither is necessary for msmtp to work with tls).
openssl genrsa -out privekey.pem 2048
openssl req -new -x509 -key privkey.pem -out cacert.pem -days 1095

The first command creates a private key, the second creates the certificate. It will prompt you for some information that the certificate uses. However, I wasn't able to use the self-signed certificate with msmtp. If you are unable to get a cert.pem or cacert.pem file that works, you can turn off tls cert checking by changing your .msmtprc to
#Gmail account

account gmail
host smtp.gmail.com
from john@gmail.com
auth on
tls on
tls_certcheck off
user john@gmail.com
password 1234
port 587

The msmtp home page advises against doing this. The cert.pem or similar file should be relatively easy to find. If you don't trust the one that I offer here, any FreeBSD using friend can get it for you by installing /usr/ports/security/ca-roots and giving you the /usr/local/share/ca-root file that the port will install. According to the msmtp home and man page, you should be able get it from a Debian using friend (which should include anyone who uses Ubuntu or its offshoots). To install it on a Debian based system, one uses
apt-get install ca-certificates

The msmtp man page mentions that you can then use /etc/ssl/certs/ca-certificates.crt, so you would change your .msmtprc file to point there. Going back to our own .msmtprc file, we now change its permissions.

chmod 0600 .msmtprc
If you want it to log to /var/log/maillog you can add the line
syslog LOG_MAIL

to your .msmtprc

Lastly, we add some lines to .muttrc.

macro generic "<esc>1" ":set from='John <john@gmail.com>'"
macro generic "<esc>2" ":set from='John <john@isp.com>'"
send2-hook '~f john@gmail.com' 'set sendmail="/usr/local/bin/msmtp"'

The example assumes that you already have an account that you use, as well as an smtp program. With the lines that we have added here, john will be able to switch back and forth between his gmail account and his normal isp.com account. Only the gmail account will use msmtp, otherwise, his normal MTA will be used.

If john opens mutt, then hits the escape key and the number one key, the next time he hits m to send mail, it will come from the john@gmail.com account. If he sends it, it will use msmtp as his mailer.

He can also simply compose email, then, before sending, hit escape and f to edit the from line. Either way will work, and both ways will call msmtp to do the mailing.

4.) Testing

We've already tested receiving. Now, john opens mutt, and either hits escape 1 or simply composes his email, then, before sending, hits escape f to change the from line, and changes it from his default john@isp.com to john@gmail.com. He hits y to send the email and he should see that it has been sent.

References

For mutt and its references, see our mutt QND guide.
We also have a getmail guide with references for further reading.
For msmtp, and some of its other uses, you can read its online manual. For using it with mutt there is the msmtp section of the mutt wiki.

Back to main page