6.5 Compose hooks

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:

As mentioned, mu4e-compose-mode-hook is especially useful for editing-related settings:

Let’s look at an example:

(add-hook 'mu4e-compose-mode-hook
  (defun my-do-compose-stuff ()
    "My settings for message composition."
    (set-fill-column 72)
    (flyspell-mode)))

The 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.