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 = f3043dd689a2e7dddfbef82703a6c65ea9b634c1Test 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.shThen 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
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_nocertcheckYou 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 orderI’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
5 comments:
Please log in to post a comment.