Creating folders for Dovecot Pigeonhole


  • Wed 20 October 2021
  • misc

Dovecot Pigeonhole is an implementation of the Sieve domain-specific language for server-side filtering of mail.

This is particularly of interest when your laptop is asleep and you're out and about with your companion devices and don't want your inbox innundated by mail that's getting missed by any client side filtering rules.

It's fair to say that I use this extensively. I have 116 rules for mail handling, the vast majority of which involve filing incoming mail in specific folders and optionally marking it read.

In RFC 5228 (linked above) section 4.1 describes the fileinto action and says, inter alia, "If the specified mailbox doesn't exist, the implementation MAY treat it as an error, create the mailbox, or deliver the message to an implementation-defined mailbox."

Pigeonhole is compliant; it will deliver the message to your inbox if the destination folder is not already there, which is not the action I'd prefer, and leads to an extra step to create the folder via the GUI on my Mac when setting up new filing rules not to mention a lot of head scratching in the event of a typo in either the .dovecot.sieve file or in Mail.app.

It turns out that one can create dovecot folders under Maildir from the command line via the doveadm tool, like so:

doveadm mailbox create -u username -s newfolder

and more particularly

doveadm mailbox create -u rs -s "Bulk.Membership Mail"

Note the . for folder hierarchy ("Membership Mail" is a subfolder of "Bulk") and the double quotes for supporting folder names with a space in them.

The following one-liner will create a folder for every folder referenced in a fileinto stanza (assuming canonical structuring) with a harmless error message for each one that already exists. Note that it assumes execution in the user's home directory and that the user's name is "rs". Edit to suit. :)

awk -F '"' '$1 ~ "fileinto" { print $2 }' < .dovecot.sieve | while read p; do doveadm mailbox create -u rs "$p" ; done