Imported Upstream version 4.84
[hcoop/debian/exim4.git] / doc / filter.txt
CommitLineData
420a0d19
CE
1Exim's interfaces to mail filtering
2
3Philip Hazel
4
5Copyright (c) 2014 University of Cambridge
6
7+-----------------------------------------------------------------------------+
8+-------------------------------------+--------------------------------+------+
9|Revision 4.84 |11 Aug 2014 |PH |
10+-------------------------------------+--------------------------------+------+
11-------------------------------------------------------------------------------
12
13TABLE OF CONTENTS
14
151. Forwarding and filtering in Exim
16
17 1.1. Introduction
18 1.2. Filter operation
19 1.3. Testing a new filter file
20 1.4. Installing a filter file
21 1.5. Testing an installed filter file
22 1.6. Details of filtering commands
23
242. Sieve filter files
25
26 2.1. Recognition of Sieve filters
27 2.2. Saving to specified folders
28 2.3. Strings containing header names
29 2.4. Exists test with empty list of headers
30 2.5. Header test with invalid MIME encoding in header
31 2.6. Address test for multiple addresses per header
32 2.7. Semantics of keep
33 2.8. Semantics of fileinto
34 2.9. Semantics of redirect
35 2.10. String arguments
36 2.11. Number units
37 2.12. RFC compliance
38
393. Exim filter files
40
41 3.1. Format of Exim filter files
42 3.2. Data values in filter commands
43 3.3. String expansion
44 3.4. Some useful general variables
45 3.5. Header variables
46 3.6. User variables
47 3.7. Current directory
48 3.8. Significant deliveries
49 3.9. Filter commands
50 3.10. The add command
51 3.11. The deliver command
52 3.12. The save command
53 3.13. The pipe command
54 3.14. Mail commands
55 3.15. Logging commands
56 3.16. The finish command
57 3.17. The testprint command
58 3.18. The fail command
59 3.19. The freeze command
60 3.20. The headers command
61 3.21. Obeying commands conditionally
62 3.22. String testing conditions
63 3.23. Numeric testing conditions
64 3.24. Testing for significant deliveries
65 3.25. Testing for error messages
66 3.26. Testing a list of addresses
67 3.27. Testing for personal mail
68 3.28. Alias addresses for the personal condition
69 3.29. Details of the personal condition
70 3.30. Testing delivery status
71 3.31. Multiple personal mailboxes
72 3.32. Ignoring delivery errors
73 3.33. Examples of Exim filter commands
74
75
76
77===============================================================================
781. FORWARDING AND FILTERING IN EXIM
79
80This document describes the user interfaces to Exim's in-built mail filtering
81facilities, and is copyright (c) University of Cambridge 2014. It corresponds
82to Exim version 4.84.
83
84
851.1 Introduction
86----------------
87
88Most Unix mail transfer agents (programs that deliver mail) permit individual
89users to specify automatic forwarding of their mail, usually by placing a list
90of forwarding addresses in a file called .forward in their home directories.
91Exim extends this facility by allowing the forwarding instructions to be a set
92of rules rather than just a list of addresses, in effect providing ".forward
93with conditions". Operating the set of rules is called filtering, and the file
94that contains them is called a filter file.
95
96Exim supports two different kinds of filter file. An Exim filter contains
97instructions in a format that is unique to Exim. A Sieve filter contains
98instructions in the Sieve format that is defined by RFC 3028. As this is a
99standard format, Sieve filter files may already be familiar to some users.
100Sieve files should also be portable between different environments. However,
101the Exim filtering facility contains more features (such as variable
102expansion), and better integration with the host environment (such as the use
103of external processes and pipes).
104
105The choice of which kind of filter to use can be left to the end-user, provided
106that the system administrator has configured Exim appropriately for both kinds
107of filter. However, if interoperability is important, Sieve is the only choice.
108
109The ability to use filtering or traditional forwarding has to be enabled by the
110system administrator, and some of the individual facilities can be separately
111enabled or disabled. A local document should be provided to describe exactly
112what has been enabled. In the absence of this, consult your system
113administrator.
114
115This document describes how to use a filter file and the format of its
116contents. It is intended for use by end-users. Both Sieve filters and Exim
117filters are covered. However, for Sieve filters, only issues that relate to the
118Exim implementation are discussed, since Sieve itself is described elsewhere.
119
120The contents of traditional .forward files are not described here. They
121normally contain just a list of addresses, file names, or pipe commands,
122separated by commas or newlines, but other types of item are also available.
123The full details can be found in the chapter on the redirect router in the Exim
124specification, which also describes how the system administrator can set up and
125control the use of filtering.
126
127
1281.2 Filter operation
129--------------------
130
131It is important to realize that, in Exim, no deliveries are actually made while
132a filter or traditional .forward file is being processed. Running a filter or
133processing a traditional .forward file sets up future delivery operations, but
134does not carry them out.
135
136The result of filter or .forward file processing is a list of destinations to
137which a message should be delivered. The deliveries themselves take place
138later, along with all other deliveries for the message. This means that it is
139not possible to test for successful deliveries while filtering. It also means
140that any duplicate addresses that are generated are dropped, because Exim never
141delivers the same message to the same address more than once.
142
143
1441.3 Testing a new filter file
145-----------------------------
146
147Filter files, especially the more complicated ones, should always be tested, as
148it is easy to make mistakes. Exim provides a facility for preliminary testing
149of a filter file before installing it. This tests the syntax of the file and
150its basic operation, and can also be used with traditional .forward files.
151
152Because a filter can do tests on the content of messages, a test message is
153required. Suppose you have a new filter file called myfilter and a test message
154in a file called test-message. Assuming that Exim is installed with the
155conventional path name /usr/sbin/sendmail (some operating systems use /usr/lib/
156sendmail), the following command can be used:
157
158/usr/sbin/sendmail -bf myfilter <test-message
159
160The -bf option tells Exim that the following item on the command line is the
161name of a filter file that is to be tested. There is also a -bF option, which
162is similar, but which is used for testing system filter files, as opposed to
163user filter files, and which is therefore of use only to the system
164administrator.
165
166The test message is supplied on the standard input. If there are no
167message-dependent tests in the filter, an empty file (/dev/null) can be used. A
168supplied message must start with header lines or the "From " message separator
169line that is found in many multi-message folder files. Note that blank lines at
170the start terminate the header lines. A warning is given if no header lines are
171read.
172
173The result of running this command, provided no errors are detected in the
174filter file, is a list of the actions that Exim would try to take if presented
175with the message for real. For example, for an Exim filter, the output
176
177Deliver message to: gulliver@lilliput.fict.example
178Save message to: /home/lemuel/mail/archive
179
180means that one copy of the message would be sent to
181gulliver@lilliput.fict.example, and another would be added to the file /home/
182lemuel/mail/archive, if all went well.
183
184The actions themselves are not attempted while testing a filter file in this
185way; there is no check, for example, that any forwarding addresses are valid.
186For an Exim filter, if you want to know why a particular action is being taken,
187add the -v option to the command. This causes Exim to output the results of any
188conditional tests and to indent its output according to the depth of nesting of
189if commands. Further additional output from a filter test can be generated by
190the testprint command, which is described below.
191
192When Exim is outputting a list of the actions it would take, if any text
193strings are included in the output, non-printing characters therein are
194converted to escape sequences. In particular, if any text string contains a
195newline character, this is shown as "\n" in the testing output.
196
197When testing a filter in this way, Exim makes up an "envelope" for the message.
198The recipient is by default the user running the command, and so is the sender,
199but the command can be run with the -f option to supply a different sender. For
200example,
201
202/usr/sbin/sendmail -bf myfilter \
203 -f islington@never.where <test-message
204
205Alternatively, if the -f option is not used, but the first line of the supplied
206message is a "From " separator from a message folder file (not the same thing
207as a From: header line), the sender is taken from there. If -f is present, the
208contents of any "From " line are ignored.
209
210The "return path" is the same as the envelope sender, unless the message
211contains a Return-path: header, in which case it is taken from there. You need
212not worry about any of this unless you want to test out features of a filter
213file that rely on the sender address or the return path.
214
215It is possible to change the envelope recipient by specifying further options.
216The -bfd option changes the domain of the recipient address, while the -bfl
217option changes the "local part", that is, the part before the @ sign. An
218adviser could make use of these to test someone else's filter file.
219
220The -bfp and -bfs options specify the prefix or suffix for the local part.
221These are relevant only when support for multiple personal mailboxes is
222implemented; see the description in section 3.31 below.
223
224
2251.4 Installing a filter file
226----------------------------
227
228A filter file is normally installed under the name .forward in your home
229directory - it is distinguished from a conventional .forward file by its first
230line (described below). However, the file name is configurable, and some system
231administrators may choose to use some different name or location for filter
232files.
233
234
2351.5 Testing an installed filter file
236------------------------------------
237
238Testing a filter file before installation cannot find every potential problem;
239for example, it does not actually run commands to which messages are piped.
240Some "live" tests should therefore also be done once a filter is installed.
241
242If at all possible, test your filter file by sending messages from some other
243account. If you send a message to yourself from the filtered account, and
244delivery fails, the error message will be sent back to the same account, which
245may cause another delivery failure. It won't cause an infinite sequence of such
246messages, because delivery failure messages do not themselves generate further
247messages. However, it does mean that the failure won't be returned to you, and
248also that the postmaster will have to investigate the stuck message.
249
250If you have to test an Exim filter from the same account, a sensible precaution
251is to include the line
252
253if error_message then finish endif
254
255as the first filter command, at least while testing. This causes filtering to
256be abandoned for a delivery failure message, and since no destinations are
257generated, the message goes on to be delivered to the original address. Unless
258there is a good reason for not doing so, it is recommended that the above test
259be left in all Exim filter files. (This does not apply to Sieve files.)
260
261
2621.6 Details of filtering commands
263---------------------------------
264
265The filtering commands for Sieve and Exim filters are completely different in
266syntax and semantics. The Sieve mechanism is defined in RFC 3028; in the next
267chapter we describe how it is integrated into Exim. The subsequent chapter
268covers Exim filtering commands in detail.
269
270
271
272===============================================================================
2732. SIEVE FILTER FILES
274
275The code for Sieve filtering in Exim was contributed by Michael Haardt, and
276most of the content of this chapter is taken from the notes he provided. Since
277Sieve is an extensible language, it is important to understand "Sieve" in this
278context as "the specific implementation of Sieve for Exim".
279
280This chapter does not contain a description of Sieve, since that can be found
281in RFC 3028, which should be read in conjunction with these notes.
282
283The Exim Sieve implementation offers the core as defined by RFC 3028,
284comparison tests, the subaddress parameter, the copy, envelope, fileinto,
285notify, and vacation extensions, but not the reject extension. Exim does not
286support message delivery notifications (MDNs), so adding it just to the Sieve
287filter (as required for reject) makes little sense.
288
289In order for Sieve to work properly in Exim, the system administrator needs to
290make some adjustments to the Exim configuration. These are described in the
291chapter on the redirect router in the full Exim specification.
292
293
2942.1 Recognition of Sieve filters
295--------------------------------
296
297A filter file is interpreted as a Sieve filter if its first line is
298
299# Sieve filter
300
301This is what distinguishes it from a conventional .forward file or an Exim
302filter file.
303
304
3052.2 Saving to specified folders
306-------------------------------
307
308If the system administrator has set things up as suggested in the Exim
309specification, and you use keep or fileinto to save a mail into a folder,
310absolute files are stored where specified, relative files are stored relative
311to $home, and inbox goes to the standard mailbox location.
312
313
3142.3 Strings containing header names
315-----------------------------------
316
317RFC 3028 does not specify what happens if a string denoting a header field does
318not contain a valid header name, for example, it contains a colon. This
319implementation generates an error instead of ignoring the header field in order
320to ease script debugging, which fits in with the common picture of Sieve.
321
322
3232.4 Exists test with empty list of headers
324------------------------------------------
325
326The exists test succeeds only if all the specified headers exist. RFC 3028 does
327not explicitly specify what happens on an empty list of headers. This
328implementation evaluates that condition as true, interpreting the RFC in a
329strict sense.
330
331
3322.5 Header test with invalid MIME encoding in header
333----------------------------------------------------
334
335Some MUAs process invalid base64 encoded data, generating junk. Others ignore
336junk after seeing an equal sign in base64 encoded data. RFC 2047 does not
337specify how to react in this case, other than stating that a client must not
338forbid to process a message for that reason. RFC 2045 specifies that invalid
339data should be ignored (apparently looking at end of line characters). It also
340specifies that invalid data may lead to rejecting messages containing them (and
341there it appears to talk about true encoding violations), which is a clear
342contradiction to ignoring them.
343
344RFC 3028 does not specify how to process incorrect MIME words. This
345implementation treats them literally, as it does if the word is correct but its
346character set cannot be converted to UTF-8.
347
348
3492.6 Address test for multiple addresses per header
350--------------------------------------------------
351
352A header may contain multiple addresses. RFC 3028 does not explicitly specify
353how to deal with them, but since the address test checks if anything matches
354anything else, matching one address suffices to satisfy the condition. That
355makes it impossible to test if a header contains a certain set of addresses and
356no more, but it is more logical than letting the test fail if the header
357contains an additional address besides the one the test checks for.
358
359
3602.7 Semantics of keep
361---------------------
362
363The keep command is equivalent to
364
365fileinto "inbox";
366
367It saves the message and resets the implicit keep flag. It does not set the
368implicit keep flag; there is no command to set it once it has been reset.
369
370
3712.8 Semantics of fileinto
372-------------------------
373
374RFC 3028 does not specify whether fileinto should try to create a mail folder
375if it does not exist. This implementation allows the sysadmin to configure that
376aspect using the appendfile transport options create_directory, create_file,
377and file_must_exist. See the appendfile transport in the Exim specification for
378details.
379
380
3812.9 Semantics of redirect
382-------------------------
383
384Sieve scripts are supposed to be interoperable between servers, so this
385implementation does not allow mail to be redirected to unqualified addresses,
386because the domain would depend on the system being used. On systems with
387virtual mail domains, the default domain is probably not what the user expects
388it to be.
389
390
3912.10 String arguments
392---------------------
393
394There has been confusion if the string arguments to require are to be matched
395case-sensitively or not. This implementation matches them with the match type
396:is (default, see section 2.7.1 of the RFC) and the comparator i;ascii-casemap
397(default, see section 2.7.3 of the RFC). The RFC defines the command defaults
398clearly, so any different implementations violate RFC 3028. The same is valid
399for comparator names, also specified as strings.
400
401
4022.11 Number units
403-----------------
404
405There is a mistake in RFC 3028: the suffix G denotes gibi-, not tebibyte. The
406mistake is obvious, because RFC 3028 specifies G to denote 2^30 (which is gibi,
407not tebi), and that is what this implementation uses as the scaling factor for
408the suffix G.
409
410
4112.12 RFC compliance
412-------------------
413
414Exim requires the first line of a Sieve filter to be
415
416# Sieve filter
417
418Of course the RFC does not specify that line. Do not expect examples to work
419without adding it, though.
420
421RFC 3028 requires the use of CRLF to terminate a line. The rationale was that
422CRLF is universally used in network protocols to mark the end of the line. This
423implementation does not embed Sieve in a network protocol, but uses Sieve
424scripts as part of the Exim MTA. Since all parts of Exim use LF as the newline
425character, this implementation does, too, by default, though the system
426administrator may choose (at Exim compile time) to use CRLF instead.
427
428Exim violates RFC 2822, section 3.6.8, by accepting 8-bit header names, so this
429implementation repeats this violation to stay consistent with Exim. This is in
430preparation for UTF-8 data.
431
432Sieve scripts cannot contain NUL characters in strings, but mail headers could
433contain MIME encoded NUL characters, which could never be matched by Sieve
434scripts using exact comparisons. For that reason, this implementation extends
435the Sieve quoted string syntax with \0 to describe a NUL character, violating \
4360 being the same as 0 in RFC 3028. Even without using \0, the following tests
437are all true in this implementation. Implementations that use C-style strings
438will only evaluate the first test as true.
439
440Subject: =?iso-8859-1?q?abc=00def
441
442header :contains "Subject" ["abc"]
443header :contains "Subject" ["def"]
444header :matches "Subject" ["abc?def"]
445
446Note that by considering Sieve to be an MUA, RFC 2047 can be interpreted in a
447way that NUL characters truncating strings is allowed for Sieve
448implementations, although not recommended. It is further allowed to use encoded
449NUL characters in headers, but that's not recommended either. The above example
450shows why.
451
452RFC 3028 states that if an implementation fails to convert a character set to
453UTF-8, two strings cannot be equal if one contains octets greater than 127.
454Assuming that all unknown character sets are one-byte character sets with the
455lower 128 octets being US-ASCII is not sound, so this implementation violates
456RFC 3028 and treats such MIME words literally. That way at least something
457could be matched.
458
459The folder specified by fileinto must not contain the character sequence ".."
460to avoid security problems. RFC 3028 does not specify the syntax of folders
461apart from keep being equivalent to
462
463fileinto "INBOX";
464
465This implementation uses inbox instead.
466
467Sieve script errors currently cause messages to be silently filed into inbox.
468RFC 3028 requires that the user is notified of that condition. This may be
469implemented in the future by adding a header line to mails that are filed into
470inbox due to an error in the filter.
471
472
473
474===============================================================================
4753. EXIM FILTER FILES
476
477This chapter contains a full description of the contents of Exim filter files.
478
479
4803.1 Format of Exim filter files
481-------------------------------
482
483Apart from leading white space, the first text in an Exim filter file must be
484
485# Exim filter
486
487This is what distinguishes it from a conventional .forward file or a Sieve
488filter file. If the file does not have this initial line (or the equivalent for
489a Sieve filter), it is treated as a conventional .forward file, both when
490delivering mail and when using the -bf testing mechanism. The white space in
491the line is optional, and any capitalization may be used. Further text on the
492same line is treated as a comment. For example, you could have
493
494# Exim filter <<== do not edit or remove this line!
495
496The remainder of the file is a sequence of filtering commands, which consist of
497keywords and data values. For example, in the command
498
499deliver gulliver@lilliput.fict.example
500
501the keyword is "deliver" and the data value is
502"gulliver@lilliput.fict.example". White space or line breaks separate the
503components of a command, except in the case of conditions for the if command,
504where round brackets (parentheses) also act as separators. Complete commands
505are separated from each other by white space or line breaks; there are no
506special terminators. Thus, several commands may appear on one line, or one
507command may be spread over a number of lines.
508
509If the character # follows a separator anywhere in a command, everything from #
510up to the next newline is ignored. This provides a way of including comments in
511a filter file.
512
513
5143.2 Data values in filter commands
515----------------------------------
516
517There are two ways in which a data value can be input:
518
519 * If the text contains no white space, it can be typed verbatim. However, if
520 it is part of a condition, it must also be free of round brackets
521 (parentheses), as these are used for grouping in conditions.
522
523 * Otherwise, text must be enclosed in double quotation marks. In this case,
524 the character \ (backslash) is treated as an "escape character" within the
525 string, causing the following character or characters to be treated
526 specially:
527
528 \n is replaced by a newline
529 \r is replaced by a carriage return
530 \t is replaced by a tab
531
532Backslash followed by up to three octal digits is replaced by the character
533specified by those digits, and "\x" followed by up to two hexadecimal digits is
534treated similarly. Backslash followed by any other character is replaced by the
535second character, so that in particular, "\"" becomes """ and "\\" becomes "\".
536A data item enclosed in double quotes can be continued onto the next line by
537ending the first line with a backslash. Any leading white space at the start of
538the continuation line is ignored.
539
540In addition to the escape character processing that occurs when strings are
541enclosed in quotes, most data values are also subject to string expansion (as
542described in the next section), in which case the characters "$" and "\" are
543also significant. This means that if a single backslash is actually required in
544such a string, and the string is also quoted, "\\\\" has to be entered.
545
546The maximum permitted length of a data string, before expansion, is 1024
547characters.
548
549
5503.3 String expansion
551--------------------
552
553Most data values are expanded before use. Expansion consists of replacing
554substrings beginning with "$" with other text. The full expansion facilities
555available in Exim are extensive. If you want to know everything that Exim can
556do with strings, you should consult the chapter on string expansion in the Exim
557documentation.
558
559In filter files, by far the most common use of string expansion is the
560substitution of the contents of a variable. For example, the substring
561
562$reply_address
563
564is replaced by the address to which replies to the message should be sent. If
565such a variable name is followed by a letter or digit or underscore, it must be
566enclosed in curly brackets (braces), for example,
567
568${reply_address}
569
570If a "$" character is actually required in an expanded string, it must be
571escaped with a backslash, and because backslash is also an escape character in
572quoted input strings, it must be doubled in that case. The following two
573examples illustrate two different ways of testing for a "$" character in a
574message:
575
576if $message_body contains \$ then ...
577if $message_body contains "\\$" then ...
578
579You can prevent part of a string from being expanded by enclosing it between
580two occurrences of "\N". For example,
581
582if $message_body contains \N$$$$\N then ...
583
584tests for a run of four dollar characters.
585
586
5873.4 Some useful general variables
588---------------------------------
589
590A complete list of the available variables is given in the Exim documentation.
591This shortened list contains the ones that are most likely to be useful in
592personal filter files:
593
594$body_linecount: The number of lines in the body of the message.
595
596$body_zerocount: The number of binary zero characters in the body of the
597message.
598
599$home: In conventional configurations, this variable normally contains the
600user's home directory. The system administrator can, however, change this.
601
602$local_part: The part of the email address that precedes the @ sign - normally
603the user's login name. If support for multiple personal mailboxes is enabled
604(see section 3.31 below) and a prefix or suffix for the local part was
605recognized, it is removed from the string in this variable.
606
607$local_part_prefix: If support for multiple personal mailboxes is enabled (see
608section 3.31 below), and a local part prefix was recognized, this variable
609contains the prefix. Otherwise it contains an empty string.
610
611$local_part_suffix: If support for multiple personal mailboxes is enabled (see
612section 3.31 below), and a local part suffix was recognized, this variable
613contains the suffix. Otherwise it contains an empty string.
614
615$message_body: The initial portion of the body of the message. By default, up
616to 500 characters are read into this variable, but the system administrator can
617configure this to some other value. Newlines in the body are converted into
618single spaces.
619
620$message_body_end: The final portion of the body of the message, formatted and
621limited in the same way as $message_body.
622
623$message_body_size: The size of the body of the message, in bytes.
624
625$message_exim_id: The message's local identification string, which is unique
626for each message handled by a single host.
627
628$message_headers: The header lines of the message, concatenated into a single
629string, with newline characters between them.
630
631$message_size: The size of the entire message, in bytes.
632
633$original_local_part: When an address that arrived with the message is being
634processed, this contains the same value as the variable $local_part. However,
635if an address generated by an alias, forward, or filter file is being
636processed, this variable contains the local part of the original address.
637
638$reply_address: The contents of the Reply-to: header, if the message has one;
639otherwise the contents of the From: header. It is the address to which normal
640replies to the message should be sent.
641
642$return_path: The return path - that is, the sender field that will be
643transmitted as part of the message's envelope if the message is sent to another
644host. This is the address to which delivery errors are sent. In many cases,
645this variable has the same value as $sender_address, but if, for example, an
646incoming message to a mailing list has been expanded, $return_path may have
647been changed to contain the address of the list maintainer.
648
649$sender_address: The sender address that was received in the envelope of the
650message. This is not necessarily the same as the contents of the From: or
651Sender: header lines. For delivery error messages ("bounce messages") there is
652no sender address, and this variable is empty.
653
654$tod_full: A full version of the time and date, for example: Wed, 18 Oct 1995
65509:51:40 +0100. The timezone is always given as a numerical offset from GMT.
656
657$tod_log: The time and date in the format used for writing Exim's log files,
658without the timezone, for example: 1995-10-12 15:32:29.
659
660$tod_zone: The local timezone offset, for example: +0100.
661
662
6633.5 Header variables
664--------------------
665
666There is a special set of expansion variables containing the header lines of
667the message being processed. These variables have names beginning with $header_
668followed by the name of the header line, terminated by a colon. For example,
669
670$header_from:
671$header_subject:
672
673The whole item, including the terminating colon, is replaced by the contents of
674the message header line. If there is more than one header line with the same
675name, their contents are concatenated. For header lines whose data consists of
676a list of addresses (for example, From: and To:), a comma and newline is
677inserted between each set of data. For all other header lines, just a newline
678is used.
679
680Leading and trailing white space is removed from header line data, and if there
681are any MIME "words" that are encoded as defined by RFC 2047 (because they
682contain non-ASCII characters), they are decoded and translated, if possible, to
683a local character set. Translation is attempted only on operating systems that
684have the iconv() function. This makes the header line look the same as it would
685when displayed by an MUA. The default character set is ISO-8859-1, but this can
686be changed by means of the headers command (see below).
687
688If you want to see the actual characters that make up a header line, you can
689specify $rheader_ instead of $header_. This inserts the "raw" header line,
690unmodified.
691
692There is also an intermediate form, requested by $bheader_, which removes
693leading and trailing space and decodes MIME "words", but does not do any
694character translation. If an attempt to decode what looks superficially like a
695MIME "word" fails, the raw string is returned. If decoding produces a binary
696zero character, it is replaced by a question mark.
697
698The capitalization of the name following $header_ is not significant. Because
699any printing character except colon may appear in the name of a message's
700header (this is a requirement of RFC 2822, the document that describes the
701format of a mail message) curly brackets must not be used in this case, as they
702will be taken as part of the header name. Two shortcuts are allowed in naming
703header variables:
704
705 * The initiating $header_, $rheader_, or $bheader_ can be abbreviated to $h_,
706 $rh_, or $bh_, respectively.
707
708 * The terminating colon can be omitted if the next character is white space.
709 The white space character is retained in the expanded string. However, this
710 is not recommended, because it makes it easy to forget the colon when it
711 really is needed.
712
713If the message does not contain a header of the given name, an empty string is
714substituted. Thus it is important to spell the names of headers correctly. Do
715not use $header_Reply_to when you really mean $header_Reply-to.
716
717
7183.6 User variables
719------------------
720
721There are ten user variables with names $n0 - $n9 that can be incremented by
722the add command (see section 3.10). These can be used for "scoring" messages in
723various ways. If Exim is configured to run a "system filter" on every message,
724the values left in these variables are copied into the variables $sn0 - $sn9 at
725the end of the system filter, thus making them available to users' filter
726files. How these values are used is entirely up to the individual installation.
727
728
7293.7 Current directory
730---------------------
731
732The contents of your filter file should not make any assumptions about the
733current directory. It is best to use absolute paths for file names; you can
734normally make use of the $home variable to refer to your home directory. The
735save command automatically inserts $home at the start of non-absolute paths.
736
737
7383.8 Significant deliveries
739--------------------------
740
741When in the course of delivery a message is processed by a filter file, what
742happens next, that is, after the filter file has been processed, depends on
743whether or not the filter sets up any significant deliveries. If at least one
744significant delivery is set up, the filter is considered to have handled the
745entire delivery arrangements for the current address, and no further processing
746of the address takes place. If, however, no significant deliveries are set up,
747Exim continues processing the current address as if there were no filter file,
748and typically sets up a delivery of a copy of the message into a local mailbox.
749In particular, this happens in the special case of a filter file containing
750only comments.
751
752The delivery commands deliver, save, and pipe are by default significant.
753However, if such a command is preceded by the word "unseen", its delivery is
754not considered to be significant. In contrast, other commands such as mail and
755vacation do not set up significant deliveries unless preceded by the word
756"seen". The following example commands set up significant deliveries:
757
758deliver jack@beanstalk.example
759pipe $home/bin/mymailscript
760seen mail subject "message discarded"
761seen finish
762
763The following example commands do not set up significant deliveries:
764
765unseen deliver jack@beanstalk.example
766unseen pipe $home/bin/mymailscript
767mail subject "message discarded"
768finish
769
770
7713.9 Filter commands
772-------------------
773
774The filter commands that are described in subsequent sections are listed below,
775with the section in which they are described in brackets:
776
777add increment a user variable (section 3.10)
778deliver deliver to an email address (section 3.11)
779fail force delivery failure (sysadmin use) (section 3.18)
780finish end processing (section 3.16)
781freeze freeze message (sysadmin use) (section 3.19)
782headers set the header character set (section 3.20)
783if test condition(s) (section 3.21)
784logfile define log file (section 3.15)
785logwrite write to log file (section 3.15)
786mail send a reply message (section 3.14)
787pipe pipe to a command (section 3.13)
788save save to a file (section 3.12)
789testprint print while testing (section 3.17)
790vacation tailored form of mail (section 3.14)
791
792The headers command has additional parameters that can be used only in a system
793filter. The fail and freeze commands are available only when Exim's filtering
794facilities are being used as a system filter, and are therefore usable only by
795the system administrator and not by ordinary users. They are mentioned only
796briefly in this document; for more information, see the main Exim
797specification.
798
799
8003.10 The add command
801--------------------
802
803 add <number> to <user variable>
804e.g. add 2 to n3
805
806There are 10 user variables of this type, with names $n0 - $n9. Their values
807can be obtained by the normal expansion syntax (for example $n3) in other
808commands. At the start of filtering, these variables all contain zero. Both
809arguments of the add command are expanded before use, making it possible to add
810variables to each other. Subtraction can be obtained by adding negative
811numbers.
812
813
8143.11 The deliver command
815------------------------
816
817 deliver <mail address>
818e.g. deliver "Dr Livingstone <David@somewhere.africa.example>"
819
820This command provides a forwarding operation. The delivery that it sets up is
821significant unless the command is preceded by "unseen" (see section 3.8). The
822message is sent on to the given address, exactly as happens if the address had
823appeared in a traditional .forward file. If you want to deliver the message to
824a number of different addresses, you can use more than one deliver command
825(each one may have only one address). However, duplicate addresses are
826discarded.
827
828To deliver a copy of the message to your normal mailbox, your login name can be
829given as the address. Once an address has been processed by the filtering
830mechanism, an identical generated address will not be so processed again, so
831doing this does not cause a loop.
832
833However, if you have a mail alias, you should not refer to it here. For
834example, if the mail address L.Gulliver is aliased to lg303 then all references
835in Gulliver's .forward file should be to lg303. A reference to the alias will
836not work for messages that are addressed to that alias, since, like .forward
837file processing, aliasing is performed only once on an address, in order to
838avoid looping.
839
840Following the new address, an optional second address, preceded by "errors_to"
841may appear. This changes the address to which delivery errors on the forwarded
842message will be sent. Instead of going to the message's original sender, they
843go to this new address. For ordinary users, the only value that is permitted
844for this address is the user whose filter file is being processed. For example,
845the user lg303 whose mailbox is in the domain lilliput.example could have a
846filter file that contains
847
848deliver jon@elsewhere.example errors_to lg303@lilliput.example
849
850Clearly, using this feature makes sense only in situations where not all
851messages are being forwarded. In particular, bounce messages must not be
852forwarded in this way, as this is likely to create a mail loop if something
853goes wrong.
854
855
8563.12 The save command
857---------------------
858
859 save <file name>
860e.g. save $home/mail/bookfolder
861
862This command specifies that a copy of the message is to be appended to the
863given file (that is, the file is to be used as a mail folder). The delivery
864that save sets up is significant unless the command is preceded by "unseen"
865(see section 3.8).
866
867More than one save command may be obeyed; each one causes a copy of the message
868to be written to its argument file, provided they are different (duplicate save
869commands are ignored).
870
871If the file name does not start with a / character, the contents of the $home
872variable are prepended, unless it is empty, or the system administrator has
873disabled this feature. In conventional configurations, this variable is
874normally set in a user filter to the user's home directory, but the system
875administrator may set it to some other path. In some configurations, $home may
876be unset, or prepending may be disabled, in which case a non-absolute path name
877may be generated. Such configurations convert this to an absolute path when the
878delivery takes place. In a system filter, $home is never set.
879
880The user must of course have permission to write to the file, and the writing
881of the file takes place in a process that is running as the user, under the
882user's primary group. Any secondary groups to which the user may belong are not
883normally taken into account, though the system administrator can configure Exim
884to set them up. In addition, the ability to use this command at all is
885controlled by the system administrator - it may be forbidden on some systems.
886
887An optional mode value may be given after the file name. The value for the mode
888is interpreted as an octal number, even if it does not begin with a zero. For
889example:
890
891save /some/folder 640
892
893This makes it possible for users to override the system-wide mode setting for
894file deliveries, which is normally 600. If an existing file does not have the
895correct mode, it is changed.
896
897An alternative form of delivery may be enabled on your system, in which each
898message is delivered into a new file in a given directory. If this is the case,
899this functionality can be requested by giving the directory name terminated by
900a slash after the save command, for example
901
902save separated/messages/
903
904There are several different formats for such deliveries; check with your system
905administrator or local documentation to find out which (if any) are available
906on your system. If this functionality is not enabled, the use of a path name
907ending in a slash causes an error.
908
909
9103.13 The pipe command
911---------------------
912
913 pipe <command>
914e.g. pipe "$home/bin/countmail $sender_address"
915
916This command specifies that the message is to be delivered to the specified
917command using a pipe. The delivery that it sets up is significant unless the
918command is preceded by "unseen" (see section 3.8). Remember, however, that no
919deliveries are done while the filter is being processed. All deliveries happen
920later on. Therefore, the result of running the pipe is not available to the
921filter.
922
923When the deliveries are done, a separate process is run, and a copy of the
924message is passed on its standard input. The process runs as the user, under
925the user's primary group. Any secondary groups to which the user may belong are
926not normally taken into account, though the system administrator can configure
927Exim to set them up. More than one pipe command may appear; each one causes a
928copy of the message to be written to its argument pipe, provided they are
929different (duplicate pipe commands are ignored).
930
931When the time comes to transport the message, the command supplied to pipe is
932split up by Exim into a command name and a number of arguments. These are
933delimited by white space except for arguments enclosed in double quotes, in
934which case backslash is interpreted as an escape, or in single quotes, in which
935case no escaping is recognized. Note that as the whole command is normally
936supplied in double quotes, a second level of quoting is required for internal
937double quotes. For example:
938
939pipe "$home/myscript \"size is $message_size\""
940
941String expansion is performed on the separate components after the line has
942been split up, and the command is then run directly by Exim; it is not run
943under a shell. Therefore, substitution cannot change the number of arguments,
944nor can quotes, backslashes or other shell metacharacters in variables cause
945confusion.
946
947Documentation for some programs that are normally run via this kind of pipe
948often suggest that the command should start with
949
950IFS=" "
951
952This is a shell command, and should not be present in Exim filter files, since
953it does not normally run the command under a shell.
954
955However, there is an option that the administrator can set to cause a shell to
956be used. In this case, the entire command is expanded as a single string and
957passed to the shell for interpretation. It is recommended that this be avoided
958if at all possible, since it can lead to problems when inserted variables
959contain shell metacharacters.
960
961The default PATH set up for the command is determined by the system
962administrator, usually containing at least /bin and /usr/bin so that common
963commands are available without having to specify an absolute file name.
964However, it is possible for the system administrator to restrict the pipe
965facility so that the command name must not contain any / characters, and must
966be found in one of the directories in the configured PATH. It is also possible
967for the system administrator to lock out the use of the pipe command
968altogether.
969
970When the command is run, a number of environment variables are set up. The
971complete list for pipe deliveries may be found in the Exim reference manual.
972Those that may be useful for pipe deliveries from user filter files are:
973
974DOMAIN the domain of the address
975HOME your home directory
976LOCAL_PART see below
977LOCAL_PART_PREFIX see below
978LOCAL_PART_SUFFIX see below
979LOGNAME your login name
980MESSAGE_ID the unique id of the message
981PATH the command search path
982RECIPIENT the complete recipient address
983SENDER the sender of the message
984SHELL /bin/sh
985USER see below
986
987LOCAL_PART, LOGNAME, and USER are all set to the same value, namely, your login
988id. LOCAL_PART_PREFIX and LOCAL_PART_SUFFIX may be set if Exim is configured to
989recognize prefixes or suffixes in the local parts of addresses. For example, a
990message addressed to pat-suf2@domain.example may cause the filter for user pat
991to be run. If this sets up a pipe delivery, LOCAL_PART_SUFFIX is "-suf2" when
992the pipe command runs. The system administrator has to configure Exim specially
993for this feature to be available.
994
995If you run a command that is a shell script, be very careful in your use of
996data from the incoming message in the commands in your script. RFC 2822 is very
997generous in the characters that are permitted to appear in mail addresses, and
998in particular, an address may begin with a vertical bar or a slash. For this
999reason you should always use quotes round any arguments that involve data from
1000the message, like this:
1001
1002/some/command '$SENDER'
1003
1004so that inserted shell meta-characters do not cause unwanted effects.
1005
1006Remember that, as was explained earlier, the pipe command is not run at the
1007time the filter file is interpreted. The filter just defines what deliveries
1008are required for one particular addressee of a message. The deliveries
1009themselves happen later, once Exim has decided everything that needs to be done
1010for the message.
1011
1012A consequence of this is that you cannot inspect the return code from the pipe
1013command from within the filter. Nevertheless, the code returned by the command
1014is important, because Exim uses it to decide whether the delivery has succeeded
1015or failed.
1016
1017The command should return a zero completion code if all has gone well. Most
1018non-zero codes are treated by Exim as indicating a failure of the pipe. This is
1019treated as a delivery failure, causing the message to be returned to its
1020sender. However, there are some completion codes that are treated as temporary
1021errors. The message remains on Exim's spool disk, and the delivery is tried
1022again later, though it will ultimately time out if the delivery failures go on
1023too long. The completion codes to which this applies can be specified by the
1024system administrator; the default values are 73 and 75.
1025
1026The pipe command should not normally write anything to its standard output or
1027standard error file descriptors. If it does, whatever is written is normally
1028returned to the sender of the message as a delivery error, though this action
1029can be varied by the system administrator.
1030
1031
10323.14 Mail commands
1033------------------
1034
1035There are two commands that cause the creation of a new mail message, neither
1036of which count as a significant delivery unless the command is preceded by the
1037word "seen" (see section 3.8). This is a powerful facility, but it should be
1038used with care, because of the danger of creating infinite sequences of
1039messages. The system administrator can forbid the use of these commands
1040altogether.
1041
1042To help prevent runaway message sequences, these commands have no effect when
1043the incoming message is a bounce (delivery error) message, and messages sent by
1044this means are treated as if they were reporting delivery errors. Thus, they
1045should never themselves cause a bounce message to be returned. The basic
1046mail-sending command is
1047
1048mail [to <address-list>]
1049 [cc <address-list>]
1050 [bcc <address-list>]
1051 [from <address>]
1052 [reply_to <address>]
1053 [subject <text>]
1054 [extra_headers <text>]
1055 [text <text>]
1056 [[expand] file <filename>]
1057 [return message]
1058 [log <log file name>]
1059 [once <note file name>]
1060 [once_repeat <time interval>]
1061e.g. mail text "Your message about $h_subject: has been received"
1062
1063Each <address-list> can contain a number of addresses, separated by commas, in
1064the format of a To: or Cc: header line. In fact, the text you supply here is
1065copied exactly into the appropriate header line. It may contain additional
1066information as well as email addresses. For example:
1067
1068mail to "Julius Caesar <jc@rome.example>, \
1069 <ma@rome.example> (Mark A.)"
1070
1071Similarly, the texts supplied for from and reply_to are copied into their
1072respective header lines.
1073
1074As a convenience for use in one common case, there is also a command called
1075vacation. It behaves in the same way as mail, except that the defaults for the
1076subject, file, log, once, and once_repeat options are
1077
1078subject "On vacation"
1079expand file .vacation.msg
1080log .vacation.log
1081once .vacation
1082once_repeat 7d
1083
1084respectively. These are the same file names and repeat period used by the
1085traditional Unix vacation command. The defaults can be overridden by explicit
1086settings, but if a file name is given its contents are expanded only if
1087explicitly requested.
1088
1089Warning: The vacation command should always be used conditionally, subject to
1090at least the personal condition (see section 3.27 below) so as not to send
1091automatic replies to non-personal messages from mailing lists or elsewhere.
1092Sending an automatic response to a mailing list or a mailing list manager is an
1093Internet Sin.
1094
1095For both commands, the key/value argument pairs can appear in any order. At
1096least one of text or file must appear (except with vacation, where there is a
1097default for file); if both are present, the text string appears first in the
1098message. If expand precedes file, each line of the file is subject to string
1099expansion before it is included in the message.
1100
1101Several lines of text can be supplied to text by including the escape sequence
1102"\n" in the string wherever a newline is required. If the command is output
1103during filter file testing, newlines in the text are shown as "\n".
1104
1105Note that the keyword for creating a Reply-To: header is reply_to, because Exim
1106keywords may contain underscores, but not hyphens. If the from keyword is
1107present and the given address does not match the user who owns the forward
1108file, Exim normally adds a Sender: header to the message, though it can be
1109configured not to do this.
1110
1111The extra_headers keyword allows you to add custom header lines to the message.
1112The text supplied must be one or more syntactically valid RFC 2822 header
1113lines. You can use "\n" within quoted text to specify newlines between headers,
1114and also to define continued header lines. For example:
1115
1116extra_headers "h1: first\nh2: second\n continued\nh3: third"
1117
1118No newline should appear at the end of the final header line.
1119
1120If no to argument appears, the message is sent to the address in the
1121$reply_address variable (see section 3.3 above). An In-Reply-To: header is
1122automatically included in the created message, giving a reference to the
1123message identification of the incoming message.
1124
1125If return message is specified, the incoming message that caused the filter
1126file to be run is added to the end of the message, subject to a maximum size
1127limitation.
1128
1129If a log file is specified, a line is added to it for each message sent.
1130
1131If a once file is specified, it is used to hold a database for remembering who
1132has received a message, and no more than one message is ever sent to any
1133particular address, unless once_repeat is set. This specifies a time interval
1134after which another copy of the message is sent. The interval is specified as a
1135sequence of numbers, each followed by the initial letter of one of "seconds",
1136"minutes", "hours", "days", or "weeks". For example,
1137
1138once_repeat 5d4h
1139
1140causes a new message to be sent if at least 5 days and 4 hours have elapsed
1141since the last one was sent. There must be no white space in a time interval.
1142
1143Commonly, the file name specified for once is used as the base name for
1144direct-access (DBM) file operations. There are a number of different DBM
1145libraries in existence. Some operating systems provide one as a default, but
1146even in this case a different one may have been used when building Exim. With
1147some DBM libraries, specifying once results in two files being created, with
1148the suffixes .dir and .pag being added to the given name. With some others a
1149single file with the suffix .db is used, or the name is used unchanged.
1150
1151Using a DBM file for implementing the once feature means that the file grows as
1152large as necessary. This is not usually a problem, but some system
1153administrators want to put a limit on it. The facility can be configured not to
1154use a DBM file, but instead, to use a regular file with a maximum size. The
1155data in such a file is searched sequentially, and if the file fills up, the
1156oldest entry is deleted to make way for a new one. This means that some
1157correspondents may receive a second copy of the message after an unpredictable
1158interval. Consult your local information to see if your system is configured
1159this way.
1160
1161More than one mail or vacation command may be obeyed in a single filter run;
1162they are all honoured, even when they are to the same recipient.
1163
1164
11653.15 Logging commands
1166---------------------
1167
1168A log can be kept of actions taken by a filter file. This facility is normally
1169available in conventional configurations, but there are some situations where
1170it might not be. Also, the system administrator may choose to disable it. Check
1171your local information if in doubt.
1172
1173Logging takes place while the filter file is being interpreted. It does not
1174queue up for later like the delivery commands. The reason for this is so that a
1175log file need be opened only once for several write operations. There are two
1176commands, neither of which constitutes a significant delivery. The first
1177defines a file to which logging output is subsequently written:
1178
1179 logfile <file name>
1180e.g. logfile $home/filter.log
1181
1182The file name must be fully qualified. You can use $home, as in this example,
1183to refer to your home directory. The file name may optionally be followed by a
1184mode for the file, which is used if the file has to be created. For example,
1185
1186logfile $home/filter.log 0644
1187
1188The number is interpreted as octal, even if it does not begin with a zero. The
1189default for the mode is 600. It is suggested that the logfile command normally
1190appear as the first command in a filter file. Once a log file has been obeyed,
1191the logwrite command can be used to write to it:
1192
1193 logwrite "<some text string>"
1194e.g. logwrite "$tod_log $message_id processed"
1195
1196It is possible to have more than one logfile command, to specify writing to
1197different log files in different circumstances. Writing takes place at the end
1198of the file, and a newline character is added to the end of each string if
1199there isn't one already there. Newlines can be put in the middle of the string
1200by using the "\n" escape sequence. Lines from simultaneous deliveries may get
1201interleaved in the file, as there is no interlocking, so you should plan your
1202logging with this in mind. However, data should not get lost.
1203
1204
12053.16 The finish command
1206-----------------------
1207
1208The command finish, which has no arguments, causes Exim to stop interpreting
1209the filter file. This is not a significant action unless preceded by "seen". A
1210filter file containing only "seen finish" is a black hole.
1211
1212
12133.17 The testprint command
1214--------------------------
1215
1216It is sometimes helpful to be able to print out the values of variables when
1217testing filter files. The command
1218
1219 testprint <text>
1220e.g. testprint "home=$home reply_address=$reply_address"
1221
1222does nothing when mail is being delivered. However, when the filtering code is
1223being tested by means of the -bf option (see section 1.3 above), the value of
1224the string is written to the standard output.
1225
1226
12273.18 The fail command
1228---------------------
1229
1230When Exim's filtering facilities are being used as a system filter, the fail
1231command is available, to force delivery failure. Because this command is
1232normally usable only by the system administrator, and not enabled for use by
1233ordinary users, it is described in more detail in the main Exim specification
1234rather than in this document.
1235
1236
12373.19 The freeze command
1238-----------------------
1239
1240When Exim's filtering facilities are being used as a system filter, the freeze
1241command is available, to freeze a message on the queue. Because this command is
1242normally usable only by the system administrator, and not enabled for use by
1243ordinary users, it is described in more detail in the main Exim specification
1244rather than in this document.
1245
1246
12473.20 The headers command
1248------------------------
1249
1250The headers command can be used to change the target character set that is used
1251when translating the contents of encoded header lines for insertion by the
1252$header_ mechanism (see section 3.5 above). The default can be set in the Exim
1253configuration; if not specified, ISO-8859-1 is used. The only currently
1254supported format for the headers command in user filters is as in this example:
1255
1256headers charset "UTF-8"
1257
1258That is, headers is followed by the word "charset" and then the name of a
1259character set. This particular example would be useful if you wanted to compare
1260the contents of a header to a UTF-8 string.
1261
1262In system filter files, the headers command can be used to add or remove header
1263lines from the message. These features are described in the main Exim
1264specification.
1265
1266
12673.21 Obeying commands conditionally
1268-----------------------------------
1269
1270Most of the power of filtering comes from the ability to test conditions and
1271obey different commands depending on the outcome. The if command is used to
1272specify conditional execution, and its general form is
1273
1274if <condition>
1275then <commands>
1276elif <condition>
1277then <commands>
1278else <commands>
1279endif
1280
1281There may be any number of elif and then sections (including none) and the else
1282section is also optional. Any number of commands, including nested if commands,
1283may appear in any of the <commands> sections.
1284
1285Conditions can be combined by using the words and and or, and round brackets
1286(parentheses) can be used to specify how several conditions are to combine.
1287Without brackets, and is more binding than or. For example:
1288
1289if
1290$h_subject: contains "Make money" or
1291$h_precedence: is "junk" or
1292($h_sender: matches ^\\d{8}@ and not personal) or
1293$message_body contains "this is not spam"
1294then
1295seen finish
1296endif
1297
1298A condition can be preceded by not to negate it, and there are also some
1299negative forms of condition that are more English-like.
1300
1301
13023.22 String testing conditions
1303------------------------------
1304
1305There are a number of conditions that operate on text strings, using the words
1306"begins", "ends", "is", "contains" and "matches". If you want to apply the same
1307test to more than one header line, you can easily concatenate them into a
1308single string for testing, as in this example:
1309
1310if "$h_to:, $h_cc:" contains me@domain.example then ...
1311
1312If a string-testing condition name is written in lower case, the testing of
1313letters is done without regard to case; if it is written in upper case (for
1314example, "CONTAINS"), the case of letters is taken into account.
1315
1316 <text1> begins <text2>
1317 <text1> does not begin <text2>
1318e.g. $header_from: begins "Friend@"
1319
1320A "begins" test checks for the presence of the second string at the start of
1321the first, both strings having been expanded.
1322
1323 <text1> ends <text2>
1324 <text1> does not end <text2>
1325e.g. $header_from: ends "public.com.example"
1326
1327An "ends" test checks for the presence of the second string at the end of the
1328first, both strings having been expanded.
1329
1330 <text1> is <text2>
1331 <text1> is not <text2>
1332e.g. $local_part_suffix is "-foo"
1333
1334An "is" test does an exact match between the strings, having first expanded
1335both strings.
1336
1337 <text1> contains <text2>
1338 <text1> does not contain <text2>
1339e.g. $header_subject: contains "evolution"
1340
1341A "contains" test does a partial string match, having expanded both strings.
1342
1343 <text1> matches <text2>
1344 <text1> does not match <text2>
1345e.g. $sender_address matches "(bill|john)@"
1346
1347For a "matches" test, after expansion of both strings, the second one is
1348interpreted as a regular expression. Exim uses the PCRE regular expression
1349library, which provides regular expressions that are compatible with Perl.
1350
1351The match succeeds if the regular expression matches any part of the first
1352string. If you want a regular expression to match only at the start or end of
1353the subject string, you must encode that requirement explicitly, using the "^"
1354or "$" metacharacters. The above example, which is not so constrained, matches
1355all these addresses:
1356
1357bill@test.example
1358john@some.example
1359spoonbill@example.com
1360littlejohn@example.com
1361
1362To match only the first two, you could use this:
1363
1364if $sender_address matches "^(bill|john)@" then ...
1365
1366Care must be taken if you need a backslash in a regular expression, because
1367backslashes are interpreted as escape characters both by the string expansion
1368code and by Exim's normal processing of strings in quotes. For example, if you
1369want to test the sender address for a domain ending in .com the regular
1370expression is
1371
1372\.com$
1373
1374The backslash and dollar sign in that expression have to be escaped when used
1375in a filter command, as otherwise they would be interpreted by the expansion
1376code. Thus, what you actually write is
1377
1378if $sender_address matches \\.com\$
1379
1380An alternative way of handling this is to make use of the "\N" expansion flag
1381for suppressing expansion:
1382
1383if $sender_address matches \N\.com$\N
1384
1385Everything between the two occurrences of "\N" is copied without change by the
1386string expander (and in fact you do not need the final one, because it is at
1387the end of the string). If the regular expression is given in quotes (mandatory
1388only if it contains white space) you have to write either
1389
1390if $sender_address matches "\\\\.com\\$"
1391
1392or
1393
1394if $sender_address matches "\\N\\.com$\\N"
1395
1396If the regular expression contains bracketed sub-expressions, numeric variable
1397substitutions such as $1 can be used in the subsequent actions after a
1398successful match. If the match fails, the values of the numeric variables
1399remain unchanged. Previous values are not restored after endif. In other words,
1400only one set of values is ever available. If the condition contains several
1401sub-conditions connected by and or or, it is the strings extracted from the
1402last successful match that are available in subsequent actions. Numeric
1403variables from any one sub-condition are also available for use in subsequent
1404sub-conditions, because string expansion of a condition occurs just before it
1405is tested.
1406
1407
14083.23 Numeric testing conditions
1409-------------------------------
1410
1411The following conditions are available for performing numerical tests:
1412
1413 <number1> is above <number2>
1414 <number1> is not above <number2>
1415 <number1> is below <number2>
1416 <number1> is not below <number2>
1417e.g. $message_size is not above 10k
1418
1419The <number> arguments must expand to strings of digits, optionally followed by
1420one of the letters K or M (upper case or lower case) which cause multiplication
1421by 1024 and 1024x1024 respectively.
1422
1423
14243.24 Testing for significant deliveries
1425---------------------------------------
1426
1427You can use the delivered condition to test whether or not any previously
1428obeyed filter commands have set up a significant delivery. For example:
1429
1430if not delivered then save mail/anomalous endif
1431
1432"Delivered" is perhaps a poor choice of name for this condition, because the
1433message has not actually been delivered; rather, a delivery has been set up for
1434later processing.
1435
1436
14373.25 Testing for error messages
1438-------------------------------
1439
1440The condition error_message is true if the incoming message is a bounce (mail
1441delivery error) message. Putting the command
1442
1443if error_message then finish endif
1444
1445at the head of your filter file is a useful insurance against things going
1446wrong in such a way that you cannot receive delivery error reports. Note:
1447error_message is a condition, not an expansion variable, and therefore is not
1448preceded by "$".
1449
1450
14513.26 Testing a list of addresses
1452--------------------------------
1453
1454There is a facility for looping through a list of addresses and applying a
1455condition to each of them. It takes the form
1456
1457foranyaddress <string> (<condition>)
1458
1459where <string> is interpreted as a list of RFC 2822 addresses, as in a typical
1460header line, and <condition> is any valid filter condition or combination of
1461conditions. The "group" syntax that is defined for certain header lines that
1462contain addresses is supported.
1463
1464The parentheses surrounding the condition are mandatory, to delimit it from
1465possible further sub-conditions of the enclosing if command. Within the
1466condition, the expansion variable $thisaddress is set to the non-comment
1467portion of each of the addresses in the string in turn. For example, if the
1468string is
1469
1470B.Simpson <bart@sfld.example>, lisa@sfld.example (his sister)
1471
1472then $thisaddress would take on the values "bart@sfld.example" and
1473"lisa@sfld.example" in turn.
1474
1475If there are no valid addresses in the list, the whole condition is false. If
1476the internal condition is true for any one address, the overall condition is
1477true and the loop ends. If the internal condition is false for all addresses in
1478the list, the overall condition is false. This example tests for the presence
1479of an eight-digit local part in any address in a To: header:
1480
1481if foranyaddress $h_to: ( $thisaddress matches ^\\d{8}@ ) then ...
1482
1483When the overall condition is true, the value of $thisaddress in the commands
1484that follow then is the last value it took on inside the loop. At the end of
1485the if command, the value of $thisaddress is reset to what it was before. It is
1486best to avoid the use of multiple occurrences of foranyaddress, nested or
1487otherwise, in a single if command, if the value of $thisaddress is to be used
1488afterwards, because it isn't always clear what the value will be. Nested if
1489commands should be used instead.
1490
1491Header lines can be joined together if a check is to be applied to more than
1492one of them. For example:
1493
1494if foranyaddress $h_to:,$h_cc: ....
1495
1496This scans through the addresses in both the To: and the Cc: headers.
1497
1498
14993.27 Testing for personal mail
1500------------------------------
1501
1502A common requirement is to distinguish between incoming personal mail and mail
1503from a mailing list, or from a robot or other automatic process (for example, a
1504bounce message). In particular, this test is normally required for "vacation
1505messages".
1506
1507The personal condition checks that the message is not a bounce message and that
1508the current user's email address appears in the To: header. It also checks that
1509the sender is not the current user or one of a number of common daemons, and
1510that there are no header lines starting List- in the message. Finally, it
1511checks the content of the Precedence: header line, if there is one.
1512
1513You should always use the personal condition when generating automatic
1514responses. This example shows the use of personal in a filter file that is
1515sending out vacation messages:
1516
1517if personal then
1518mail to $reply_address
1519subject "I am on holiday"
1520file $home/vacation/message
1521once $home/vacation/once
1522once_repeat 10d
1523endif
1524
1525It is tempting, when writing commands like the above, to quote the original
1526subject in the reply. For example:
1527
1528subject "Re: $h_subject:"
1529
1530There is a danger in doing this, however. It may allow a third party to
1531subscribe you to an opt-in mailing list, provided that the list accepts bounce
1532messages as subscription confirmations. (Messages sent from filters are always
1533sent as bounce messages.) Well-managed lists require a non-bounce message to
1534confirm a subscription, so the danger is relatively small.
1535
1536If prefixes or suffixes are in use for local parts - something which depends on
1537the configuration of Exim (see section 3.31 below) - the tests for the current
1538user are done with the full address (including the prefix and suffix, if any)
1539as well as with the prefix and suffix removed. If the system is configured to
1540rewrite local parts of mail addresses, for example, to rewrite "dag46" as
1541"Dirk.Gently", the rewritten form of the address is also used in the tests.
1542
1543
15443.28 Alias addresses for the personal condition
1545-----------------------------------------------
1546
1547It is quite common for people who have mail accounts on a number of different
1548systems to forward all their mail to one system, and in this case a check for
1549personal mail should test all their various mail addresses. To allow for this,
1550the personal condition keyword can be followed by
1551
1552alias <address>
1553
1554any number of times, for example:
1555
1556if personal alias smith@else.where.example
1557 alias jones@other.place.example
1558then ...
1559
1560The alias addresses are treated as alternatives to the current user's email
1561address when testing the contents of header lines.
1562
1563
15643.29 Details of the personal condition
1565--------------------------------------
1566
1567The basic personal test is roughly equivalent to the following:
1568
1569not error_message and
1570$message_headers does not contain "\nList-Id:" and
1571$message_headers does not contain "\nList-Help:" and
1572$message_headers does not contain "\nList-Subscribe:" and
1573$message_headers does not contain "\nList-Unsubscribe:" and
1574$message_headers does not contain "\nList-Post:" and
1575$message_headers does not contain "\nList-Owner:" and
1576$message_headers does not contain "\nList-Archive:" and
1577(
1578"${if def:h_auto-submitted:{present}{absent}}" is "absent" or
1579$header_auto-submitted: is "no"
1580) and
1581$header_precedence: does not contain "bulk" and
1582$header_precedence: does not contain "list" and
1583$header_precedence: does not contain "junk" and
1584foranyaddress $header_to:
1585( $thisaddress contains "$local_part$domain" ) and
1586not foranyaddress $header_from:
1587(
1588$thisaddress contains "$local_part@$domain" or
1589$thisaddress contains "server@" or
1590$thisaddress contains "daemon@" or
1591$thisaddress contains "root@" or
1592$thisaddress contains "listserv@" or
1593$thisaddress contains "majordomo@" or
1594$thisaddress contains "-request@" or
1595$thisaddress matches "^owner-[^@]+@"
1596)
1597
1598The variable $local_part contains the local part of the mail address of the
1599user whose filter file is being run - it is normally your login id. The $domain
1600variable contains the mail domain. As explained above, if aliases or rewriting
1601are defined, or if prefixes or suffixes are in use, the tests for the current
1602user are also done with alternative addresses.
1603
1604
16053.30 Testing delivery status
1606----------------------------
1607
1608There are two conditions that are intended mainly for use in system filter
1609files, but which are available in users' filter files as well. The condition
1610first_delivery is true if this is the first process that is attempting to
1611deliver the message, and false otherwise. This indicator is not reset until the
1612first delivery process successfully terminates; if there is a crash or a power
1613failure (for example), the next delivery attempt is also a "first delivery".
1614
1615In a user filter file first_delivery will be false if there was previously an
1616error in the filter, or if a delivery for the user failed owing to, for
1617example, a quota error, or if forwarding to a remote address was deferred for
1618some reason.
1619
1620The condition manually_thawed is true if the message was "frozen" for some
1621reason, and was subsequently released by the system administrator. It is
1622unlikely to be of use in users' filter files.
1623
1624
16253.31 Multiple personal mailboxes
1626--------------------------------
1627
1628The system administrator can configure Exim so that users can set up variants
1629on their email addresses and handle them separately. Consult your system
1630administrator or local documentation to see if this facility is enabled on your
1631system, and if so, what the details are.
1632
1633The facility involves the use of a prefix or a suffix on an email address. For
1634example, all mail addressed to lg303-<something> would be the property of user
1635lg303, who could determine how it was to be handled, depending on the value of
1636<something>.
1637
1638There are two possible ways in which this can be set up. The first possibility
1639is the use of multiple .forward files. In this case, mail to lg303-foo, for
1640example, is handled by looking for a file called .forward-foo in lg303's home
1641directory. If such a file does not exist, delivery fails and the message is
1642returned to its sender.
1643
1644The alternative approach is to pass all messages through a single .forward
1645file, which must be a filter file so that it can distinguish between the
1646different cases by referencing the variables $local_part_prefix or
1647$local_part_suffix, as in the final example in section 3.33 below.
1648
1649It is possible to configure Exim to support both schemes at once. In this case,
1650a specific .forward-foo file is first sought; if it is not found, the basic
1651.forward file is used.
1652
1653The personal test (see section 3.27) includes prefixes and suffixes in its
1654checking.
1655
1656
16573.32 Ignoring delivery errors
1658-----------------------------
1659
1660As was explained above, filtering just sets up addresses for delivery - no
1661deliveries are actually done while a filter file is active. If any of the
1662generated addresses subsequently suffers a delivery failure, an error message
1663is generated in the normal way. However, if a filter command that sets up a
1664delivery is preceded by the word "noerror", errors for that delivery, and any
1665deliveries consequent on it (that is, from alias, forwarding, or filter files
1666it invokes) are ignored.
1667
1668
16693.33 Examples of Exim filter commands
1670-------------------------------------
1671
1672Simple forwarding:
1673
1674# Exim filter
1675deliver baggins@rivendell.middle-earth.example
1676
1677Vacation handling using traditional means, assuming that the .vacation.msg and
1678other files have been set up in your home directory:
1679
1680# Exim filter
1681unseen pipe "/usr/ucb/vacation \"$local_part\""
1682
1683Vacation handling inside Exim, having first created a file called .vacation.msg
1684in your home directory:
1685
1686# Exim filter
1687if personal then vacation endif
1688
1689File some messages by subject:
1690
1691# Exim filter
1692if $header_subject: contains "empire" or
1693$header_subject: contains "foundation"
1694then
1695save $home/mail/f+e
1696endif
1697
1698Save all non-urgent messages by weekday:
1699
1700# Exim filter
1701if $header_subject: does not contain "urgent" and
1702$tod_full matches "^(...),"
1703then
1704save $home/mail/$1
1705endif
1706
1707Throw away all mail from one site, except from postmaster:
1708
1709# Exim filter
1710if $reply_address contains "@spam.site.example" and
1711$reply_address does not contain "postmaster@"
1712then
1713seen finish
1714endif
1715
1716Handle multiple personal mailboxes:
1717
1718# Exim filter
1719if $local_part_suffix is "-foo"
1720then
1721save $home/mail/foo
1722elif $local_part_suffix is "-bar"
1723then
1724save $home/mail/bar
1725endif
1726