Skip to main content

Dispatch Catch-All Emails By Recipient

·291 words·2 mins
email sieve
Author
Fungal
Borned to be fungus, forced to be mushroom. she/her

After more than a year of procrastination I finally setup a catch-all domain for my email; whatever recipient is set it goes to a single and only one mailbox. So you can send an email to hello@example.com or contact@example.com, it will land in a single mailbox. Convenient, no? Even better, you can use this to segregate services on which you create an account, and know which sells your data, or gets pwned. E.g:

  • netflixandchill@example.com
  • twitterxxx@example.com
  • coolwebsite@example.com

Pretty rad, no?

However, one downside is all emails will fill your INBOX, making it a real mess and unmanageable. Is our fate doomed? No, email filters, aka Sieve, is our saviour! Roughly a dozen lines of script is enough:

require ["fileinto", "variables", "mailbox"];

if header :matches "To" "*<*@*" {
  fileinto :create "auto-sorted ${2}";
  stop;
}
if header :matches "To" "*@*" {
  fileinto :create "auto-sorted ${1}";
  stop;
}

My script-fu isn’t high, it can probably be improved. The two blocks that will interest you are the two if. They do pretty much the same thing, but matching different patterns.

  • First will capure the recipient when the To: field is in the form Lain <the-wire@example.com>.
  • Second will capture the recipient when the To: field is in the form the-wire@example.com.

In both situations, the captured value is the-wire, they use it to concatenate auto-sorted with the former. In the end, any of these incoming emails go in the directory auto-sorted the-wire.

Quick examples to recap:

  • hi@example.comauto-sorted hi
  • contact@example.comauto-sorted contact
  • Anon <netflixandchill@example.com>auto-sorted netflixandchill
  • netflixandchill@example.com>auto-sorted netflixandchill

Huge thanks to my friend who helped me to fix my skill-issue moments, and linked me the RFC1 that explains everything well. Also, my filter is hugely inspired by examples from RFC 5229§3.22.