Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Last active November 9, 2024 16:50
Show Gist options
  • Save miguelmota/9456162 to your computer and use it in GitHub Desktop.
Save miguelmota/9456162 to your computer and use it in GitHub Desktop.
Multiple accounts with Mutt E-Mail Client (gmail example)

How to set up multiple accounts with Mutt E-mail Client

Thanks to this article by Christoph Berg

Instructions

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 <f2> '<sync-mailbox><enter-command>source ~/.mutt/account.com.gmail.foo<enter><change-folder>!<enter>'
macro index <f3> '<sync-mailbox><enter-command>source ~/.mutt/account.com.gmail.bar<enter><change-folder>!<enter>'

# Fetch mail shortcut
bind index G imap-fetch-mail
~/.mutt/account.com.gmail.foo
set imap_user = "foo@gmail.com"                       
set imap_pass = "<PASSWORD>"                                    
set smtp_url = "smtp://foo@smtp.gmail.com:587/"       
set smtp_pass = "<PASSWORD>"                                    
set from = "foo@gmail.com"                            
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 = "bar@gmail.com"                       
set imap_pass = "<PASSWORD>"                                    
set smtp_url = "smtp://bar@smtp.gmail.com:587/"       
set smtp_pass = "<PASSWORD>"                                    
set from = "bar@gmail.com"                            
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         
@adriensck
Copy link

Thanks! Really helpful! 👍

@gotbletu
Copy link

Thanks this was useful but i was confused by the structure

Think you meant
account = text files
com = folders

~/
|── .mutt/
|   ├── account.com.gmail.foo
|   ├── account.com.gmail.bar
|   ├── com.gmail.foo/
|   └── com.gmail.bar/
└── .muttrc

@blacksails
Copy link

Isen't it redundant having folder hooks doing the same thing as the macros?
I don't think you should source the files in the macros, isen't that what the folder hooks are for?

@srikawnth
Copy link

Hi, I am using mutt sidebar patch. When i switch account, I could still see old account labels inside this account. How could i refresh the sidebar with the active account labels?

@zergler
Copy link

zergler commented Jun 28, 2015

To refresh the sidebar, you have to put this in all of your account files and change the mailboxes command to include all the mailboxes you want to show up. But I think there is a bug in the unmailboxes * command which I'm not sure has been fixed yet.

unmailboxes *
mailboxes "+Inbox" "+[Gmail]/All Mail"

@bruno-
Copy link

bruno- commented Aug 14, 2015

