The following code snippet, meant to demonstrate
mu4e-compose-pre-hook, is found in
;; 1) messages to email@example.com should be replied with From:firstname.lastname@example.org ;; 2) messages to email@example.com should be replied with From:firstname.lastname@example.org ;; 3) all other mail should use From:email@example.com (add-hook 'mu4e-compose-pre-hook (defun my-set-from-address () "Set the From address based on the To address of the original." (let ((msg mu4e-compose-parent-message)) ;; msg is shorter... (when msg (setq user-mail-address (cond ((mu4e-message-contact-field-matches msg :to "firstname.lastname@example.org") "email@example.com") ((mu4e-message-contact-field-matches msg :to "firstname.lastname@example.org") "email@example.com") (t "firstname.lastname@example.org")))))))
As an illustration, it serves its purpose perfectly, but it goes further than that, answering a need to those of us with multiple email accounts. As a matter of fact, I have seen this snippet many a time in the wild. Sightly adapted, of course. To accomodate configurations with a work and a personal email address, for example. It is only a beginning of a solution, though, because in the real world, in addition to the
From email, you would probably want to switch the full name and signature as well.
I have come up with a solution that works well for me and that I am offering below.
(cl-defstruct account full-name address signature) (setq my-accounts (list (make-account :full-name "Stannis Baratheon" :address "email@example.com" :signature "Best dad ever\nAlso Protector of the Realm") (make-account :full-name "Daenerys Targaryen" :address "firstname.lastname@example.org" :signature "Dragon pup for sale") (make-account :full-name "Melisandre" :address "email@example.com" :signature "I love science"))) (add-hook 'mu4e-compose-pre-hook (lambda () "Set the From address based on the To address of the original." (let* ((msg mu4e-compose-parent-message) (default-account (car my-accounts)) (current-account (if msg (loop for account in my-accounts when (mu4e-message-contact-field-matches msg :to (account-address account)) return account) default-account))) (setq user-mail-address (account-address current-account) user-full-name (account-full-name current-account) mu4e-compose-signature (account-signature current-account)))))
The nice thing about this solution is that it is easily extensible. You can add as many accounts as you want in the list. Also, you can easily add or remove properties to the account object/struct. It should just work™.
P.S. Follow me on Twitter.Daniel Szmulewicz 27 June 2015