Home > Uncategorized > Mutt aka Bash Is Still King

Mutt aka Bash Is Still King

December 5th, 2011 subogero

I’ve had problems with sending emails from the command line before. On Windows. That’s how mapis was born. This time it’s the same story on Linux. I want to send an email from the command line with an attachment. The rather lovely Evolution does not support it.

In comes mutt, the even more lovely terminal based email client. But I don’t want to loose features:

  • Gmail IMAP access
  • integration with my Gmail contacts
  • Hungarian spell-check
  • mutt as my default mail client in Gnome, so Firefox uses it when Sending Link…

And while I’m at it, I want some new features as well:

  • editing email with vim (with Hungarian spell-check, obviously)
  • bonus: email desktop notifications, even when the client is not running

Installation

apt-get install mutt  # as root

My ~/.muttrc config file:

set from = "your@email"
set realname = "Your Name"
set imap_user = "your@email"
set imap_pass = "yourpassword"
set folder = "imaps://imap.gmail.com:993"
set spoolfile = "+INBOX"
set postponed = "+[Gmail],Drafts"
set trash = "imaps://imap.gmail.com/[Gmail]/Trash"
set header_cache = ~/.mutt/cache/headers
set message_cachedir = ~/.mutt/cache/bodies
set certificate_file = ~/.mutt/certificates
set smtp_url = "smtp://your@email:587/"
set smtp_pass = "your_password"
set editor  = "vi"
set query_command = "goobook query '%s'"
set sort = "threads"
set sort_aux = "last-date-received"

color normal white default
color index white default ~p
color index white default ~P
color index white default ~O
color index brightwhite default ~N
color tree red default
color hdrdefault cyan default
color quoted green default
color tilde white default
color signature blue default
color attachment brightyellow default
color status brightwhite green
color indicator black white

You’ve probably noticed vi as the editor, some options that don’t let you miss new posts to old email threads and a colour scheme that fits well into Linux Mint. Also goobook is mentioned, which can access your Gmail  contacts from the command line. Homework: find the goobook website on the internet! Then download and extract the source tarball, run

sudo ./setup.py install

Then create its config file ~/.goobookrc

[DEFAULT]
email: your@email
password: your_password

As the above config files store your password, don’t forget to change their permissions:

chmod 700 ~/.muttrc ~/.goobookrc

As to the Hungarian spell-check in vi, add a line to ~/.vimrc which maps it to <F7>, like some office tools:

map <F7> :setlocal spell spelllang=hu<Enter>

The first time you activate it, it will download the Hungarian database, then highlight the misspelled words with red. Move around them with [s and ]s and let vim suggest correct spelling with the z= key combo. It kicks ass indeed.

Mutt as Gnome’s default email client? Main Menu (I don’t use the Mint Menu), System, Preferences, Preferred Applications. Mail Reader. Choose Custom, then specify “/usr/bin/mutt %s” as the command. Run in terminal of course. Test with Send Link… in Firefox.

Now all is well. Just try to send a file attachment from the command line.

mutt -a foo.txt

opens mutt, which in turn asks for the mail recipient. Just type a part of the name or email, hit Ctrl-t, and mutt will complete the email address from your Gmail Contacts. Then enter a subject and voilá, vi opens up to edit the mail body. Edit, spellcheck, ZZ, y. Done.

And now for something completely different. Desktop notifications for new email, even when no email client is running. I’ve found a nice mutt-Gmail tutorial blogpost somewhere, I forget where. It contained a Python script for email notifications. I opened it. I got scared. 200 lines of code! I nearly gave up. But after  taking a courageous look I realized it just parsed the Gmail atom feed and called Gnome’s notify-send. Using a class and using an XML parsing module.

Python is a rather lovely language, but I have to say its adherents are famous for writing bloated code. I bet I can write a 10-line shell script for that. I ended up with 11 lines, but including the shebang and a configurable checking period. It’s among my Gnome Startup Applications:

#!/bin/sh
SLEEP=`echo $1 | egrep '^[1-9][0-9]*$'`
[ -z "$SLEEP" ] && SLEEP=300
USER=`sed -rn 's/^set imap_user = "(.+)"$/\1/p' ~/.muttrc`
PASS=`sed -rn 's/^set imap_pass = "(.+)"$/\1/p' ~/.muttrc`
while true; do
 EMAILS=`curl -u ${USER}:${PASS} https://mail.google.com/mail/feed/atom \
 | sed -rn 's/<name>(.+)<\/name>$/\1/p'`
 [ -n "$EMAILS" ] && notify-send "NEW EMAIL" "$EMAILS"
 sleep $SLEEP
done

Bash is still king.

Categories: Uncategorized Tags: , , , ,
  1. gergo
    December 6th, 2011 at 08:00 | #1

    Install lynx
    # sudo apt-get install lynx

    set automatic html conversation in your ~/.muttrc
    auto_view text/html

    and the html mails will get a reasonable face again.

  2. December 6th, 2011 at 08:23 | #2

    Done. It’s cool. The links, however, disappeared. They used to be clickable in Gnome-terminal. It’s not a problem though. Only the bloody advertisers send html-email.

  3. gergo
    December 25th, 2011 at 09:53 | #3

    Ubuntu is a mess over Maverick.
    I step back to linux mint julia, which uses maverick repos,
    and viola spellcheck is working in vi.
    I got download error for vi earlier.

  4. gergo
    December 25th, 2011 at 10:00 | #4

    I recomend to use pip for python package management.
    # apt-get install pip
    The pipy recomendation is 1.4aplpha4 version from goobook.
    If you are not the brave one of the open source universe,
    you can install 1.3 like this
    # pip install goobook==1.3
    goobook eats .netrc too, so you have to use .goobookrc only if there is some space in your password,
    but this should not be the case.

Comments are closed.