login

Most people use Gmail. Some people like CLI mail clients. This post describes how I use Gmail in the best CLI mail client, mutt. Many people will back me up when I say it’s a very good setup.

This post shares a lot of information with my second mutt post regarding multiple accounts. If that’s something you’re planning on setting up, you can skip this tutorial as it’s a subset of the information there.

My full, working setup can always be found in my mutt-config repo.

Offlineimap

Step one is to setup Offlineimap to keep ~/Mail in sync with Gmail. This is a two way sync so anything moved, deleted, or sent from any IMAP-connected interface or our local mutt interface will act exactly the same. This also has the added benefit of storing offline, local copies of all your mails.

First, install Offlineimap and fill in an ~/.offlineimaprc like so:

[general]
ui = ttyui
accounts = Gmail

[Account Gmail]
localrepository = Gmail-Local
remoterepository = Gmail-Remote

[Repository Gmail-Local]
type = Maildir
localfolders = ~/Mail/Gmail

[Repository Gmail-Remote]
type = Gmail
remoteuser = you@gmail.com
remotepass = secret
realdelete = no
maxconnections = 3

# newer offlineimap needs this
cert_fingerprint = f3043dd689a2e7dddfbef82703a6c65ea9b634c1

Test that this works by running offlineimap -o. Your first sync could take some time, but once done, you should see the folders under ~/Mail/Gmail with the proper structure.

Offlineimap is kind of buggy for me; if I use its built-in refresh mechanism, I find it’ll often hang or quit and I’ll be left with an unsynced mailbox. Therefore, I choose to set offlineimap to never refresh and put a [re]start script in a cronjob to take care of it.

Once you’re sure things are syncing fine, set up a cron job to run a script called mailrun.sh every 3 minutes:

crontab -e

# add this:
*/3 * * * * /path/to/mailrun.sh

Then create that script with these contents:

#!/bin/bash
read -r pid < ~/.offlineimap/pid

if ps $pid &>/dev/null; then
  echo "offlineimap ($pid): another instance running." >&2
  kill -9 $pid
fi

offlineimap -o -u quiet &

And make it executable via chmod +x /path/to/mailrun.sh

You may have to restart cron for these changes to take affect.

Msmtp

Now we need a way to send mails. I like msmtp, you can also use other smtp clients. If you choose to install msmtp, the config file is at ~/.msmtprc and should look like this:

account default 
host smtp.gmail.com
port 587
protocol smtp
auth on
from user@gmail.com
user user@gmail.com
password secret
tls on
tls_nocertcheck

You can test this by executing echo "a test message" | msmtp you@gmail.com.

Mutt

Now the fun part! I don’t know how many hours I’ve spent in the past year fine tuning my muttrc, but it’ll never be done. Here are the parts required to get this setup working.

set mbox_type   = Maildir
set sendmail    = /usr/bin/msmtp

set folder      = ~/Mail
set spoolfile   = "+INBOX"
set mbox        = "+[Gmail]/All Mail"
set postponed   = "+[Gmail]/Drafts"
unset record

mailboxes +INBOX

macro index D \
    "<save-message>+[Gmail]/Trash<enter>" \
    "move message to the trash"

macro index S \
    "<save-message>+[Gmail]/Spam<enter>" \
    "mark message as spam"

The above should be enough to get a connection and start sending/receiving mail, but here are some other must-have options that make it feel a bit more like gmail:

# main options
set realname   = "Real Name"
set from       = "user@gmail.com"
set mail_check = 0
set envelope_from

unset move           # gmail does that
set delete           # don't ask, just do
unset confirmappend  # don't ask, just do!
set quit             # don't ask, just do!!
unset mark_old       # read/new is good enough for me

# sort/threading
set sort     = threads
set sort_aux = reverse-last-date-received
set sort_re

# look and feel
set pager_index_lines = 8
set pager_context     = 5
set pager_stop
set menu_scroll
set smart_wrap
set tilde
unset markers

