When refiling messages, perhaps to archive them, it can be useful to have different target folders for different messages, based on some property of those messages — smart refiling.
To accomplish this, we can set the refiling folder (mu4e-refile-folder)
to a function that returns the actual refiling folder for the particular
message. An example should clarify this:
(setq mu4e-refile-folder
(lambda (msg)
(cond
;; messages to the mu mailing list go to the /mu folder
((mu4e-contact-matches-p msg :to "mu-discuss@googlegroups.com") "/mu")
;; messages sent directly to some specific address go to /private
((mu4e-contact-matches-p msg :to "me@example.com") "/private")
;; messages with football or soccer in the subject go to /football
((string-match "football\\|soccer" (mu4e-message-field msg :subject))
"/football")
;; messages sent by me go to the sent folder
((mu4e-message-sent-by-me msg) mu4e-sent-folder)
;; everything else goes to /archive
;; important to have a catch-all at the end!
(t "/archive"))))
This can be very powerful; you can select some messages in the headers view, then press r, and have them all marked for refiling to their particular folders.
Some notes:
mu4e-refile-folder to an anonymous (lambda) function. This
function takes one argument, a message plist16. The plist corresponds to the message at point. See
Message functions for a discussion on how to deal with them.
cond control structure; the function
returns the first of the clauses that matches. It’s important to make the last
clause a catch-all, so we always return some folder.
mu4e-contact-matches-p
17, which evaluates to
non-nil if any of the names or e-mail addresses in a contact field (in this
case, the To:-field) matches the regular expression. The contact-field
parameter can also be a list of such fields, e.g., (:to :cc), the special
value :recip which matches (:to :cc :bcc) or the special value
:any to match all contact-fields (the latter two since 1.14.4).
a property list describing a message
This is a convenience alias for
mu4e-message-contact-field-matches, since 1.14.4