Translated using DeepL

Machine-translated page for increased accessibility for English questioners.

What is procmail?

Procmail is used to process incoming mail. It is not necessary to call .forward for FI to work, but the existence of the configuration file .procmailrc is sufficient. The procmail program starts for each message and according to the rules it finds in the configuration file, it performs the action belonging to the rule. For example, it can save the message to a specified mailbox or send the message to an external program.

.procmailrc

The .procmailrc file must be located in the home directory. It consists of rules that have two parts. The first part describes the message to which the rule is to be applied, and the second is the action itself - that is, what to do with a message that satisfies the rule. For example:

:0
* ^From.*adam
adambox

This rule saves every message coming from an address that contains the substring adam, to the mailbox adambox. All paths are relative to the directory set in the MAILBOX variable (the home directory on FI). To read mail via IMAP, which on FI looks for mailboxes in the ~/mail/ directory, add the line .procmailrc to the beginning of the file

MAILDIR=$HOME/mail

The rule always starts with a line where the first character is ":" followed by a list of flags that describe the processing of the message, e.g. whether to check the body of the message in addition to the headers, whether to forward the message to other rules after processing actions, etc. (see the manual page procmailrc for details.

(The number 0 is a historical remnant and has no function, but it is part of the beginning of the rule and should be left there.) Not specifying any flags means that we expect default functionality (the most important of which is that only the message headers are checked, not the body). The second line is a condition that describes which messages the rule applies to. Each condition is on a single line, starts with a "*", and everything after that is interpreted as a regular expression (POSIX 1003.2 RE) compatible with egrep's extended regular expressions (see the man pages at egrep and regex). Finally, the third line is an action. The action is always one line at the end of the rule. In this case, it means that the message is saved to the mailbox adambox.

A modification of the previous simple example is a rule for procmail that stores all incoming mail in the mailbox myspool:

:0
myspool

A more complicated example:

:0 c
* ^From.*adam
* ^Subject:.*compilers
! william@example.com
    
:0
* ^Subject:.*perl compilers
perlcomp

This example has two rules. There is a "c" flag on line 1 that says the message will not end up with this rule, but a copy of it will be sent for further processing. So for all messages from user adam with a subject containing the substring "compilers", the rule will perform the action on line 4, which is to forward the message to william@example.com. The second rule saves all messages related to Perl compilers to the mailbox perlcomp.

Forwarding email using Procmail

Procmail allows us to set up the same forwarding of emails as .forward, i.e. with the sender's envelope address preserved. See below for an example setup. Note: there are always two white characters in the square brackets: a space and a tab. Replace the string Xuzivatel with your login, e.g. Xnovak29.

:0
* !^X-FI-Xuzivatel-Loop
{
    :0fw
    | formail -a 'X-FI-Xuzivatel-Loop: loop prevention'

    :0
    * ^Return-Path:[    ]*\/[^  ].+
    { env=$MATCH }

    :0
    ! ${env+-f "$env"} honza.uzivatel@example.com
}

In a faculty environment, the main difference from using .forward is that all Procmail settings are applied before forwarding, such as the spam filter or automatic reply to absent email.

Removing log images from emails

If you read mail in its text version, you may be disturbed by signatures or logos that appear as attachments in HTML messages, even if they are not attachments you should pay attention to. If it's always repeating images, you can use our script to remove them.

The condition is that you receive mail on the Anxur server (this can be verified in the Faculty Administration).

Add the following lines to your .procmailrc:

:0fW
| /usr/local/libexec/image-attachment-filter.pl

Then you need to create a file ~/.image-attachment-filter.cfg, where you place the SHA-256 hashes of the attachments you want to delete (there can be a comment after the hash), for example as follows:

$ cat ~/.image-attachment-filter.cfg
1373b8627155d238a9856e3342961f1946858932086f7a2565b483b1959e14d9 Logo firmy XY

The hash is the output from sha256sum (can be used to calculate the hashes of other attachments you want to delete).

Please note that deleting an attachment will break any electronic signatures on the message.

Rules for encoded headers

Headers are encoded when non-ASCII characters are present, which may make it difficult for you to use the procmail rules. While procmail can see the header

Subject: =?UTF-8?B?TcOhbWUgdsSbdMWhw60gcHJvYmzDqW0=?=

the decoded form you see in mail clients is not available to it:

Subject: Máme větší problém

This can be solved by adding a new decoded version of the header (each must be added separately) before any other rules in .procmailrc that will use it. Example for the header Subject, which after decoding is stored in SUBJECT:

# extract the header if it's MIME-encoded
:0 h
* ^Subject:.*=\?
SUBJECT=| formail -cXSubject: | perl -MEncode=from_to -pe 'from_to $_, "MIME-Header", "utf-8"'

# if the header is not MIME, just extract it
:0 hE
SUBJECT=| formail -cXSubject

# make a decision based on the decoded header
:0:
* SUBJECT ?? ^Subject:.*větší
bigger

Inspiration Source.