Adding a Gmail account to your Mutt

Adding a Gmail IMAP inbox along-side your local mailbox can be useful to have easy access to both personal and work emails. It took a bit of fiddling around, but the recipe below worked for me.

In your standard .muttrc, at the top, just add these two lines:

unset folder
unset smtp_url

unset use_from

This ensures your personal setup does not try to contact Gmail after you have used your work profile.

Create a config for your work setup in .mutt/work.rc. Copy your standard .muttrc over, to get the same setup you are used to, then add the lines below at the top.

set imap_user = "your@work.com"
set imap_pass = "password"
set smtp_url = "smtp://your@work.com@smtp.gmail.com:587/"
set smtp_pass = "password"
set use_from=yes
set from = "your@work.com"
set realname = "Your Name"
set folder="imaps://imap.gmail.com:993"

set record="~/mail/other-sent-mail"
set postponed="=Drafts"
set spoolfile="+INBOX"
set signature="~/.mutt/signature.work"

bind index G imap-fetch-mail

The bind statement allows you to refresh the IMAP mailbox by hitting ‘G’. In your standard .muttrc, set your personal signature file:

set signature="~/.mutt/signature.personal"

Now, in both files, set folders and macros to switch between them.

# Folder hooks
mbox-hook 'mailbox' 'source ~/.muttrc'
folder-hook 'imaps://imap.gmail.com:993' 'source ~/.mutt/work.rc'
macro index <f2> '<sync-mailbox><enter-command>source ~/.mutt.rc<enter><change-folder>/var/mail/<name><enter>'
macro index <f3> '<sync-mailbox><enter-command>source ~/.mutt/work.rc<enter><change-folder>imaps://imap.gmail.com:993<enter>'

To be honest, I’m not 100% sure on the mbox-hook line, but nothing is broken, so I have left it like that. With the above lines, you can now hit <f2> and <f3> to switch between mail boxes.

Remember, with Gmail, you both need to enable IMAP and setup 2-factor authentication, or it won’t work. Then you need to “Add app password” to get the password to use in your work.rc configuration.

Have fun!

This entry was posted in Software and tagged , , , , . Bookmark the permalink.

One Response to Adding a Gmail account to your Mutt

  1. morsing says:

    Just adding a quick note as sending mail broke after a week with the message “No Authenticators Available”.

    I am running this on MacOS, don’t know if it is a specific MacOS issue, but adding the following to .muttrc fixed it:

    set smtp_authenticators = ‘gssapi:login’

    I am on Catalina.

Leave a Reply