# composing 
set fcc_attach
unset mime_forward
set forward_format = "Fwd: %s"
set include
set forward_quote

ignore *                               # first, ignore all headers
unignore from: to: cc: date: subject:  # then, show only these
hdr_order from: to: cc: date: subject: # and in this order

I’ve left out quite a few tweaks in the above so that those who are happy with mutt’s very sane defaults aren’t overwhelmed. Keep in mind, man muttrc is a great command for when you’re bored.

That should do it. Hopefully this info will get you going in the right direction.

published on Dec 5, 2009, tagged with linux, gmail, mutt

Comments

5 comments:

on Dec 30, 2011 , howardshippin wrote:

I read in https://wiki.archlinux.org/index.php/OfflineIMAP#Gmail_configuration : “Note: after version 6.3.5, offlineimap also creates remote folders to match your local ones. Thus you may need a nametrans rule for your local repository too that reverses the effects of this nametrans rule.” Not knowing how to play with nametrans syntax, and having read of bugs, I’ve been trying to use the native Gmail folder names - so far without success. Could you comment on either of these issues please

on Dec 30, 2011 , howardshippin wrote:

  • ok seem to have got the native Gmail folder names working.

on Jan 5, 2012 , pbrisbin wrote:

You know, it’s funny, someone came to me in IRC b/c they were getting errors using my nametrans.

Offlineimap was talking about some sort of cyclical dependency. He stated that copying the nametrans into both the Local and Remote blocks solved it for him (as the error instructed him to do so). I’m not sure how this works since your quote says the Local nametrans should “reverse” the Remote one so things stay in sync whereas what he had done would make the same translation twice (and, in that specific case, have no effect the second time).

During trouble shooting, I noticed that under his Mail directory he had folders of both the original and translated names (as well as a few mash-up versions too). I cited that as the likely culprit and suggested a cleanup – I haven’t heard back.

All I know is the following:

I’m using the latest version, (only) the nametrans listed in this post, and everything is working fine. I get no errors, and I have no extra folders created on the Remote side to match the differently-named version I keep locally. Offlineimap seems to deal with it sanely (which I would expect).

Perhaps if I were to all-of-a-sudden drop a new folder locally, and it was named in a way that my nametrans would have an effect, Offlineimap would push it up to the remote but then try to sync to a translated local version thus leaving that folder as extra (requiring an upload) and causing an infinite loop.

So, by keeping my local versions in accordance with the nametrans or by not adding ad-hoc local folders, I avoid the issue.

on Jul 20 , kim.callis wrote:

Ok, this is driving me nuts. I have finally switched back to using mutt everyday. Again, I fell back to using your configuration and once upon a time it work flawlessly. I am having two issues this time around! One is that offlineimap, does initially start unless I actually fat finger start runmail.sh. I have runmail.sh in cron, but absolutely nothing unless I start it. Once it is started, it pulls down the mail once, then nothing. Even if I restart runmail.sh, it doesn’t pull down any other mail. Secondly, when I try to send a message, and let it go out with msmtp, it seems like everything is good to go, but when I check for the message (a test message sent my gmail account), it never arrives. As I said, I am using your configuration almost to the letter, and it is failing. Any pointer would be greatly appreciated!

on Jul 23 , pbrisbin wrote:

Hi Kim,

First of all, make sure that the following things work on their own:

offlineimap -o

Try it a few times, see if you can get any sort of error message.

echo a test message | \
    msmtp -- you@whatever.com

See if you get an error or the message does/does not arrive.

Now, if the offlineimap command works consistently, then you’ve got an issue in runmail.sh or your cron setup. If it does not work, you should have an error you can report back to me and we can go from there.

If msmtp command works then you’ve got an issue in your muttrc where you’re defining the sendmail option. Similarly, if it does not work, you should have an error you can report back, or at least we know then the issue is your msmtprc.

While you’re at it, if you want, please post your configs somewhere so I can take a look.

Good luck, and let me know.

Please log in to post a comment.

Display ManagerStatus Bars in XMonad