Next: Signing and encrypting, Previous: Address autocompletion, Up: Editor view [Contents]
If you want to change some setting, or execute some custom action before message composition starts, you can define a hook function. mu4e offers two hooks:
mu4e-compose-pre-hook: this hook is run before composition starts; if
you are composing a reply, forward a message, or edit an existing
message, the variable mu4e-compose-parent-message points to the message being
replied to, forwarded or edited, and you can use mu4e-message-field to get the
value of various properties (and see Message
functions).mu4e-compose-mode-hook: this hook is run just before composition starts, when
the whole buffer has already been set up. This is a good place for editing-related settings.
mu4e-compose-parent-message (see above) is also at your disposal.Let’s look at some examples. First, suppose we want to set the From:-address for a reply message based on the receiver of the original:
;; 1) messages to me@foo.example.com should be replied with From:me@foo.example.com
;; 2) messages to me@bar.example.com should be replied with From:me@bar.example.com
;; 3) all other mail should use From:me@cuux.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 "me@foo.example.com")
"me@foo.example.com")
((mu4e-message-contact-field-matches msg :to "me@bar.example.com")
"me@bar.example.com")
(t "me@cuux.example.com")))))))
Secondly, as mentioned, mu4e-compose-mode-hook is especially useful for
editing-related settings. For example:
(add-hook 'mu4e-compose-mode-hook
(defun my-do-compose-stuff ()
"My settings for message composition."
(set-fill-column 72)
(flyspell-mode)))
This hook is also useful for adding headers or changing headers, since the message is fully
formed when this hook runs. For example, to add a Bcc:-header, you could add something
like the following, using message-add-header from message-mode.
(add-hook 'mu4e-compose-mode-hook
(defun my-add-bcc ()
"Add a Bcc: header."
(save-excursion (message-add-header "Bcc: me@example.com\n"))))
Or to something context-specific:
(add-hook 'mu4e-compose-mode-hook
(lambda()
(let* ((ctx (mu4e-context-current))
(name (if ctx (mu4e-context-name ctx))))
(when name
(cond
((string= name "account1")
(save-excursion (message-add-header "Bcc: account1@example.com\n")))
((string= name "account2")
(save-excursion (message-add-header "Bcc: account2@example.com\n"))))))))
For a more general discussion about extending mu4e, see Extending mu4e.
Next: Signing and encrypting, Previous: Address autocompletion, Up: Editor view [Contents]