/ root / pages / mutt.html

You're using an old link! - Thankfully, you no longer need to specify a nonstandard port (8080) to access my site. You could've used the more standard: http://pbrisbin.com/pages/mutt.html.

Mutt + Gmail + Offlineimap


If you're interested, I've written a second tutorial regarding my new setup with two IMAP accounts accessed within Mutt. It contains a lot of the same information presented below and is slightly more up to date as far as configuration goes. You can find it here.

Continue original post...

I've been using this email solution for some time now. My muttrc has been called 'epic' and 'awesome', so I figured I'd share my setup and how it got that way here.

What is it? A couple of applications working together to offer a simple, clean, usable way of interacting with my gmail account.

Offlineimap syncs a local Maildir at ~/Mail with my gmail IMAP account every 3 minutes. I use mutt to interface with said Maildir, and msmtp to send email from mutt via gmails smtp server. Full config files can be found with my dotfiles, but I'll show the important parts here.

What it looks like

Mutt-ng (mutt with sidebar patch) with a message being viewed in the pager.

Mutt 
        Shot

Looking at All Mail (archive) with nifty color coding

Mutt 
        Shot 2

Composing a message in vim with mutt syntax highlighting

Mutt 
        Shot 3

Offlineimap

Step one is to setup Offlineimap to keep ~/Mail in sync with our gmail's IMAP folder. 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 = Curses.Blinkenlights
  #ui = TTY.TTYUI
  ui = Noninteractive.Quiet
  #ui = Noninteractive.Basic
  accounts = GMail

  [Account GMail]
  localrepository = Local
  remoterepository = Remote
  autorefresh = 0 
  quick = 0

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

  [Repository Remote]
  type = Gmail 
  remoteuser = user@gmail.com
  remotepass = XXXXX 
  realdelete = no
  nametrans = lambda folder: re.sub('.*Spam$', 'spam', re.sub('.*Drafts$', 'drafts', re.sub('.*Sent Mail$', 'sent', re.sub('.*Starred$', 'flagged', re.sub('.*Trash$', 'trash', re.sub('.*All Mail$', 'archive', folder))))))

  # vim: ft=cfg tw=0

The first thing to note is nametrans. My line there will change folder names during the sync so IMAP/[Gmail].Archive becomes ~/Mail/archive and similar for all other folders. You can choose not to use this line if you don't mind the default folder names, but you'll have to be careful to use the right folder names in your ~/.muttrc

Also be aware of the ui setting. Noninteractive.Quiet means no ouput (or very little) and is good once it's in a cron job. During initial setup, you should probably uncomment Blinkenlights instead and run offlineimap by itself from the commandline, viewing its output, to be sure it's working properly.

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 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

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

Then create that script with these contents:

#!/bin/sh

PID=`pgrep offlineimap`

[ -n "$PID" ] && kill $PID

offlineimap &

exit 0

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 via gmail's smtp server. 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:

  # msmtp config file

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

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 [I think] just the parts required to get this setup working. Again, feel free to steal all the tweaks in my actual muttrc and the relevant sourced files.

To connect to our Maildir at ~/Mail:

  # options
  set mbox_type   = Maildir               # mailbox type
  set folder      = ~/Mail                # root folder
  set spoolfile   = "+INBOX"              # inbox
  set mbox        = "+archive"            # [Gmail]/All Mail
  set postponed   = "+drafts"             # [Gmail]/Drafts
  unset record                            # required to prevent duplicates in Sent

  # mailboxes
  mailboxes +INBOX +archive +sent +drafts +spam +trash

  # bindings
  macro index D "<save-message>+trash<enter>"   "move message to the trash"
  macro index S "<save-message>+spam<enter>"    "mark message as spam"

The above expects my nametrans switch in offlineimaprc

