D.2 Refiling messages

By setting mu4e-refile-folder to a function, you can dynamically determine where messages are to be refiled. If you want to do this based on the subject of a message, you can use a function that matches the subject against a list of regexes in the following way. First, set up a variable my-mu4e-subject-alist containing regexes plus associated mail folders:

(defvar my-mu4e-subject-alist '(("kolloqui\\(um\\|a\\)" . "/Kolloquium")
                                ("Calls" . "/Calls")
                                ("Lehr" . "/Lehre")
                                ("webseite\\|homepage\\|website" . "/Webseite"))
  "List of subjects and their respective refile folders.")

Now you can use the following function to automatically refile messages based on their subject line:

(defun my-mu4e-refile-folder-function (msg)
  "Set the refile folder for MSG."
  (let ((subject (mu4e-message-field msg :subject))
        (folder (or (cdar (member* subject my-mu4e-subject-alist
                                   :test #'(lambda (x y)
                                             (string-match (car y) x))))
                    "/General")))
    folder))

Note the "/General" folder: it is the default folder in case the subject does not match any of the regexes in my-mu4e-subject-alist.

In order to make this work, you’ll of course need to set mu4e-refile-folder to this function:

(setq mu4e-refile-folder 'my-mu4e-refile-folder-function)

If you have multiple accounts, you can accommodate them as well:

(defun my-mu4e-refile-folder-function (msg)
  "Set the refile folder for MSG."
  (let ((maildir (mu4e-message-field msg :maildir))
        (subject (mu4e-message-field msg :subject))
        folder)
    (cond
     ((string-match "Account1" maildir)
      (setq folder (or (catch 'found
                         (dolist (mailing-list my-mu4e-mailing-lists)
                           (if (mu4e-message-contact-field-matches
                             msg :to (car mailing-list))
                               (throw 'found (cdr mailing-list)))))
                       "/Account1/General")))
     ((string-match "Gmail" maildir)
      (setq folder "/Gmail/All Mail"))
     ((string-match "Account2" maildir)
      (setq folder (or (cdar (member* subject my-mu4e-subject-alist
                                      :test #'(lambda (x y)
                                                (string-match
                                                   (car y) x))))
                       "/Account2/General"))))
    folder))

This function actually uses different methods to determine the refile folder, depending on the account: for Account2, it uses my-mu4e-subject-alist, for the Gmail account it simply uses the folder “All Mail”. For Account1, it uses another method: it files the message based on the mailing list to which it was sent. This requires another variable:

(defvar my-mu4e-mailing-lists
  '(("mu-discuss@googlegroups.com" . "/Account1/mu4e")
    ("pandoc-discuss@googlegroups.com" . "/Account1/Pandoc")
    ("auctex@gnu.org" . "/Account1/AUCTeX"))
  "List of mailing list addresses and folders where
   their messages are saved.")