[mutt] Multiple email accounts using hooks
This is a continuation of my effort to configure mutt. This post explains how to set up multiple IMAP and SMTP accounts using account-hook and folder-hook. All the configurations go into the file ~/.mutt/muttrc (or ~/.muttrc, if you prefer).
I am assuming that you want to setup mutt for two gmail accounts user1@gmail.com and user2@gmail.com with passwords pass1 and pass2, respectively. I am also assuming that you have saved the passwords in an encrypted file .passwd.gpg in the ~/.secret directory which contains the passwords in the second column as (refer to the previous post):
GMail1: pass1 Gmail2: pass2
as well as the corresponding signatures in user1.sig and user2.sig files in ~/.mutt directory.
With the above setup our ~/.mutt/muttrc now becomes:
#----------------------------------------- # ~/.mutt/muttrc settings for mutt 1.5.20 # Process the password file first #----------------------------------------- set my_tmp=`gpg -o ~/.secret/.tmp -d ~/.secret/.passwd.gpg` set my_pass1=`awk '/GMail1:/ {print $2}' ~/.secret/.tmp` set my_pass2=`awk '/GMail2:/ {print $2}' ~/.secret/.tmp` set my_del=`rm -f ~/.secret/.tmp` # #------------------------------------- # Account Hooks #------------------------------------- account-hook . 'unset imap_userunsetimap_pass' # unset first! account-hook 'imaps://user1@imap.gmail.com/' "\ set imap_user=user1 imap_pass=$my_pass1 " account-hook 'imaps://user2@imap.gmail.com/' "\ set imap_user=user2 imap_pass=$my_pass2 " # #------------------------------------- # Folders, mailboxes and folder hooks #------------------------------------- # Setup for user1: set folder="imaps://user1@imap.gmail.com/" mailboxes =INBOX =[Gmail]/Drafts =[Gmail]/'Sent Mail' =[Gmail]/Spam folder-hook 'imaps://user1@imap.gmail.com' " \ set folder=imaps://user1@imap.gmail.com/ \ postponed=+[Gmail]/Drafts \ record=+[Gmail]/'Sent Mail' \ smtp_url=smtps://user1@smtp.gmail.com \ smtp_pass=$my_pass1 \ signature=~/.mutt/user1.sig \ from='User One <user1@gmail.com> ' \ realname='User One' " # # Setup for user2: set folder="imaps://user2@imap.gmail.com/" mailboxes =INBOX =[Gmail]/Drafts =[Gmail]/'Sent Mail' =[Gmail]/Spam folder-hook 'imaps://user2@imap.gmail.com' " \ set folder=imaps://user2@imap.gmail.com/ \ postponed=+[Gmail]/Drafts \ record=+[Gmail]/'Sent Mail' \ smtp_url=smtps://user2@smtp.gmail.com \ smtp_pass=$my_pass2 \ signature=~/.mutt/user2.sig \ from='User Two <user2@gmail.com> ' \ realname='User Two' " # #------------------------------------- # Macros to make life easier #------------------------------------- macro index <esc>1 "y1<return><return>" # ESC+1 takes to first INBOX macro index <esc>2 "y5<return><return>" # ESC+2 takes to the second # #------------------------------------- # Mail-check preferences #------------------------------------- set timeout=60 #Check for mail every minute set mail_check=5 # # EOF
To configure other nifty options (e.g. color, editor) please refer to the previous post.

Thank you,
you saved some of my time for the initial mutt configuration with mutt’s imap support.
Keep on.
Glad that I could of some help.
I get twelve instances of “unset: unknown variable”
Any idea what this is? I have removed two lines of “signature=…” since I don’t have .sig files.
Probably the spurious ‘unset’ command was being treated as a variable in my post:
account-hook . ‘unset imap_user
unsetimap_pass’Apologies!
Try it with the second ‘unset’ and let us know if it worked.
I meant try it “without” the second ‘unset’!
[...] IMAP, I got it working in part to the nice guides here. In particular, I used this guide and this one. However, the configuration from the second link contains a couple typos that are just wrong (no [...]
Thanks for the rundown. I have refined it a bit. It works perfectly. Of course, you’ll have to define key-bindings for the various folders if you unset imap_check_subscribed. The mailbox screen can be reached by typing c, ?, . Also, it isn’t necessary to set record since Gmail will automatically save sent messages to [GMAIL]/Sent. Although most of the text is wrapped in this reply, said text is actually one line.
account-hook . ‘unset imap_user ; unset imap_pass’
account-hook ‘imaps://user1@imap.gmail.com/’ set imap_pass = password
account-hook ‘imaps://user2@imap.gmail.com/’ set imap_pass = password
unset folder
mailboxes imaps://user1@imap.gmail.com
folder-hook ‘imaps://user1@imap.gmail.com’ “set folder = imaps://user1@imap.gmail.com ; set postponed = +[Gmail]/Drafts ; unset record ; set smtp_url = smtps://user1@smtp.gmail.com:587 ; set smtp_pass = ‘password’ ; set from = ‘User ‘ ; set realname = ‘User’”
mailboxes imaps://user2@imap.gmail.com
folder-hook ‘imaps://user2@imap.gmail.com’ “set folder = imaps://user2@imap.gmail.com ; set postponed = +[Gmail]/Drafts ; unset record ; set smtp_url = smtps://user2@smtp.gmail.com:587 ; set smtp_pass = ‘password’ ; set from = ‘User ‘ ; set realname = ‘User’”
Hey Brian, thanks a lot!
You’re right about the ‘set record’ with GMail. Everything you mentioned should work perfectly. However, I just want to make a quick comment: you can set several variables with a single set command as mentioned in the text, that is,
and
are the same things.
Ooops! Accounts can be reached by typing c, ?, TAB.
Actually there is better way to decrypt passwords on the fly (no temp unencrypted files, whitch can be left on disk)
source command will read stdin instead of file when followed by “|”
This is how I use it:
.secrets file:
set my_pass1=”pass1″
set my_pass2=”pass2″
muttrc:
source “gpg –textmode -d .secrets.gpg |”
Hey, thanks! That was a nice tip!
I think there is a mistake: for “set imap_user” the “=” is missing, right?
thnx,
A.
Yes, you are absolutely right! Thank you!
-Kousik
[...] how to use multiple IMAP accounts in [...]
Okay, but how does this work if I set iceweasel (firefox in debian) to use mutt for mailto links
xterm -e mutt “$1″
can I choose which address I am sending from with the same esc-1, or esc-2 binding? Seem that will only go to the inbox.
Or would mutt give me the option for the From: field as it would for subject, etc.?
I currently have two muttrc files (one .muttrc, and one .guttrc, and have aliased gmx to mutt -F .guttrc, since that is gmx.com mail over imap, the first being for gmail over imap), but I haven’t figure out how to allow myself a choice with mailto links.
I had tried with
xterm -e read -p “gmail (mutt) or gmx (gmx)? ” mailprog;
$mailprog “$1″
but the xterm dies as soon as I enter my choice.
If I can use one muttrc for both accounts, as indicated here, I’m assuming this would simplify that.
It’s a good feature to have. But I don’t know how it can be acheived. Please let me know if you found a solution. Thanks!
Hi,
I had to replace:
macro index 1 “y1″ # ESC+1 takes to first INBOX
by:
macro index 1 “c?1″
Mutt doesn’t seem to know the “y” shortcut (mutt 1.5.21)