Victor is a full stack software engineer who loves travelling and building things. Most recently created Ewolo, a cross-platform workout logger.
Using gmail with mutt

I recently switched to using mutt for email and while setting up mutt to use imap is pretty straightforward, this tutorial will also document some advanced concepts such as encrypting your account password and sending emails from a different From address.

This tutorial assumes that you have some familiarity with using mutt and have installed it with sidebar support (sudo apt-get install mutt-patched for the ubuntu folks) and are comfortable with editing your muttrc.

If you would just like to skip to the end, my mutt configuration file can be found here.

Initial configuration

The first order of business is to enable IMAP support in your gmail account settings. Then, the following lines are required in muttrc.

set imap_user           = "you@gmail.com"
set imap_keepalive      = 30
unset imap_passive                             # allow mutt to open new imap connection automatically
set folder              = "imaps://imap.gmail.com"
set spoolfile           = "+[Gmail]/All Mail"
set postponed           = "+[Gmail]/Drafts"
set header_cache        = ~/.mutt/victorparmar/headers
set message_cachedir    = ~/.mutt/victorparmar/bodies
set certificate_file    = ~/.mutt/certificates
set smtp_url            = "smtp://victorparmar@smtp.gmail.com:587/"
set move                = no

Also, don't forget to actually create the cache directories and the certificate file:

mkdir -p ~/.mutt/victorparmar/headers
mkdir -p ~/.mutt/victorparmar/bodies
touch ~/.mutt/certificates

This should be enough to get going to use mutt with gmail but we would also like to have a consistent view of the mailbox if we ever were to switch back to the web version. To do this we need to add certain macros such as moving email to Trash on delete and allowing reporting spam, etc.

macro index,pager I  <set-flag>O  "Mark as read"
macro index,pager U  <clear-flag>O  "Mark as unread"
macro index,pager ga "<change-folder>=[Gmail]/All Mail<enter>"  "Go to all mail"
macro index,pager gs <change-folder>=[Gmail]/Starred<enter> "Go to 'Starred'"
macro index,pager gd <change-folder>=[Gmail]/Drafts<enter>  "Go to 'Drafts'"
macro index,pager gl <change-folder>?       "Go to 'Label'"
macro index,pager gi <change-folder>=INBOX<enter>     "Go to inbox"
macro index,pager gt "<change-folder>=[Gmail]/Sent Mail<enter>" "Go to 'Sent Mail'"
macro index,pager d "<save-message>=[Gmail]/Trash<enter>" "Trash"
macro index,pager +  <save-message>=[Gmail]/Important<enter><enter> "Mark as important"
macro index,pager !  <save-message>=[Gmail]/Spam<enter><enter> "Report spam"
macro index,pager e  "<save-message>=[All]/Gmail Mail<enter><enter>" "Archive conversation"

folder-hook +INBOX 'macro index,pager y "<save-message>=[Gmail]/All Mail<enter><enter>" "Archive conversation"'
folder-hook +[Gmail]/Trash macro index,pager y <save-message>=INBOX<enter><enter> "Move to inbox"
folder-hook +[Gmail]/Starred bind  index,pager y flag-message #"Toggle star"

Note that in the above setup, the default view is: All Mail. Change the spoolfile variable to +INBOX to have the inbox be the default view on login. Also, if you'd like to have some gmail style shortcuts, you can add them in as well:

bind  editor <space> noop
bind  index,pager c  mail                      # compose
bind  generic     x  tag-entry                 # Select Conversation
bind  index       x  tag-thread                # Select Conversation
bind  pager       x  tag-message               # Select Conversation
bind  index,pager s  flag-message              # Star a message

bind  index,pager a  group-reply               # Reply all
bind  index,pager \# delete-thread             # Delete
bind  index,pager l  copy-message              # Label
bind  index,pager v  save-message              # Move to

Finally, since we will be making a connection everytime we want to check for new mail, it is good to have some sensible network timeouts:

set timeout             = 60                   # idle time before scanning
set mail_check          = 60                   # minimum time between scans
Encrypting your account password

Rather than have your gmail account password as plaintext in your muttrc you can encrypt it using gpg. To do so first create a public/private key pair: gpg --gen-key. This can take as long as 5 minutes and you would need to keep moving your mouse or continue on with your work until it is done.

Then, create a passwords file (~/.mutt/passwords) with the following contents:

set imap_pass="password"
set smtp_pass="password"

Encrypt the passwords file and delete the original:

# the following creates a /home/user/.mutt/passwords.gpg file using a passphrase
gpg -r your.email@example.com -c ~/.mutt/passwords
shred ~/.mutt/passwords
rm ~/.mutt/passwords

Finally, add the following command at the start of your muttrc which will then prompt your for your passphrase before firing up mutt:

source "gpg -d ~/.mutt/passwords.gpg |"
Send mail from another address using gmail's smtp

To enable sending of email from a different email address, we will need to use the web to log into gmail and head to gmail settings -> Accounts and Import -> Add another email address you own. Follow the prompts and enter in your desired name and email address. On clicking next you will need to provide the smtp configuration for the domain that your email is setup on. Fill in smtp.gmail.com for the SMTP server and 587 for the port. Enter your username and password and click the Add Account button.

At this point, given that your username and password is correct you will most likely get a "web login required" error. If you're sure your password is correct, you can try these troubleshooting tips:

  • Allow less secure apps access to your account: https://www.google.com/settings/security/lesssecureapps. This should be enough in most cases.
  • Head to the myaccounts security page and review suspicious activity and mark login attempts from google's servers as yours (in this case, the attempt was at the time of trying to add the account and the activity originated in the USA).
  • If you're still having problems, visit http://www.google.com/accounts/DisplayUnlockCaptcha and sign in with your Gmail username and password. If necessary, enter the letters in the distorted picture.
  • Make sure your mail app isn't set to check for new email too frequently. If your mail app checks for new messages more than once every 10 minutes, the app's access to your account could be blocked.

After trying the above, you can try the "add another email address you own" option again and voila, you should be able to respond via a spanking new email address in gmail! For maximum juice you can setup auto-forwarding from the other address to gmail and you can enjoy using gmail's features with your custom email address :)

Final mutt configuration

My final muttrc configuration file can be found here. Happy emailing!