I'm using contexts within mu4e, and I know how to add custom headers via the mu4e-header-info-custom list.

What I'd like to do, however, is to alter existing headers when replying or composing within certain contexts.

Here's one example: if I reply to a message that matches one of these special contexts, I'd like the To: header to be automatically changed to a different address, or to add additional addresses to the To: line.

Suppose the sender is sender@domain.com. If I reply to the message, I'd like the To: line to automatically be changed to:

To: newuser@domain.com

... or even perhaps to ...

To: sender@domain.com, newuser1@domain.com, newuser2@domain.com

I might want to automatically change other existing headers, as well, such as Subject:.

Is there any way to do this in mu4e?


I will not take credit for this answer. cpbotha wrote a very informative blog post on this very topic. There are a few functions that need to be copied into your init.el or .emacs file, one for checking the recipient of the email, then executing a function that updates the user-full-name, user-mail-address, and updates the signature. This is a gist for you, but refer to the original blog for more useful configuration ideas.

(defun get-string-from-file (filePath)
  "Return FILEPATH's file content."
    (insert-file-contents filePath)

(defun my-mu4e-personal()
  (message "personal mail account")
  (setq user-full-name "MyFirst MyLast"
    user-mail-address "my.email@test.com"
    mu4e-compose-signature (get-string-from-file "~/.signature")))

(defun my-mu4e-work()
  (message "work mail account")
  (setq user-full-name "MyFirst MyLast"
    user-mail-address "my.email@work-test.com"
    mu4e-compose-signature (get-string-from-file "~/.signature-work")))

;; when you reply to a message, use the identity that the mail was sent to
;; the cpbotha variation -- function that checks to, cc and bcc fields
(defun cpb-mu4e-is-message-to (msg rx)
  "Check if to, cc or bcc field in MSG has any address in RX."
  (or (mu4e-message-contact-field-matches msg :to rx)
      (mu4e-message-contact-field-matches msg :cc rx)
      (mu4e-message-contact-field-matches msg :bcc rx)))

;; we only do something if we recognize something (i.e. no stupid default)
(add-hook 'mu4e-compose-pre-hook
  (defun my-set-from-address ()
    "Set current identity based on to, cc, bcc of original."
      (let ((msg mu4e-compose-parent-message)) ;; msg is shorter...
        (if msg
           ((cpb-mu4e-is-message-to msg "my.email@test.com")
           ((cpb-mu4e-is-message-to msg "my.email@work-test.com")
| 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.