Mu4e: More contextual +mu4e-set-from-address-h

This hook should only be used when users define email aliases, or no
contexts are defined (in which case the list of personal addresses
should be used). Otherwise, the `:match-func` contexts feature is
sufficient.
This commit is contained in:
Liam Hupfer 2021-07-10 13:47:44 -05:00 committed by TEC
parent a75dea6281
commit 2fd89a7eca
No known key found for this signature in database
GPG key ID: 779591AFDB81F06C
2 changed files with 37 additions and 24 deletions

View file

@ -169,6 +169,22 @@ Then configure Emacs to use your email address:
t) t)
#+END_SRC #+END_SRC
If you use multiple email accounts, defining them with ~set-email-account!~ will
automatically set the appropriate account context when replying to emails in
that account's maildir. ~mu4e-context-policy~ and ~mu4e-compose-context-policy~
can be modified to change context behavior when opening mu4e and composing
email:
#+begin_src emacs-lisp
(setq mu4e-context-policy 'ask-if-none
mu4e-compose-context-policy 'always-ask)
#+end_src
If you send mail from various email aliases for different services,
~+mu4e-personal-addresses~ can be set per-context with ~set-email-account!~. If
you are not replying to an email to or from one of the specified aliases, you
will be prompted for an alias to send from.
*** Gmail *** Gmail
With the =+gmail= flag, integrations are applied which account for the different With the =+gmail= flag, integrations are applied which account for the different
behaviour of Gmail. behaviour of Gmail.

View file

@ -30,8 +30,11 @@ default/fallback account."
collect context)) collect context))
(let ((context (make-mu4e-context (let ((context (make-mu4e-context
:name label :name label
:enter-func (lambda () (mu4e-message "Switched to %s" label)) :enter-func
:leave-func #'mu4e-clear-caches (lambda () (mu4e-message "Switched to %s" label))
:leave-func
(lambda () (progn (setq +mu4e-personal-addresses nil)
(mu4e-clear-caches)))
:match-func :match-func
(lambda (msg) (lambda (msg)
(when msg (when msg
@ -328,26 +331,20 @@ When otherwise called, open a dired buffer and enable `dired-mu4e-attach-ctrl-c-
;;;###autoload ;;;###autoload
(defun +mu4e-set-from-address-h () (defun +mu4e-set-from-address-h ()
"Set the account for composing a message. If a 'To' header is present, "If the user defines multiple `+mu4e-personal-addresses' for email aliases
and correspands to an email address, this address will be selected. within a context, set `user-mail-address' to an alias found in the 'To' or
Otherwise, the user is prompted for the address they wish to use. Possible 'From' headers of the parent message if present, or prompt the user for a
selections come from the mu database or a list of email addresses associated preferred alias"
with the current context." (when-let ((addresses (if (or mu4e-contexts+mu4e-personal-addresses)
(unless (and mu4e-compose-parent-message (and (> (length +mu4e-personal-addresses) 1)
(let ((to (cdr (car (mu4e-message-field mu4e-compose-parent-message :to)))) +mu4e-personal-addresses)
(from (cdr (car (mu4e-message-field mu4e-compose-parent-message :from))))) (mu4e-personal-addresses))))
(if (member to (mu4e-personal-addresses))
(setq user-mail-address to)
(if (member from (mu4e-personal-addresses))
(setq user-mail-address from)
nil))))
(setq user-mail-address (setq user-mail-address
(if (= (length +mu4e-personal-addresses) 1) (if mu4e-compose-parent-message
(car +mu4e-personal-addresses) (let ((to (cdr (car (mu4e-message-field mu4e-compose-parent-message :to))))
(completing-read (from (cdr (car (mu4e-message-field mu4e-compose-parent-message :from)))))
"From: " (cond
(if-let ((context-addresses ((member to addresses) to)
(when mu4e~context-current ((member from addresses) from)
(alist-get '+mu4e-personal-addresses (mu4e-context-vars mu4e~context-current))))) (t (completing-read "From: " addresses))))
context-addresses (completing-read "From: " addresses)))))
(mu4e-personal-addresses)))))))