Using Sieve: Resetting Email Filters Effectively


Sieve is a powerful tool to filter email. In this article, I present a few examples of filters built with Sieve (the dialect used by Proton mail).

Reset Filter

In some cases , a ‘reset filter’ might be required to remove previous filtering. A reset filter should:

  1. remove all stars from the starred messages (this is currently not supported by my email provider)
  2. move all messages back to Inbox
  3. remove all the expire times set
  4. mark the messages back as unread

Using the Sieve language, a filter like this would look like this:

require ["include", "environment", "variables", "relational", "comparator-i;ascii-numeric", "spamtest"];
require ["fileinto", "imap4flags", "vnd.proton.expire"];

# Generated: Do not run this script on spam messages
if allof (environment :matches "vnd.proton.spam-threshold" "*", spamtest :value "ge" :comparator "i;ascii-numeric" "${1}") {
return;
}

/**
* @type or
* @comparator contains
*/

# Note: this reset filter should not used as normal operation and it should be triggered as one off as part of maintenance

if anyof (
address :all :comparator "i;unicode-casemap" :contains ["To", "Cc", "Bcc"] "parvu",
address :domain ["To", "Cc", "Bcc"] "mac.com",
address :domain ["To", "Cc", "Bcc"] "parvu.org",
address :domain ["To", "Cc", "Bcc", "From"] "*",
header :matches "subject" "*"
)
{
removeflag "\\Seen";
unexpire;
fileinto "inbox";
}

Note: this reset filter should not used as normal operation

it should be triggered as one off as part of maintenance

Sort incoming emails into own mailboxes

I use several aliases and integrate a few other mail services which I keep as legacy (people that have my old email addresses). It makes sense to presort this incoming email into separate mailboxes for easier processing:

require ["include", "environment", "variables", "relational", "comparator-i;ascii-numeric", "spamtest"];
require ["fileinto", "imap4flags", "envelope", "vnd.proton.expire"];
# Generated: Do not run this script on spam messages
if allof (environment :matches "vnd.proton.spam-threshold" "*", spamtest :value "ge" :comparator "i;ascii-numeric" "${1}") {
return;
}
# icloud emails
if anyof
(
address :domain ["To","Cc","Bcc", "From"] "privaterelay.appleid.com",
address :domain ["To","Cc","Bcc", "From"] "mac.com",
address :domain ["To","Cc","Bcc", "From"] "me.com",
address :domain ["To","Cc","Bcc", "From"] "icloud.com"
)
{
fileinto "MAILBOX/icloud";
removeflag "\\Seen";
unexpire;
}
# runbox emails
elsif address :domain ["To","Cc","Bcc"] "runbox.com"
{
fileinto "MAILBOX/runbox";
removeflag "\\Seen";
unexpire;
}
# acn emails
elsif address :domain ["To","Cc","Bcc", "From"] "accenture.com"
{
fileinto "MAILBOX/acn";
removeflag "\\Seen";
unexpire;
}
elsif envelope :all :is "X-Original-To" "r14q@pm.me"
{
fileinto "MAILBOX/r14q@pm.me";
removeflag "\\Seen";
unexpire;
}
elsif anyof (
envelope :all :is "X-Original-To" "helpdesk@parvu.org",
envelope :all :is "Delivered-To" "helpdesk@parvu.org"
)
{
fileinto "MAILBOX/helpdesk@parvu.org";
removeflag "\\Seen";
unexpire;
}
elsif address :all :comparator "i;unicode-casemap" :matches "From" "*@*"
{
fileinto "MAILBOX/${1}@${2}";
removeflag "\\Seen";
unexpire;
}
elsif address :all :comparator "i;unicode-casemap" :matches "Bcc" "*@*"
{
fileinto "MAILBOX/${1}@${2}";
removeflag "\\Seen";
unexpire;
}
elsif address :all :comparator "i;unicode-casemap" :matches "Cc" "*@*"
{
fileinto "MAILBOX/${1}@${2}";
removeflag "\\Seen";
unexpire;
}
elsif address :all :comparator "i;unicode-casemap" :matches "To" "*@*"
{
fileinto "MAILBOX/${1}@${2}";
removeflag "\\Seen";
unexpire;
}
else
{
fileinto "MAILBOX";
removeflag "\\Seen";
unexpire;
}

Set expiration dates

If your email provider supports the feature, you can set expiration dates for emails (for example marketing emails with offers that expire).

In the example, I will process incoming email that will be:

  • archived so they do not show in the inbox any longer
  • labeled ‘To Do’ (also I will add a task in OmniFocus to check To Do items every two days)
  • mark them Unread (this is not really needed, only if I must run the filter manually at a later stage)
  • set expire after seven days. This can be refined later for other emails to have different expiry periods
require ["include", "environment", "variables", "relational", "comparator-i;ascii-numeric", "spamtest"];
require ["fileinto", "imap4flags", "vnd.proton.expire"];

# Generated: Do not run this script on spam messages
if allof (environment :matches "vnd.proton.spam-threshold" "*",
spamtest :value "ge" :comparator "i;ascii-numeric" "${1}")
{
    return;
}
#
#
#
if anyof (
	# address :all :comparator "i;unicode-casemap" :is "From" "garmin@emea.garmin.com",
    	# address :all :comparator "i;unicode-casemap" :is "From" "reply@newsletter.kusmitea.com",	
    	# address :all :comparator "i;unicode-casemap" :is "From" "springernature@newsletter.springernature.com",
   	# address :all :comparator "i;unicode-casemap" :is "From" "shop@pocketbook.de",
    	address :all :comparator "i;unicode-casemap" :is "From" "garmin@emea.garmin.com")
{
  	fileinto "To Do"	;
  	fileinto "Archive"	;
  	setflag  "\\Unseen"	;
  	expire   "day" "7"	;
}

References

  1. Sieve filter (advanced custom filters)
  2. Sieve: An Email Filtering Language

Discover more from Radu Pârvu

Subscribe to get the latest posts sent to your email.


Leave a Reply

Discover more from Radu Pârvu

Subscribe now to keep reading and get access to the full archive.

Continue reading

Discover more from Radu Pârvu

Subscribe now to keep reading and get access to the full archive.

Continue reading