0

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.

miguelmorin
  • 1,751
  • 11
  • 33
  • I'd start changing `mu4e-compose-context-policy` to `'pick-first` and disabling `my-mu4e-set-account` which at first sight seems to be overriding your context settings in the pre-hook. – Muihlinn Aug 06 '20 at 11:26
  • With `(setq mu4e-compose-context-policy 'pick-first) (setq mu4e-compose-pre-hook nil)`, a draft email starts with `Account1` (of course). With `(setq mu4e-compose-context-policy 'ask) (setq mu4e-compose-pre-hook nil)`, a draft email prompts me for the context, I choose `Account2`, and it still uses the email address and saves it on `Account1`, even though the context shows as `[Account2]`. – miguelmorin Aug 06 '20 at 11:37
  • 2
    There's a discussion [here](https://github.com/djcb/mu/issues/1688) on a similar issue. – jagrg Aug 06 '20 at 11:39
  • @jagrg It seems the same issue indeed. The fixes suggested there don't work for me and I updated the question. – miguelmorin Aug 08 '20 at 10:18

0 Answers0