Mu4e documentation has an example of smart refiling, whereby you can choose which maildir to refile an email based on the properties of that email. I'd like to do that, except the destination maildir's name is a property of the email (specifically the :to property).

Probably easiest to explain in an example. In the documentation:

(setq mu4e-refile-folder
  (lambda (msg)
      ;; messages to the mu mailing list go to the /mu folder
      ((mu4e-message-contact-field-matches msg :to
     "[email protected]")
    ;; ........[ SNIP ]

In this example all emails to a googlegroups [email protected] end up in the /mu maildir. I'd like to go one further. I'd like %anything%@googlegroups.com to go to the maildir /googlegroups/%anything%, for any value of %anything%.

1 Answer 1


Wow, what a ride into the world of elisp. I think I got it so that it works, well enough for my modest needs anyway (don't care about cross-posting, case differences etc). As with most things, you get more responses on SE by posting code than with an open-ended question such as I did originally, and I am happy to improve upon this snippet with suggestions. This really is my first non-toy function I've ever created in elisp.

Massive props to the creators or mu4e and s.el. Both these packages are beautifully documented and made the learning experience a heck of a lot more pleasant.

You will need the s.el package for s-suffix?, s-downcase (not that it's strictly needed), and s-chop-suffix

(defun personal/refile-maildir (msg)
  "Refile based on a message's characteristics"
      ((to-addresses (mu4e-message-field msg :to))
       (maillist-addr  (cl-find-if
                        (lambda (to) (s-suffix? "@googlegroups.com" (cdr to)))
        (concat "/googlegroups." (s-chop-suffix "@googlegroups.com" (s-downcase (cdr maillist-addr)))))
       (t "/Archive"))))

Guaranteed there are cuter answers out there so I will not mark this question as solved for a while, in case anyone wants to chip in with a better solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.