[mutt] Configure mutt to receive email via IMAP and send via SMTP

05 May

“All mail clients suck. This one [mutt] just sucks less.” Michael Elkins, ca. 1995

Actually I have tried a lot of desktop email clients (viz., Outlook, thunderbird, seamonkey, sylpheed, pine, alpine, claws email, kmail, evolution, opera) and in the long run they all starts sucking at the RAM. Now to be precise, I must tell you that it’s not a full-fledged mail-transfer agent (MTA), but rather a mail user agent (MUA), i.e. just an email client just like those previously mentioned.

Step 1: Create a password file, say ~/.secret/.passwd. Enter the passwords in it as follows and save the file (don’t worry, we’re going to encrypt it soon!):

GMail:    passwd1
Yahoo:    passwd2

Step 2: Now encrypt the file with your gpg key (refer to the previous post for details). Don’t forget to delete the unencrypted file.

$ cd ~/.secret
$ gpg -e .passwd -o .passwd.gpg
$ rm -f .passwd

Step 3a: back to the home direcotory and modify the ~/.muttrc file: (you may equivalently do the same in ~/.mutt/muttrc)

# Process the password files first
set my_tmpsecret=`gpg -o ~/.secret/.tmp -d ~/.secret/.passwd.gpg`
set my_gpass=`awk '/GMail:/ {print $2}' ~/.secret/.tmp`
set my_ypass=`awk '/Yahoo:/ {print $2}' ~/.secret/.tmp`
set my_del=`rm -f ~/.secret/.tmp`

Note: All user-defined variable must start with “my_”.

Step 3b: Now set up mutt for IMAP: (let’s say for GMail)

set imap_user=""
set imap_pass=$my_gpass
set folder="imaps://"
set spoolfile="+INBOX"
set postponed="+[Gmail]/Drafts"
set record=+[Gmail]/"Sent Mail"

Step 3c: Now let’s say we want to use Yahoo’s SMTP service to send emails: here’s how to set it up in ~/.muttrc:

set smtp_pass=$my_ypass
set smtp_url = "smtp://"  # using port 587 for TLS
set from=""
set realname="Your Real_Name"

Step 3d: Set your favorite editor to compose emails next in ~/.muttrc (mine is vim, so….)

set editor='vim + -c "set textwidth=72" -c "set wrap" -c "set nocp" -c "?^$"'

Step 3e: Other necessary things to put in ~/.muttrc:

set header_cache=~/.mutt/cache/headers
set message_cachedir=~/.mutt/cache/bodies
set certificate_file=~/.mutt/certificates

Step 3f: Now time for some pretty color: (add these too to ~/.muttrc)

color  body            brightyellow    black    (((ht|f)tps?)|mailto):(//)?[^\ "\t]*|www\.[-a-z0-9.]+)[^\ .,;\t>">]
mono   body            bold                     (((ht|f)tps?)|mailto):(//)?[^\ "\t]*|www\.[-a-z0-9.]+)[^\ .,;\t>">]
color  body            brightmagenta   black   "[-a-z_0-9.]+@[-a-z_0-9.]+"
mono   body            bold                    "[-a-z_0-9.]+@[-a-z_0-9.]+"
color  body            brightyellow    black   "^Good signature"
mono   body            bold                    "^Good signature"
color  body            brightwhite     red     "^Bad signature from.*"
mono   body            bold                    "^Bad signature from.*"

Step 3g: Some other customizations to put in ~/.muttrc:

set smart_wrap = yes
set sort = 'threads'
set sort_aux = 'last-date-received'
set imap_check_subscribed
ignore "Authentication-Results:"
ignore "DomainKey-Signature:"
ignore "DKIM-Signature:"
hdr_order Date From To Cc
set timeout=60	#Check for mail every minute
set mail_check=5

Step 3h: Now save the file in your home directory and make it readable by none but the current user (you of course!):

$ chmod 700 ~/.muttrc

Step 4:Next create the these two directories to save mutt related stuffs:

$ mkdir .mutt
$ mkdir .mutt/cache

Step 5:To render the HTML/etc messages correctly create another file ~/.mailcap with the following as its content make it readable by you only (use chmod as above)

text/html; lynx -dump %s ; copiousoutput

Change lynx to links/links2/elinks depending what you actually have on your system.

Things to do:
I still cannot use multiple IMAP inboxes at a time — the account-hook / folder-hook does not seem to work. I need to spend some more time on it.

Here’s how to use multiple IMAP accounts in mutt.

References for this post: Crunchbang Linux site (my latest favorite distro!), muttrc builder, and, of course. Another old reference from the days when mutt wouldn’t do SMTP. Also look at the references at the end of this wikipedia page. To build mutt from the source, refer to this.

References for to-do: Derkeller and mutt-users forum (from 2002!).

About these ads

Posted by on May 5, 2010 in linux, osx, unix


Tags: , , , , , , , , , , ,

11 responses to “[mutt] Configure mutt to receive email via IMAP and send via SMTP

  1. Tom

    April 18, 2011 at 8:55 AM

    Thanks, esp. the gnupg way of storing passwords is a good solution :-)

  2. David

    November 13, 2012 at 5:52 PM

    This isn’t very secure. You’re using a standard ‘rm’, which will leave the unencrypted contents still on your drive. Instead of

    gpg -o ~/.secret/.tmp -d ~/.secret/.passwd.gpg
    awk ‘/GMail:/ {print $2}’ ~/.secret/.tmp

    have gpg send the output to stdout and pipe that into awk.
    Also your initial setup of the passwords isn’t secure for the same reason. Doesn’t gpg have an option to securely delete input files?

    • kousik

      November 14, 2012 at 2:11 AM

      Well, I don’t have much experience with GPG or encryption for that matter. That being said, it looks like GPG doesn’t have an option to delete the unencrypted files.

      It’s also my understanding, that using rm to delete from an ext3 filesystem is pretty safe IF the rm command is successful (other FSs are not that secure). I’ll be glad if you know of any alternatives.

      Of course, your idea of piping in the password from STDOUT to awk can deal with one of the two problems. Thanks!

  3. Kepi

    December 30, 2012 at 4:28 PM

    Agree with David, it is better not to use temporary files. And it is also more clean as you don’t need temporary mutt variables:

    set my_gpass=`gpg -q -o – -d ~/.secret/.mails.gpg | awk ‘/GMail:/ {print $2}’`

    Anyway thanks for nice tip!

    Btw if you want delete file securely, use shred.

    • kousik

      January 1, 2013 at 8:31 PM


  4. Bill Luck

    May 7, 2013 at 8:12 AM

    Why not manually type in your password when mutt asks for it… you only have to do it once per session and it’s probably more secure. Second of all, I see way too many tutorials telling people to chmod 700 files that DON’T NEED TO BE EXECUTED. Not a big deal, but 600 would be the proper permission settings.

    • kousik

      May 9, 2013 at 7:15 PM

      I agree with both of your comments. Yes, entering password in each session is definitely more secure than saving it in a file. But the reason I chose this method is to mimic the behavior of a “typical” email client (say, thunderbird). And yes, ‘read permission for the user (600) for .muttrc is all that matters. Thanks!

  5. felipe1982

    June 3, 2014 at 8:17 PM

    use `shred -u` instead of `rm`. This will securely delete/erase files


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 34 other followers

%d bloggers like this: