Pigeonhole Sieve examples ========================= Contents 1. Pigeonhole Sieve examples 1. Mail filtering by various headers 2. Flagging or Highlighting your mail 3. Spam/Virus rules 1. Direct filtering using message header 2. Filtering using the spamtest and virustest extensions 4. Plus Addressed mail filtering 5. Vacation auto-reply 6. Include scripts 7. Translation from Procmail Below are some simple Sieve code examples, more can be found from http://libsieve.sourceforge.net/script1.php and http://wiki.fastmail.fm/index.php?title=SieveExamples. Mail filtering by various headers --------------------------------- Use if/elsif/else to store messages into various folders/subfolders: * ---%<---------------------------------------------------------------------- require ["fileinto", "envelope"]; if address :is "to" "dovecot@dovecot.org" { fileinto "Dovecot-list"; } elsif envelope :is "from" "owner-cipe-l@inka.de" { fileinto "lists.cipe"; } elsif anyof (header :contains "X-listname" "lugog@cip.rz.fh-offenburg.de", header :contains "List-Id" "Linux User Group Offenburg") { fileinto "ml.lugog"; } else { # The rest goes into INBOX # default is "implicit keep", we do it explicitly here keep; } ---%<---------------------------------------------------------------------- "anyof" means logical OR, "allof" is AND. Forward mails with "order" or "buy" in their subject to another address: * ---%<---------------------------------------------------------------------- if header :contains "subject" ["order", "buy"] { redirect "orders@company.dom"; } ---%<---------------------------------------------------------------------- Message-ID and recipient of forwarded message are stored in a '.dovecot.lda-dupes' at users home directory to prevent mail loops. Flagging or Highlighting your mail ---------------------------------- Some mail readers use these flags: ---%<------------------------------------------------------------------------- require "imap4flags"; require "regex"; if anyof (exists "X-Cron-Env", header :regex ["subject"] [".* security run output", ".* monthly run output", ".* daily run output", ".* weekly run output"]) { addflag "$label1"; # ie 'Important'/red label within Thunderbird # Other flags: # addflag "$label1"; # Important: #ff0000 => red # addflag "$label2"; # Work: #ff9900 => orange # addflag "$label3"; # personal: #009900 => green # addflag "$label4"; # todo: #3333ff => blue # addflag "$label5"; # later: #993399 => violet # } ---%<------------------------------------------------------------------------- Local copy of your emails: ---%<------------------------------------------------------------------------- require "envelope"; if envelope "from" "my_address@my_domain.com" { setflag "\\seen"; } ---%<------------------------------------------------------------------------- /Useful, when you want sieve to manage your incoming *and* outgoing email (you must ask your mail reader to Bcc your mail to your dovecot in this case)./ Spam/Virus rules ---------------- Most spam and virus scanners add a special header to mail messages, so that users can apply filtering accordingly. Depending on how the Sieve interpreter is configured, filtering can either be performed by evaluating these headers directly, or using the spamtest and virustest extensions. Direct filtering using message header ------------------------------------- Evaluating the headers directly is always possible as long as the headers are actually added to the messages by the scanner software. For example, to fileSpamAssassin-tagged mails into a folder called "Spam": ---%<------------------------------------------------------------------------- require "fileinto"; if header :contains "X-Spam-Flag" "YES" { fileinto "Spam"; } ---%<------------------------------------------------------------------------- The following example discards SpamAssassin-tagged mails with level higher than or equal to 10: ---%<------------------------------------------------------------------------- if header :contains "X-Spam-Level" "**********" { discard; stop; } ---%<------------------------------------------------------------------------- Some spam scanners only produce a numeric score in a header. Then, the test becomes more involved: ---%<------------------------------------------------------------------------- require ["comparator-i;ascii-numeric","relational"]; if allof ( not header :matches "x-spam-score" "-*", header :value "ge" :comparator "i;ascii-numeric" "x-spam-score" "10" ) { discard; stop; } ---%<------------------------------------------------------------------------- *NOTE:* Be very careful when matching against spam score headers using the relational extension and the i;ascii-numeric comparator. This comparator can only be used to match unsigned integers. Strings that do not begin with a digit character represent positive infinity and will therefore always be larger than any score mentioned in your rule! That is why the above example first checks the minus sign explicitly. Filtering using the spamtest and virustest extensions ----------------------------------------------------- When the spamtest [http://tools.ietf.org/html/rfc5235#section-3.2] and virustest [http://tools.ietf.org/html/rfc5235#section-3.3] extensions are configured on the server ( [Pigeonhole.Sieve.Extensions.SpamtestVirustest.txt] is explained how), users (and GUIs) can have a much easier way to filter spam and virus messages respectively. To filter spam, the spamtest extension can for example be used as follows: ---%<------------------------------------------------------------------------- require "spamtestplus"; require "fileinto"; require "relational"; require "comparator-i;ascii-numeric"; /* If the spamtest fails for some reason, e.g. spam header is missing, file * file it in a special folder. */ if spamtest :value "eq" :comparator "i;ascii-numeric" "0" { fileinto "Unclassified"; /* If the spamtest score (in the range 1-10) is larger than or equal to 3, * file it into the spam folder: */ } elsif spamtest :value "ge" :comparator "i;ascii-numeric" "3" { fileinto "Spam"; /* For more fine-grained score evaluation, the :percent tag can be used. The * following rule discards all messages with a percent score * (relative to maximum) of more than 85 %: */ } elsif spamtest :value "gt" :comparator "i;ascii-numeric" :percent "85" { discard; } /* Other messages get filed into INBOX */ ---%<------------------------------------------------------------------------- The virustest extension can be used in a similar manner: ---%<------------------------------------------------------------------------- require "virustest"; require "fileinto"; require "relational"; require "comparator-i;ascii-numeric"; /* Not scanned ? */ if virustest :value "eq" :comparator "i;ascii-numeric" "0" { fileinto "Unscanned"; /* Infected with high probability (value range in 1-5) */ } if virustest :value "eq" :comparator "i;ascii-numeric" "4" { /* Quarantine it in special folder (still somewhat dangerous) */ fileinto "Quarantine"; /* Definitely infected */ } elsif virustest :value "eq" :comparator "i;ascii-numeric" "5" { /* Just get rid of it */ discard; } ---%<------------------------------------------------------------------------- Plus Addressed mail filtering ----------------------------- This is useful when you don't want just any +tag to create a directory, but you want to use tagged addresses such as with amavisd-new. This example would place email addressed to user+spam@example.com into user's Spam folder. ---%<------------------------------------------------------------------------- require ["fileinto", "envelope", "subaddress"]; if envelope :detail "to" "spam"{ fileinto "Spam"; } ---%<------------------------------------------------------------------------- To work with Postfix, this requires that the envelope "to" still contains the full address, so pass it with the -a flag. ---%<------------------------------------------------------------------------- dovecot unix - n n - - pipe flags=DRhu user=mail:mail argv=/usr/local/libexec/dovecot/dovecot-lda -f ${sender} -d ${user}@${nexthop} -a ${recipient} ---%<------------------------------------------------------------------------- or ---%<------------------------------------------------------------------------- mailbox_command = /usr/lib/dovecot/dovecot-lda -a "$RECIPIENT" ---%<------------------------------------------------------------------------- Vacation auto-reply ------------------- ---%<------------------------------------------------------------------------- require ["fileinto", "vacation"]; # Move spam to spam folder if header :contains "X-Spam-Flag" "YES" { fileinto "spam"; # Stop here so that we do not reply on spams stop; } vacation # Reply at most once a day to a same sender :days 1 :subject "Out of office reply" # List of additional recipient addresses which are included in the auto replying. # If a mail's recipient is not the envelope recipient and it's not on this list, # no vacation reply is sent for it. :addresses ["j.doe@company.dom", "john.doe@company.dom"] "I'm out of office, please contact Joan Doe instead. Best regards John Doe"; ---%<------------------------------------------------------------------------- Include scripts --------------- It's possible to include other Sieve scripts in your script: ---%<------------------------------------------------------------------------- require ["include"]; include :global "global-spam"; include :personal "my-own-spam"; ---%<------------------------------------------------------------------------- The lookup directories can be specified with: ---%<------------------------------------------------------------------------- plugin { # Directory for :personal include scripts. The default is to use home directory. sieve_dir = %h/sieve # Directory for :global include scripts (not to be confused with sieve_global_path). # If unset, the include fails. sieve_global_dir = /etc/dovecot/sieve/ } ---%<------------------------------------------------------------------------- Both 'sieve_dir' and 'sieve_global_dir' may also be overridden by [UserDatabase.ExtraFields.txt]. It's not currently possible to use subdirectories for the scripts. Having a '/' character in the script name always fails the include. This is just an extra check to avoid potential problems with including scripts within mail directories. Translation from Procmail ------------------------- There exists a script which attempts to translate simple Procmail rules into Sieve rules:http://www.earth.ox.ac.uk/~steve/sieve/procmail2sieve.pl (dovecot.org mirror [http://dovecot.org/tools/procmail2sieve.pl]) Here's the original post announcing it: http://dovecot.org/list/dovecot/2007-March/020895.html (This file was created from the wiki on 2011-08-29 04:42)