mu4e account switching

The following code snippet, meant to demonstrate mu4e-compose-pre-hook, is found in mu4e manual:

;; 1) messages to me@foo.com should be replied with From:me@foo.com
;; 2) messages to me@bar.com should be replied with From:me@bar.com
;; 3) all other mail should use From:me@cuux.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.com")
              "me@foo.com")
            ((mu4e-message-contact-field-matches msg :to "me@bar.com")
              "me@bar.com")
            (t "me@cuux.com")))))))

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 "stannis.baratheon@seven-kingdoms.com"
                                 :signature "Best dad ever\nAlso Protector of the Realm")
                   (make-account :full-name "Daenerys Targaryen"
                                 :address "daenerys.targaryen@meereen.com"
                                 :signature "Dragon pup for sale")
                   (make-account :full-name "Melisandre"
                                 :address "melisandre@lordoflight.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
blog comments powered by Disqus