@zergler thanks for the unmailboxes * tip. That should be the solution, but unfortunately there's a bug and it doesn't really work. If I invoke that command manually I get a segmentation fault with mutt :(

@shioyama
Copy link

shioyama commented Dec 7, 2015

If anybody is struggling with this segfault issue, it's a known issue with the sidebar and the unmailboxes command but I've found that it can be fixed by using refresh before unmailboxes.

I have some source files for each mailbox which start with unmailboxes * to clear the sidebar. My macros look like this:

# Macros for switching accounts
macro index <f2> '<sync-mailbox><refresh><enter-command>source ~/.mutt/gmail<enter><change-folder>!<enter>'
macro index <f3> '<sync-mailbox><refresh><enter-command>source ~/.mutt/fastmail<enter><change-folder>!<enter>'
...

You see that I added the <refresh> just before sourcing the files with unmailboxes. This has the added benefit of updating sidebar numbers so that they are correct for all mailboxes when you switch, and I find now that I don't get any more segfaults or issues. But, YMMV.

@cambrant
Copy link

@shioyama That sounded exactly like it would solve my problem, where unmailboxes * before setting mailboxes again would result in me having only +Inbox in my sidebar after switching accounts, but instead I just get a couple of weird characters in the sidebar. Are you running 1.5.24?

I'll have a look at the patch next and try to fix the issue, but I'm sure it's a bit more complicated than it seems now.

@cambrant
Copy link

Everything seems to be working fine for me when I use mutt-lightweight-sidebar. Not sure if all functionality is there, but I can unmailboxes * just fine.

@Leandros
Copy link

What version are you using @cambrant ?
I've switched to that patch, and I've got the same issues as with the old patch, my sidebar looks totally scrambled after switching.

@andreas-bulling
Copy link

same issue here - I also only get scrambled characters in the sidebar after switching accounts (I am running 1.6.0)

Any update on this? Does anybody know a fix?

@oblitum
Copy link

oblitum commented Sep 26, 2016

Instead of getting stuck with bugs as everyone is talking about I've instead opted to just load multiple accounts in specific tmux windows. I'm using tmuxp to load them all easily, with just a command (tmuxp load mail) I load all my accounts in a new "mail" tmux session, with each account in its own tmux window running its own mutt instance.

@stormdragon2976
Copy link

I wonder if anyone here would be interested in helping with https://github.com/2mb-solutions/fleacollar? My idea is to make email configuration with mutt really simple. It handles practically everything from gpg to multiple email address. It's even pretty close to working. It sure would be nice having someone who has a better grasp of folder hooks and things to help out though.

@b0o
Copy link

b0o commented Nov 23, 2017

Thanks for this!
I wanted to configure mutt so that I can commit my main configuration files publicly, while my account-specific configurations remain private. Here's what I've done:

~/.muttrc:

# Folder hooks
folder-hook "$MUTT_ACCOUNT_1" 'source ~/.mutt/$MUTT_ACCOUNT_1'
folder-hook "$MUTT_ACCOUNT_2" 'source ~/.mutt/$MUTT_ACCOUNT_2'

# Default account
source ~/.mutt/$MUTT_ACCOUNT_1

# Macros for switching accounts
macro index <f2> '<sync-mailbox><enter-command>source ~/.mutt/$MUTT_ACCOUNT_1<enter><change-folder>!<enter>'
macro index <f3> '<sync-mailbox><enter-command>source ~/.mutt/$MUTT_ACCOUNT_2<enter><change-folder>!<enter>'

# Fetch mail shortcut
bind index G imap-fetch-mail

~/.private/mutt

#!/bin/bash
export MUTT_ACCOUNT_1="account.com.gmail.foo"
export MUTT_ACCOUNT_2="account.com.gmail.bar"

~/bin/mutt

#!/bin/bash
source $DOTFILES/private/mutt
/usr/bin/mutt

~/.zshrc

alias mutt $HOME/bin/mutt

Now I can keep my .muttrc in my dotfiles repo without worrying about my email addresses or passwords leaking, as long as I keep my ~/.mutt and ~/.private directories private.

@1094
Copy link

1094 commented Feb 13, 2018

Hi. I was wondering if you'd be able to help. I am using the sidebar in mutt and have marcos set up to switch accounts. What I would like to do is remove the sidebar and have all emails from all accounts showing in the default index. Do you know how I would do that? I tried to google it, but everything leads to a set-up much like this.

@TravelTrader
Copy link

I do not use gmail but I had mutt already running.
I changed my files with your script and added one more account and --- it worked immediately!

I added $my_server to not repeat this and to easier add more accounts by simply use cp ...
Thanks.

--- I will implement pwd-protection soon ---

@erji44
Copy link

erji44 commented Mar 6, 2018

I have four accounts set up the way described above.
Two of the accounts are gmail and it works fine whereas the other account is on another server.
Problem I'm having is the gmail accounts work fine when the default account is set to gmail.
When I try and change to the accounts on the other server I get login failed.
Same thing a happens if I try and change the default account as one of the other two accounts, the other accounts work and when I try change to gmail I get again login failed.
Individually the accounts work fine but seems like change of server causes a problem.

Any thoughts?
Thank you

@tuannvm
Copy link

tuannvm commented Jun 18, 2018

@erji44

You're right, when we change account, we do need to change folder as well. For example I have 4th account using yandex, here is the necessary config:

macro index <f4> '<sync-mailbox><enter-command>source ~/.mutt/account.com.yandex.bar<enter><change-folder>imaps://imap.yandex.com<enter>'

@dmzely
Copy link

dmzely commented Oct 21, 2018

Thanks!!!

@doa379
Copy link

doa379 commented Feb 13, 2019

oh mutt still sucks

@mcepl
Copy link

mcepl commented May 2, 2019

How can I copy/move messages between two IMAP accounts? Is there a way how to make some kind of aliases, so that I don't have to copy to imaps://matej#foo@imap.somewhere.company.com/INBOX/somefolder all the time?

@minusf
Copy link

minusf commented May 5, 2019

the problem with these setups is that if any of the imap accounts are not reachable mutt will go into an infinite loop and it's not possible to even switch to any of the other ones. mutt just wasn't built for this.

@amiralshamiah
Copy link

how can i convert a mutt-e-mail into postgresql database

@vladimirtamayo
Copy link

In neomutt I had to change imap folder parameter set folder = "imaps://$imap_user@imap.gmail.com:993"

@chekmanh
Copy link

Thank you very much

@hasufell
Copy link

Doesn't work in mutt, only in neomutt. Mutt hangs forever on sorting emails when switching accounts.

@talau
Copy link

talau commented May 14, 2021

Thanks for the article. Here you can find another way to use multiple mutt accounts.

@carlcidromero
Copy link

@IanDarwin
Copy link

how can i convert a mutt-e-mail into postgresql database

Look into archiveopteryx, a back-end for storing mail in postgresql.

@jbjoeB
Copy link

jbjoeB commented Nov 10, 2022

To refresh the sidebar, you have to put this in all of your account files and change the mailboxes command to include all the mailboxes you want to show up. But I think there is a bug in the unmailboxes * command which I'm not sure has been fixed yet.

unmailboxes * mailboxes "+Inbox" "+[Gmail]/All Mail"

This resolved my issue after a couple of days looking for an answer. I even asked on irc and they were stumped.

it's 12:27pm PST 10/10/22

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment