27

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?

yi.tang.uni
  • 1,007
  • 8
  • 20

4 Answers4

17

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

~/Mail
|
+---- 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)))
              account-vars)
      (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
  '(("private"
     (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"))
    ("work"
     (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)))
              my-mu4e-account-alist))

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 05 '19 at 08:20
10

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")
                    ...))
         ,(make-mu4e-context
           :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")
                   ...))))
Magnus
  • 259
  • 2
  • 5
  • 2
    I'm having trouble getting this example working. Per [the docs](http://www.djcbsoftware.nl/code/mu/mu4e/Contexts-example.html) 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](https://www.reddit.com/r/emacs/comments/4jqyzu/help_with_mu4e_multiple_accounts/) 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
  • 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 Jun 29 '20 at 16:34
  • 1
    See here for the "solution": https://github.com/djcb/mu/issues/1087 – salotz Jun 29 '20 at 16:57
4

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 ()
  (interactive)
  (setenv "MAILDIR" "/Users/foo/Maildir/gmail")
  (setenv "MU_HOME" "/Users/foo/.mu/gmail")
  (setq mu4e-maildir "/Users/foo/Maildir/gmail")
  (setq user-mail-address "...")
  (mu4e-quit))
3

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"
  (interactive)
  (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
  (mu4e)
  )

;; 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
Dalker
  • 131
  • 3