I am unable to set the contexts right in mu4e
. I have a default account set with:
(setq mu4e-sent-folder "/Account1/Sent Items"
mu4e-drafts-folder "/Account1/Drafts"
mu4e-trash-folder "/Account1/Deleted Items"
user-mail-address "me@domain.com"
;; ...
)
I set the other accounts from the manual on accounts:
(defvar my-mu4e-account-alist
'(
("Account1"
(mu4e-sent-folder "/Account1/Sent Items")
(mu4e-drafts-folder "/Account1/Drafts")
(mu4e-trash-folder "/Account1/Deleted Items")
(user-mail-address "me@domain.com")
;; ...
)
("Account2"
(mu4e-sent-folder "/Account2/Sent Items")
(mu4e-drafts-folder "/Account2/Drafts")
(mu4e-trash-folder "/Account2/Deleted Items")
(user-mail-address "me@domain2.com")
;; ...
)
))
I also set contexts from the contexts part of the manual:
(setq mu4e-contexts
`( ,(make-mu4e-context
:name "Account1"
:match-func (lambda (msg) (when msg
(string-prefix-p "/Account1" (mu4e-message-field msg :maildir))))
:vars '(
(mu4e-trash-folder . "/Account1/Deleted Items")
(mu4e-refile-folder . "/Account1/Deleted Items")
))
,(make-mu4e-context
:name "Account2"
:match-func (lambda (msg) (when msg
(string-prefix-p "/Account2" (mu4e-message-field msg :maildir))))
:vars '(
(mu4e-trash-folder . "/Account2/Deleted Items")
(mu4e-refile-folder . "/Account2/Deleted Items")
))
))
And I skip asking about a context:
(setq mu4e-compose-context-policy nil)
I go to the mu4e
page with M-x mu4e
and switch context to Account2
, so I see [Account2]
above the mini-buffer. When I start a new message with C-x m
, it uses the email address from Account1
and is saved to /Account1/Drafts
, even though [Account2]
is still showing above the mini-buffer.
One workaround is this function from the multiple accounts part of the manual:
;; Ask the right question about contexts.
(defun my-mu4e-set-account ()
"Set the account for composing a message.
This function is taken from:
https://www.djcbsoftware.nl/code/mu/mu4e/Multiple-accounts.html"
(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"))))
(add-hook 'mu4e-compose-pre-hook 'my-mu4e-set-account)
This works and selects the right context (email address, SMTP server, folders, etc), however I am unable to draft emails programmatically because (mu4e-context-switch t "Account2")
has no effect (see How to draft email programmatically?).
What is wrong with my mu4e
setup that switching contexts with ;
has no effect?
Update: After the comment on this Github issue, I confirm that I get the same results after setting (setq mail-user-agent 'mu4e-user-agent)
and that I don't use user-email-address
. I do use user-mail-address
as in the code above.
I am using mu4e-mu-version 1.4.10
, Emacs 26.3 (9.0)
, and macOS 10.14.6
.