summaryrefslogtreecommitdiffstats
path: root/doc/wiki/Pigeonhole.Sieve.Examples.txt
blob: fb578df02bed1e68072487bb7fad3d777c2b7a17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
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. Archiving a Mailinglist by Date

     8. Emulating lmtp_save_to_detail_mailbox=yes

     9. Translation from Procmail

Below are some simple Sieve code examples, more can be found at
http://sieve.info/examplescripts.

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", "imap4flags"];
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 ( <here>
[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
-----------------------------

Using the subaddress [http://tools.ietf.org/html/rfc5233/] extension, it is
possible to match against the 'detail' part of an e-mail address, e.g. a
''+tag'' suffix to the local part of the address. This is for example 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";
}
---%<-------------------------------------------------------------------------

The following more advanced example uses the subaddress
[http://tools.ietf.org/html/rfc5233/] extension to handle recipient addresses
structured as 'sales+<name>@company.com' in a special way. The '<name>' part is
extracted from the address using variables
[http://tools.ietf.org/html/rfc5229/] extension, transformed into a format with
the first letter in upper case and subsequently used to create the folder name
where the message is stored. The folder name is structured as 'users/<name>'.
If the '+<name>' detail is omitted from the recipient address, the message is
filed in the 'sales' folder.

---%<-------------------------------------------------------------------------
require ["variables", "envelope", "fileinto", "subaddress"];

if envelope :is :user "to" "sales" {
  if envelope :matches :detail "to" "*" {
    /* Save name in ${name} in all lowercase except for the first letter.
     * Joe, joe, jOe thus all become 'Joe'.
     */
    set :lower :upperfirst "name" "${1}";
  }

  if string :is "${name}" "" {
    /* Default case if no detail is specified */
    fileinto "sales";
  } else {
    /* For sales+joe@ this will become users/Joe */
    fileinto "users/${name}";
  }
}
---%<-------------------------------------------------------------------------

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 ${original_recipient}
---%<-------------------------------------------------------------------------

or

---%<-------------------------------------------------------------------------
mailbox_command = /usr/lib/dovecot/dovecot-lda -a "$RECIPIENT"
---%<-------------------------------------------------------------------------

Vacation auto-reply
-------------------

Auto-responder functionality is implemented using the vacation
[http://tools.ietf.org/html/rfc5230/] extension. The following script sends
out-of-office replies when the message is not spam:

---%<-------------------------------------------------------------------------
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";
---%<-------------------------------------------------------------------------

It's also possible to include the original subject using the variables
[http://tools.ietf.org/html/rfc5229/] extension:

---%<-------------------------------------------------------------------------
require ["variables", "vacation"];
# Store old Subject line so it can be used in vacation message
if header :matches "Subject" "*" {
        set "subjwas" ": ${1}";
}
vacation
  :days 1
  :subject "Out of office reply${subjwas}"
  :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 <userdb extra
fields> [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.

Archiving a Mailinglist by Date
-------------------------------

You can archive messages from mailing lists in a date-structured folder tree as
follows:

---%<-------------------------------------------------------------------------
require ["variables","date","fileinto","mailbox"];

# Extract date info
if currentdate :matches "year" "*" { set "year" "${1}"; }
if currentdate :matches "month" "*" { set "month" "${1}"; }

# Archive Dovecot mailing list items by year and month.
# Create folder when it does not exist.
if header :is "list-id" "dovecot.dovecot.org" {
  fileinto :create "INBOX.Lists.${year}.${month}.dovecot";
}
---%<-------------------------------------------------------------------------

For example, in March 2013 this puts messages from the Dovecot mailing list in
a folder called 'INBOX.Lists.2013.03.dovecot'. It combines the date
[http://tools.ietf.org/html/rfc5260#section-4] and variables
[http://tools.ietf.org/html/rfc5229/] extensions to extract the required date
strings. Using the ':create' argument for the 'fileinto' command, the indicated
folder is created automatically if it doesn't exist. The ':create' argument is
provided by the mailbox [http://tools.ietf.org/html/rfc5490#section-3]
extension.

Emulating lmtp_save_to_detail_mailbox=yes
-----------------------------------------

If you can't turn this option on, you can emulate the behaviour to some extent
with following code.

---%<-------------------------------------------------------------------------
require ["variables", "fileinto", "envelope", "subaddress", "mailbox"];

if envelope :matches :detail "to" "*" {
  # you can prefix with INBOX/ or INBOX. if necessary
  # remove :create if you want to permit only existing mailboxes
  fileinto :create "${1}";
}
---%<-------------------------------------------------------------------------

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 2019-06-19 12:42)