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?


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  "private@domain.net")
     (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  "work@domain.net")
     (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 May 5 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")
  • 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 May 19 '16 at 13:39
  • Indeed, I've had to resort to restarting Emacs when switching. For me that's still worth it. – Magnus May 19 '16 at 16:12

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 "...")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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