I am downloading mail using fetchmail from a pop account and using gnus to read mails from the maildir.

I would like to run elisp code on emails that are matched by fancy split rules, or something similar.

My end goal would be to match all mails from [email protected] and run elisp code to extract the data and upload it to a server.

I have read the linked manual and got the idea after reading this portion

(! func split)
If the split is a list, and the first element is !, 
then split will be processed, and func will be called as a 
function with the result of split as argument. func should return a split.

Would I be able to use the function to run code for the side effects and still return the split, or am I on the completely wrong track?

1 Answer 1


I spent a few hours reading the docs and testing a solution. I have now solved this as per below.

  1. Set the nnmail split method to "nnmail-split-fancy"

    (setq nnmail-split-methods 'nnmail-split-fancy)

    This allows one to match incoming emails to groups.

  2. Set the var nnmail-split-fancy to define the rules to filter emails

    (setq nnmail-split-fancy '(|
               (any "deploy@client" (: script-send-mail))

    The "|" char can be thought of as an OR and the ":" as "execute function and return group". The rule above is the equivalent of saying "If any incoming mail's headers match 'deploy@client' then file according to the return of the function 'script-send-mail', else file in 'mail.misc'"

  3. Write function script-send-mail to operate on the current mail's buffer-string

    (defun script-send-mail ()
          ;; (widen) is necessary to get body of email
            ;; Extract data and send to server
            (script-extract-data (buffer-string))
            ;; final return is a gnus group
            (format "mail.deploy")))))

    Now, whenever an incoming mail is received matching [email protected] The function will run and do what it needs to with the email before it is sent to a group.

*ps: Not sure if Answering my own question is acceptable. Please close if I have crossed any guidelines.

  • 1
    Answering your own question is explicitly encouraged. You can accept your own answer after 48 hours, or accept an answer that someone else posts if you like it better than your own.
    – legoscia
    Commented Oct 16, 2014 at 17:15

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.