Mutt — multiaccount support

mutt

Can anybody help me with mutt configuration?

I need to configure mutt to support mutiple accounts the same time.

At this moment my config file for personal email looks like this:

~ $ cat ~/.mutt/personal

set imap_user = «%personal_email%@gmail.com»
set imap_pass = «%personal_email_pass%»
set smtp_url = «smtps://%personal_email%@smtp.gmail.com:465/»
set smtp_pass = «%personal_email_pass%»
set from = «%personal_email%»
set realname = «%username%»
set hostname = gmail.com
set folder = «imaps://%personal_email%@imap.gmail.com:993»
set spoolfile = «imaps://%personal_email%@imap.gmail.com/INBOX»
set postponed = «imaps://%personal_email%@imap.gmail.com/[Gmail]/Drafts»
set record = «imaps://%personal_email%@imap.gmail.com/[Gmail]/Sent Mail»

"~/.mutt/work" looks the same way, except the values of "%personal_email%" and "%personal_email_pass%", of course.

"~/.muttrc" looks like this:

~ $ cat ~/.muttrc

source «~/.mutt/personal»

macro index <f2> '<sync-mailbox><enter-command>source ~/.mutt/personal<enter><change-folder>!<enter>'
macro index <f3> '<sync-mailbox><enter-command>source ~/.mutt/work<enter><change-folder>!<enter>'

When I press F2 or F3 mutt really changes an account, but when I'm trying to change mailbox (via 'c' -> '?') it opens a mailbox of the first account (in my case "personal") even when the current account is "work".

Why? What am I doing wrong? How can I fix it?

Best Answer

Some trickery with hooks. You might want to think twice about storing passwords into .muttrc. At least take care to use application specific passwords instead of your main Google account password when you do something as insecure as this.

# Gmail account passwords and usernames
set my_gmail_user1 = "<account 1, without @gmail or anything>"
set my_gmail_user2 = "<account 2, without @gmail or anything>"
set my_gmail_pass1 = "<password, hopefully an app-specific one>"  
set my_gmail_pass2 = "<password, hopefully an app-specific one>"  

# Account hooks
account-hook . "unset imap_user ; unset imap_pass"
account-hook "imaps://$my_gmail_user1@imap.gmail.com" "\
    set imap_user = $my_gmail_user1 \
            imap_pass = $my_gmail_pass1"
account-hook "imaps://$my_gmail_user2@imap.gmail.com" "\
    set imap_user = $my_gmail_user2 \
            imap_pass = $my_gmail_pass2"

# Gmail folders
set folder          = imaps://$my_gmail_user1@imap.gmail.com/
mailboxes           = +INBOX =[Gmail]/Drafts =[Gmail]/'Sent Mail' =[Gmail]/Spam =[Gmail]/Trash
set spoolfile       = +INBOX
folder-hook         imaps://$my_gmail_user1@imap.gmail.com/ "\
    set folder      = imaps://$my_gmail_user1@imap.gmail.com/ \
        spoolfile   = +INBOX \
        postponed   = +[Gmail]/Drafts \
        record      = +[Gmail]/'Sent Mail' \
        from        = 'First User Real Name <$my_gmail_user1@gmail.com> ' \
        realname    = 'First User Real Name' \
        smtp_url    = smtps://$my_gmail_user1@smtp.gmail.com \
        smtp_pass   = $my_gmail_pass1"
set folder          = imaps://$my_gmail_user2@imap.gmail.com/
mailboxes           = +INBOX =[Gmail]/Drafts =[Gmail]/'Sent Mail' =[Gmail]/Spam =[Gmail]/Trash
set spoolfile       = +INBOX
folder-hook         imaps://$my_gmail_user2@imap.gmail.com/ "\
    set folder      = imaps://$my_gmail_user2@imap.gmail.com/ \
        spoolfile   = +INBOX \
        postponed   = +[Gmail]/Drafts \
        record      = +[Gmail]/'Sent Mail' \
        from        = 'First User Real Name <$my_gmail_user2@gmail.com> ' \
        realname    = 'First User Real Name' \
        smtp_url    = smtps://$my_gmail_user2@smtp.gmail.com \
        smtp_pass   = $my_gmail_pass2"

Now all there is left to it is to fire up mutt, then hit y and select the folder for an account you want.

If you want more secure setup, your favorite search engine can tell you how to secure IMAP/POP passwords for Mutt with PGP. Realname/from is left as an exercise for the reader.

Related Question