granth green sumo

turkey sandwiches!

Photo Photo Photo Photo Photo Photo

Mutt With Imap and Ssl on Os X 23 November 2006

os10, mail, mutt, imap, ssl

Mutt

Why use Mutt instead of a pretty graphical mail client? Because it still sucks less than anything else I’ve tried.

Mutt is incredibly configurable, which of course means it takes a long time to set up.

install

Install MacPorts if you don’t have it.

$ sudo port install mutt-devel +headercache +imap +ssl
$ sudo port install msmtp mailtomutt lbdb urlview w3m

configure Mutt

Add the following to ~/.muttrc. Use your own email address and mail server, obvs.

set query_command="lbdbq %s"  # little brother talks to address book
set text_flowed  # use format=flowed
unset wait_key   # return immediately from external programs
set encode_from  # use quoted-printable for From at the start of a line
unset metoo      # remove me from CC
set edit_headers
set autoedit
set read_inc=100

# imap
set imap_idle
set imap_user=grant
set imap_pass=`security find-internet-password -g -r pami -s mail.antiflux.org 2>&1 >/dev/null | cut -d\" -f2`
set folder=imaps://mail.antiflux.org/mail/
set spoolfile=imaps://mail.antiflux.org/inbox
unset record         # no sent-mail folder
fcc-hook !(~l|~p) !  # save non-list, non-to-me messages to the inbox
set header_cache="~/.mutt/cache/headers"
set message_cachedir="~/.mutt/cache/bodies"
set certificate_file=~/.mutt/certificates
set ssl_force_tls
set mail_check=300

# smtp
set use_envelope_from
set sendmail="/opt/local/bin/msmtp"
set from="Grant Hollingworth <grant@antiflux.org>"
set hostname=antiflux.org
set sendmail_wait=-1  # send in background

# 'mailboxes' has to be after 'set folder' for the '+' to work`
# e.g., mailboxes ! +debian-security-announce

caches

Mutt will cache the headers of the mail in each folder you look at, as well as the bodies of all the mail you’ve read.

mkdir -p ~/.mutt/cache/{bodies,headers}

This stuff adds up, though, and Mutt doesn’t delete old headers and bodies. Make a cron entry to delete cache files more than a week old. Run crontab -e and add the following line:

@hourly find ~/.mutt/cache -mtime +7 -mindepth 2 -delete

smtp

Mutt doesn’t have SMTP support built in. Instead, it expects a sendmail-style command to be available. You can use Postfix, which comes with OS X, but I prefer to send the mail using mail.antiflux.org, my mail server. msmtp acts like sendmail and sends mail by SMTP over SSL.

Create ~/.msmtprc with the following, adjusted for your own account:

account default
host mail.antiflux.org
from grant@antiflux.org
auth on
user grant
password SEKRIT
tls on
tls_trust_file /Users/grant/.mutt/certificate-authority
tls_starttls off
domain spooky.example.org

mail.antiflux.org has an SSL certificate, but it’s signed by our own certificate authority. To tell msmtp I trust it, I downloaded the authority certificate.

curl http://antiflux.org/ca/antiflux.org-root.pem > ~/.mutt/certificate-authority

The domain setting on the last line of .msmtprc is a bit of a kluge. By default, msmtp tells the mail server its name is ‘localhost’. mail.antiflux.org rejects potential spam by denying external mail claiming to be local. I used the example.org domain to work around this.

address book

The Little Brother’s Database (lbdb) can search Apple’s Address Book for email addresses. It also knows how to read Mutt’s alias file, Pine’s address book LDAP directories, and more.

Make ~/.lbdbrc:

MODULES_PATH="$HOME/.lbdb /opt/local/lib/lbdb"
METHODS="m_muttalias m_osx_addressbook"

If you want to search for names with accented characters, you need to make your own version of the m_osx_addressbook script.

mkdir ~/.lbdb

~/.lbdb/m_osx_addressbook:

#! /bin/bash -posix
#
# -*-sh-*-
#
#     Copyright (C) 2003  Brendan Cully <brendan@kublai.com>
#     Copyright (C) 2005  Roland Rosenfeld <roland@spinnaker.de>
# 
#     This program is free software; you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation; either version 2 of the License, or
#     (at your option) any later version.
# 
#     This program is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
# 
#     You should have received a copy of the GNU General Public License
#     along with this program; if not, write to the Free Software
#     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
#

libdir=/opt/local/lib/lbdb/

m_osx_addressbook_query()
{
  $libdir/ABQuery `echo "$@" | iconv -f 'utf-8' -t 'macroman'` | grep -ia "$@"
}

Copying links and pasting them into your browser gets old after a while. You can’t just click on links in Mutt as in a graphical mail client. The next best thing is urlview, which you can run with ⌃B.

~/.urlview:

COMMAND open %s

urlview will use open(1) for all links. open will use your default browser.

A lot of mailing lists have headers with URLs in them, which I don’t want to see in the list of links. I made a wrapper to chop them out.

~/bin/urlview:

#!/bin/sh
# throw away mail headers to avoid list URLs
ruby -e 'gets until /^\n$/; print while gets' $@ | /opt/local/bin/urlview

For this to work, $HOME/bin should be before /opt/local/bin in $PATH.

viewing HTML mail and images

curl http://filibusta.crema.unimi.it/~gufo/files/view_attachment.sh > ~/bin/view_attachment
chmod +x ~/bin/view_attachment

~/.mailcap:

text/html; view_attachment %s html; needsterminal
text/html; w3m -dump -T %t; copiousoutput
image/*; view_attachment %s

~/.muttrc:

alternative_order text/plain *
auto_view text/html

With this setup, Mutt will show the text/plain section of a MIME message with both plain text and HTML. If the message is HTML-only, it will convert it to plain text with w3m. To view HTML in your browser, hit v for the attachment menu, select the attachment, and hit enter.

use UTF-8

Terminal.app uses UTF-8 encoding of text by default, but not all your programs will know that.

add to ~/.profile (or ~/.MacOSX/environment.plist):

export LC_CTYPE=en_US.UTF-8

add to ~/.inputrc:

set input-meta on
set output-meta on
set convert-meta off

Finally, in Terminal’s “Window Settings”, turn off the wide glyphs options under “Display”.

use TextMate to write your mail

Buy TextMate if you haven’t already. It’s really good.

in ~/.muttrc:

set editor="mate -w"

To look up addresses, use the Find Email Address in Address Book command, written by Brett Terpstra and me.

Download and double click it to install. Type part of a name in TextMate and hit ⌃⇧⌘A.

MailtoMutt is a simple program that runs Mutt when you click on a mailto URL. It leaves a Terminal window and itself running after you send the message. Ugly, but it’s better than nothing.

Configure MailtoMutt as the default app for mailto links using RCDefaultApp.

previously: Fixed Pitchfork Feeds