How to manage multiple imap accounts with mutt
I love mutt, and the mutt version shipped with Ubuntu is awesome (it even has a patched version), the only caveat I've found so far it's that I had not been able to manage several imap accounts at the same time.
Currently my mutt setup looks like this:
#$HOME/.mutt/muttrc
=== Accounts ====
set my_decrypt=`gpg -o $HOME/.mutt/accounts/passwd.gpg.tmp -d $HOME/.mutt/accounts/passwd.gpg`
set my_first_mail_passwd=`awk '/[email protected]:/ {print $2}' $HOME/.mutt/accounts/passwd.gpg.tmp`
set my_second_mail_passwd=`awk '/[email protected]:/ {print $2}' $HOME/.mutt/accounts/passwd.gpg.tmp`
set my_rm=`rm -f $HOME/.mutt/accounts/passwd.gpg.tmp`
source $HOME/.mutt/accounts/[email protected]/[email protected]
//source $HOME/.mutt/accounts/[email protected]/[email protected]
Which means that a password.gpg file is decrypted on the fly (where my email passwords live) use it to set some vars and deleted, also a default profile is sourced, it looks like this:
#$HOME/.mutt/accounts/[email protected]/[email protected]
set folder = "imaps://imap.foo.com"
set spoolfile = "imaps://imap.foo.com/INBOX"
set header_cache = "$HOME/.mutt/cache/[email protected]"
set message_cachedir = "$HOME/.mutt/cache/[email protected]"
set imap_user = "[email protected]"
set imap_pass = $my_first_mail_passwd
set smtp_url = "smtp://[email protected]"
set smtp_pass = $my_first_mail_passwd
Using this, I'm only able to use the account configured in the default profile.., I've heard that folder hooks can be used but I've no idea how to use them, and I'm also not sure if that's what I'm looking for, I'd like to just be able to source different profiles depending which email I'd like to read.., Any suggestion method is welcome.., I'd prefer a setup who don't rely on external programs who aren't part of a default Ubuntu installation.
Solution 1:
Even though there are some useful guides on this site and this one, I thought I would just show you my currently working solution. You might note that it is similar to the aforementioned guides, but with additions and differences: I have found it is possible not to use account-hooks
or folder-hooks
in some cases. See the Mutt wiki for an alternative take on setting this up.
I will post the elements of the setup and then post my entire .muttrc
so you can see how it all fits together.
It is easiest to enter the settings of both of your accounts in separate files and save them in the ~/.mutt
folder. As an example, your two accounts could be called account.gmail1
and account.gmail2
. So create two text files with those names and follow this template below for the contents; they should be both identical apart from the user@
part; just replace all occurrences of that with your real email (e.g. mik123@
) in both files.
set imap_user = "[email protected]"
set imap_pass = ""
set from = "[email protected]"
set realname = "user"
set hostname = gmail.com
set folder = "imaps://[email protected]:993"
set spoolfile = "imaps://[email protected]/INBOX"
set postponed = "imaps://[email protected]/[Gmail]/Drafts"
set record = "imaps://[email protected]/[Gmail]/Sent Mail"
set smtp_url = "smtp://[email protected]:587"
set smtp_pass = ""
Note that I have left the password blank (
""
), which means you will be prompted for it, and that is what I prefer, but you can integrate your own solution if you don't want to have to keep entering the password.-
If you want a default account loaded at startup, you can use in your
.muttrc
a similar line to this, just specify your own config file.source "~/.mutt/account.gmail1"
-
To actually switch between the accounts, place a shortcut in your
.muttrc
referring to the actual location and name of your account configs:macro index <f4> '<sync-mailbox><enter-command>source ~/.mutt/account.gmail1<enter><change-folder>!<enter>'
macro index <f5> '<sync-mailbox><enter-command>source ~/.mutt/account.gmail2<enter><change-folder>!<enter>'
-
You will be able to switch between folders within both accounts with pager commands such as
macro index,pager ga "<change-folder>=[Gmail]/All Mail<enter>" "Go to all mail"
that are defined in your .muttrc
; these will still work for each account (see my attached .muttrc
).
- I tested these particular settings with the additional general ones in my
.muttrc
, so you can use that file and/or collate it with your own, it is up to you.
Now just load mutt
in terminal and you should be able to load up one account and then switch to the other one using your shortcut. My .muttrc
is pasted here if you need a working example of a configuration.
See the good article on this site on Mutt and the Mutt wiki for further ways of setting up mutt
such as using folder-hooks
to associate setings with various mailboxes, which is also explained here.
Solution 2:
You can use several configuration files. For example, you could run:
-
mutt -F .muttrc-yahoo
on/dev/pts/1
-
mutt -F .muttrc-gmail
on/dev/pts/2
Solution 3:
There's also a good writeup/howto here (GitHub). Might be a little easier to follow, or not.
Directories and files
~/ |── .mutt/ | ├── account.com.gmail.foo | ├── account.com.gmail.bar | ├── com.gmail.foo/ | └── com.gmail.bar/ └── .muttrc
~/.muttrc
# Folder hooks folder-hook 'account.com.gmail.foo' 'source ~/.mutt/account.com.gmail.foo' folder-hook 'account.com.gmail.bar' 'source ~/.mutt/account.com.gmail.bar' # Default account source ~/.mutt/account.com.gmail.foo # Macros for switching accounts macro index 'source ~/.mutt/account.com.gmail.foo!' macro index 'source ~/.mutt/account.com.gmail.bar!' # Fetch mail shortcut bind index G imap-fetch-mail
~/.mutt/account.com.gmail.foo
set imap_user = "[email protected]" set imap_pass = "" set smtp_url = "smtp://[email protected]:587/" set smtp_pass = "" set from = "[email protected]" set realname = "Foo" set folder = "imaps://imap.gmail.com:993" set spoolfile = "+INBOX" set postponed = "+[Gmail]/Drafts" set header_cache = ~/.mutt/com.gmail.foo/cache/headers set message_cachedir = ~/.mutt/com.gmail.foo/cache/bodies set certificate_file = ~/.mutt/com.gmail.foo/certificates
~/.mutt/account.com.gmail.bar
set imap_user = "[email protected]" set imap_pass = "" set smtp_url = "smtp://[email protected]:587/" set smtp_pass = "" set from = "[email protected]" set realname = "Bar" set folder = "imaps://imap.gmail.com:993" set spoolfile = "+INBOX" set postponed = "+[Gmail]/Drafts" set header_cache = ~/.mutt/com.gmail.bar/cache/headers set message_cachedir = ~/.mutt/com.gmail.bar/cache/bodies set certificate_file = ~/.mutt/com.gmail.bar/certificates