24

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?

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.

| improve this answer | |
  • 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 '19 at 8:20
9

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")
                   ...))))
| improve this answer | |
  • 1
    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
  • 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 at 16:34
  • See here for the "solution": github.com/djcb/mu/issues/1087 – salotz Jun 29 at 16:57
3

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))
| improve this answer | |
2

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
| improve this answer | |

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.