This line is all that's required to use our msmtp setup to send mail:

  set sendmail            = /usr/bin/msmtp                # to use msmtp 'default' account
  #set sendmail           = "/usr/bin/msmtp -a gmail"     # if in a multi-account situation

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"                   # who am i?
  set from                = "user@gmail.com"              # who am i?
  set envelope_from                                       # which from?
  set mail_check          = 0                             # check for mail always
  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
  set xterm_set_titles                                    # set my title (muttng only?)

  # index options
  set sort                = threads                       # like gmail 
  set sort_aux            = reverse-last-date-received    # like gmail
  set sort_re                                             # thread based on reply_regexp

  # pager
  set pager_index_lines   = 8                             # show 8 messages when in pager
  set pager_context       = 5                             # show five lines when skipping in pager
  set pager_stop                                          # don't go to next message automatically
  set menu_scroll                                         # scroll in menus 
  set smart_wrap                                          # don't split words
  set tilde                                               # show tildes like in vim
  unset markers                                           # no ugly plus signs

  # composing mail
  set fcc_attach                                          # save attachments with the body
  unset mime_forward                                      # forward attachments as part of body
  set forward_format      = "Fwd: %s"                     # format for subject when forwarding
  set include                                             # include message in replies
  set forward_quote                                       # include message in forwards

  # headers to show
  ignore *                                                # ignore all headers
  unignore from: to: cc: date: subject:                   # 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.

If you like the sidebar in the screenshot, you'll have to patch mutt (or install mutt-ng) to enable it, then add something like the following to your ~/.muttrc:

  # muttng sidebar stuff
  set sidebar_delim       = ''
  set sidebar_visible     = yes
  set sidebar_width       = 15 

  # sidebar bindings
  bind index,pager \CN    sidebar-next
  bind index,pager \CP    sidebar-prev
  bind index,pager \CO    sidebar-open

That should do it. Hopefully this info will get you going in the right direction. You might notice my various mutt settings are spread across a few different files. These are then sourced in the main ~/.muttrc. You might also find this approach useful, or you can just put everything in one file.

One Finishing Touch

Obviously, one can use whatever editor they like to compose messages in mutt. Personally, I'm extremely comfortable in vim; so here are two tricks to make vim a little more mutt-friendly.

First, set your editor this way in muttrc to skip passed any headers when composing:

  set editor = "/usr/bin/vim +/^$"

Second, add a file at ~/.vim/mail.vimrc with the following contents:

  " To use with Mutt, just put this line your ~/.vimrc :
  "   autocmd BufRead ~/.mutt/temp/mutt*      :source ~/.vim/mail

  " * <F1> to re-format the current paragraph correctly
  " * <F2> to format a line which is too long, and go to the next line
  " * <F3> to merge the previous line with the current one, with a correct
  "        formatting (sometimes useful associated with <F2>)

  " turn on spell checking
  set spell

  " reformat paragraphs
  nmap    <F1>    gqap
  nmap    <F2>    gqqj
  nmap    <F3>    kgqj
  map!    <F1>    <ESC>gqapi
  map!    <F2>    <ESC>gqqji
  map!    <F3>    <ESC>kgqji

As can be read in the comments, this automatically moves your cursor to the right place when composing a new mail, and it gives you handy F{1,2,3} mappings for reformatting paragraphs.

To apply these nifty tweaks when composing mail, we need to add the following to ~/.vimrc:

  autocmd BufRead ~/.mutt/temp/mutt*   :source ~/.vim/mail.vimrc

My muttrc is set up to keep temp files in ~/.mutt/temp. By default, I believe messages are stored somewhere in /tmp so adjust the above line as needed.

Comments

on Sat, 19 Jun 2010 14:28:56 -0400, Jacques wrote:

just great

on Wed, 25 Aug 2010 11:01:44 -0400, Scott wrote:

Pretty cool, but when I do this and reply to an email, the header is quoted and then I start typing after the quoted email's header. Is there a way to fix that?

on Wed, 25 Aug 2010 12:32:11 -0400, me wrote:

I'm not sure what you mean.

set editor = "/usr/bin/vim +/^$" simply means use vim and skip to the first blank line in the file.

Is this not what's happening for you?





pbrisbin dot com 2010