I am using mu4e for email in Emacs, and it is currently only configured for one mail account, and I set key to view different inbox. I wonder how to use mu4e for managing multiple email accounts?

4 Answers 4


I'm using mu4e with two accounts. Each account has its own maildir:

+---- work
`---- private

The manual comes with an example function to choose an account:

(defun my-mu4e-set-account ()
  "Set the account for composing a message."
  (let* ((account
          (if mu4e-compose-parent-message
              (let ((maildir (mu4e-message-field mu4e-compose-parent-message :maildir)))
                (string-match "/\\(.*?\\)/" maildir)
                (match-string 1 maildir))
            (completing-read (format "Compose with account: (%s) "
                                     (mapconcat #'(lambda (var) (car var))
                                                my-mu4e-account-alist "/"))
                             (mapcar #'(lambda (var) (car var)) my-mu4e-account-alist)
                             nil t nil nil (caar my-mu4e-account-alist))))
         (account-vars (cdr (assoc account my-mu4e-account-alist))))
    (if account-vars
        (mapc #'(lambda (var)
                  (set (car var) (cadr var)))
      (error "No email account found"))))

;; ask for account when composing mail
(add-hook 'mu4e-compose-pre-hook 'my-mu4e-set-account)

For that to work you will also need the my-mu4e-account-alist:

(defvar my-mu4e-account-alist
     (user-mail-address  "[email protected]")
     (user-full-name     "My Name")
     (mu4e-sent-folder   "/private/Sent Items")
     (mu4e-drafts-folder "/private/Drafts")
     (mu4e-trash-folder  "/private/Deleted Items")
     (mu4e-refile-folder "/private/Archive"))
     (user-mail-address  "[email protected]")
     (mu4e-sent-folder   "/work/Sent Items")
     (mu4e-drafts-folder "/work/Drafts")
     (mu4e-trash-folder  "/work/Deleted Items")
     (mu4e-refile-folder "/work/Archives"))))

(setq mu4e-user-mail-address-list
      (mapcar (lambda (account) (cadr (assq 'user-mail-address account)))

You can simply switch between mail folders with j, as long as all of your maildirs are subdirectories of mu4e-maildir.

I'm actually using a more elaborate function for mu4e-trash-folder and mu4e-refile-folder to avoid moving emails from one maildir over to another, but the information above should be sufficient to use multiple accounts.

  • I'm struggling to comprehend how mu4e knows which inbox to show. There's no mu4e-inbox-folder variable, and when I press ji I get incoming mail from all of my accounts, not just the work or private context.
    – AstroFloyd
    Commented May 5, 2019 at 8:20

I'm using two different accounts that I want to mix as little as possible.

offlineimap is configured with two accounts, one delivering mail into ~/.maildir-work the other one into ~/.maildir-home.

My configuration for mu4e uses the new context mechanism:

(setq mu4e-contexts
      `( ,(make-mu4e-context
           :name "home"
           :match-func (lambda (_) (string-equal "home" (mu4e-context-name mu4e~context-current)))
           :enter-func '()
           :leave-func (lambda () (mu4e-clear-caches))
           :vars '((mu4e-maildir . "~/.maildir-home")
                   (mu4e-mu-home . "~/.mu-home")
                   (mu4e-get-mail-command . "offlineimap -a Home")
           :name "work"
           :match-func (lambda (_) (string-equal "work" (mu4e-context-name mu4e~context-current)))
           :enter-func '()
           :leave-func (lambda () (mu4e-clear-caches))
           :vars '((mu4e-maildir . "~/.maildir-work")
                   (mu4e-mu-home . "~/.mu-work")
                   (mu4e-get-mail-command . "offlineimap -a Work")
  • 2
    I'm having trouble getting this example working. Per the docs the mu4e-maildir and mu4e-mu-home variables aren't supposed to be changeable without quitting mu4e first. I posted a bit more detail here on my set up.
    – kostajh
    Commented May 19, 2016 at 13:39
  • Indeed, I've had to resort to restarting Emacs when switching. For me that's still worth it.
    – Magnus
    Commented May 19, 2016 at 16:12
  • 1
    This works but in the mu4e-context-switch menu if more than one context start with the same letter you won't be able to choose them.
    – salotz
    Commented Jun 29, 2020 at 16:34
  • 1
    See here for the "solution": github.com/djcb/mu/issues/1087
    – salotz
    Commented Jun 29, 2020 at 16:57

I'm using a setup very similar to Magnus', and just wanted to add that (mu4e-quit) exists, and it works fine for cleaning up accounts without restarting. Actual mechanism:

(defun mail-gmail ()
  (setenv "MAILDIR" "/Users/foo/Maildir/gmail")
  (setenv "MU_HOME" "/Users/foo/.mu/gmail")
  (setq mu4e-maildir "/Users/foo/Maildir/gmail")
  (setq user-mail-address "...")

After reading all previous answers in this thread and tweaking around, I came up with the following solution, that assumes separate contexts have been defined, which include a redefinition of mu4e-maildir and mu4e-mu-home. As stated in comments to older answers, it is necessary to quit and restart mu4e when these two variables are redefined. The trick is to replace the default behaviour of ; in order to accomplish this automatically when changing context (it will still ask for confirmation for the "quit" step):

(defun dak-switch-mail-account ()       
  "quit and reload mu4e to properly change context, i.e. account"
  (mu4e-context-switch)  ; will ask for new context
  (mu4e-quit)  ; necessary for new context to change maildir and mu-home
  (sit-for .5)  ; mu4e-quit needs a bit of time before mu4e can be properly restarted

;; rebinding of context switch key:
(define-key mu4e-main-mode-map (kbd ";") 'dak-switch-mail-account)
(define-key mu4e-headers-mode-map (kbd ";") 'dak-switch-mail-account)
;; and possibly on other mu4e modes as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.