{"id":8506,"date":"2015-01-08T18:02:38","date_gmt":"2015-01-08T18:02:38","guid":{"rendered":"https:\/\/wp.okra.host\/article\/mail-filtering\/"},"modified":"2021-03-07T14:24:57","modified_gmt":"2021-03-07T13:24:57","slug":"mail-filtering","status":"publish","type":"ht_kb","link":"https:\/\/kb.okra.host\/de\/article\/mail-filtering\/","title":{"rendered":"Mail filtering"},"content":{"rendered":"<h2 id=\"overview\" >\u00dcbersicht<\/h2>\n<p>Message filtering is done prior to delivery via maildrop. Each message goes through two levels of filters: (1) global &#8212; processed first in <code>\/etc\/maildroprc<\/code> followed by (2) local per-user filters in <code>$HOME\/.mailfilter<\/code>. Basic filtering recipes are provided below. Syntax and usage may be found in <a class=\"external text\" href=\"http:\/\/apnscp.com\/linux-man\/man7\/maildropfilter.7.html\" rel=\"nofollow\">mailfilter(7)<\/a>.<\/p>\n<div class=\"ui-infobox ui-infobox-warning\"><span style=\"color: #ff0000\"><strong>Wichtig:<\/strong><\/span> on <a title=\"Ermitteln der Plattformversion\" href=\"https:\/\/kb.okra.host\/de\/platform\/determining-platform-version\/\">older platforms<\/a>, (less than\u00a0v6), remember to always run <a class=\"external text\" href=\"http:\/\/apnscp.com\/linux-man\/man1\/dos2unix.1.html\" rel=\"nofollow\">dos2unix<\/a> or EOL conversion &#8220;Windows -&gt; Unix&#8221; (<strong>Dateien<\/strong> &gt; <strong>File Manger<\/strong> &gt; <strong>Eigenschaften<\/strong> <em>action<\/em>)\u00a0on the filter after making changes. maildrop will not read filter files written on Windows or Mac correctly. Consequently, mail cannot be delivered to the account until corrected.<\/div>\n<p><a class=\"mw-redirect\" title=\"SpamAssassin\" href=\"http:\/\/wiki.apnscp.com\/index.php\/SpamAssassin\">SpamAssassin<\/a> is invoked from the global maildrop filter, <code>\/etc\/maildroprc<\/code>. The following block of code passes the message off to SpamAssassin if it is smaller than 128 KB.<\/p>\n<pre class=\"maildrop code\">if ($SIZE &lt; 131072)\r\n{\r\n        xfilter \"\/usr\/bin\/spamc -u $RECIPIENT\"\r\n}<\/pre>\n<div class=\"ui-infobox ui-infobox-warning\">Please note that these 4 lines are required for a message to be filtered through SpamAssassin. Removal of these lines from <code>\/etc\/maildroprc<\/code> will cause mail to be delivered unfiltered. Further, the linebreaks are critical. Opening and closing braces <b>must<\/b> be on their own lines. <a class=\"external text\" href=\"http:\/\/en.wikipedia.org\/wiki\/Indent_style#BSD_KNF_style\" rel=\"nofollow\">K&amp;R\/KNF style braces<\/a> <b>nicht<\/b> work. Likewise, ensure line endings are correct (previous warning).<\/div>\n<h2 id=\"default-maildrop-filter\" ><span id=\"Default_maildrop_filter\" class=\"mw-headline\">Default maildrop filter<\/span><\/h2>\n<div><span class=\"geshi-header\">\/etc\/maildroprc<\/span><\/div>\n<pre class=\"maildrop code\"># Global maildrop rules go here\r\n# See http:\/\/www.courier-mta.org\/maildrop\/maildropfilter.html for syntax\r\nif ($SIZE &lt; 131072)\r\n{\r\n        exception {\r\n                xfilter \"\/usr\/bin\/spamc -u $RECIPIENT\"\r\n        }\r\n}\r\n\u00a0\r\nDELETE_THRESHOLD=10.0\r\nif (\/^X-Spam-Flag: YES\/)\r\n{\r\n        \/X-Spam-Score: (d+)\/\r\n        if ($MATCH1 &gt;= $DELETE_THRESHOLD)\r\n        {\r\n                to \/dev\/null\r\n        }\r\n        else\r\n        {\r\n                to Mail\/.Spam\/\r\n        }\r\n}<\/pre>\n<p><strong>Explanation:<\/strong> if the message size is smaller than 128 KB, hand it off to SpamAssassin. <code>DELETE_THRESHOLD<\/code> is the maximum score an e-mail may have <i>if and only if<\/i> it is labeled as spam. If the score is greater or equal to <code>DELETE_THRESHOLD<\/code>, then the message will be deleted by being sent to <code>\/dev\/null<\/code> otherwise deliver to the Spam mailbox on the server. <a title=\"Accessing Spam folder\" href=\"https:\/\/kb.okra.host\/de\/e-mail\/accessing-spam-folder\/\">This mailbox<\/a> may be accessed through <a title=\"Zugriff auf E-Mail\" href=\"https:\/\/kb.okra.host\/de\/e-mail\/accessing-e-mail\/#webmail\">webmail<\/a> or IMAP.<\/p>\n<h3 id=\"globally-disabling-per-user-filter-files\" ><span id=\"Globally_disabling_per-user_filter_files\" class=\"mw-headline\">Globally disabling per-user filter files<\/span><\/h3>\n<p>Adding <code>to $DEFAULT<\/code> at the end of the global filtering file will deliver the message to the default mailbox, <code>$HOME\/Mail<\/code>, and cease further processing.<\/p>\n<h3 id=\"selectively-disabling-per-user-filtering\" ><span id=\"Selectively_disabling_per-user_filtering\" class=\"mw-headline\">Selectively disabling per-user filtering<\/span><\/h3>\n<p><code>LOGNAME<\/code> holds the current username on the server. A simple check can be used to prohibit user filtering for a specific user.<\/p>\n<div><span class=\"geshi-header\">\/etc\/maildroprc<\/span><\/div>\n<pre class=\"maildrop code\">if ($SIZE &lt; 131072)\r\n{\r\n        xfilter \"\/usr\/bin\/spamc -u $RECIPIENT\"\r\n}\r\n# User \"bill\" loves his spam\r\nif ($LOGNAME ne \"bill\" &amp;&amp; \/^X-Spam-Flag: YES\/)\r\n{\r\n        to \/dev\/null\r\n}<\/pre>\n<p>Likewise to disable checking the filter file for a user, the above recipe can be further modified&#8230;<\/p>\n<pre class=\"maildrop code\">if ($SIZE &lt; 131072)\r\n{\r\n        xfilter \"\/usr\/bin\/spamc -u $RECIPIENT\"\r\n}\r\n# User \"bill\" loves his spam\r\nif ($LOGNAME ne \"bill\" &amp;&amp; \/^X-Spam-Flag: YES\/)\r\n{\r\n        to \/dev\/null\r\n}\r\n# But he's prohibited from adding any filter rules\r\nif ($LOGNAME eq \"bill\")\r\n{\r\n        to $DEFAULT\r\n}<\/pre>\n<div class=\"ui-infobox ui-infobox-warning\">Note that eq, lt, le, gt, ge, ne are used for string comparisons, while ==, &lt;, &lt;=, &gt;, &gt;=, != are used for numeric comparisons.<\/div>\n<h3 id=\"deleting-all-messages-marked-spam\" ><span id=\"Deleting_all_messages_marked_spam\" class=\"mw-headline\">Deleting all messages marked spam<\/span><\/h3>\n<p>Before the recipe is given bear in mind this is strongly discouraged for two reasons, (1) young e-mail accounts may have a lot of variability in scoring and (2) no failure notice is generated. Consequently, neither the sender nor you will know if the message had been deleted, because no delivery failure status is generated. This is very similar to the <a href=\"http:\/\/wiki.apnscp.com\/index.php\/SMTP#What_is_the_default_maildrop_filter.3F\">default maildroprc<\/a>, except threshold scoring is removed and all spam is deleted.<\/p>\n<pre class=\"maildrop code\">\u00a0\r\nif (\/^X-Spam-Flag: YES\/)\r\n{\r\nto \/dev\/null\r\n}\r\n<\/pre>\n<h3 id=\"filtering-to-an-external-program\" ><span id=\"Filtering_to_an_external_program\" class=\"mw-headline\">Filtering to an external program<\/span><\/h3>\n<p>maildrop&#8217;s <a class=\"external text\" href=\"http:\/\/apnscp.com\/linux-man\/man7\/maildropfilter.7.html#lbBI\" rel=\"nofollow\">xfilter<\/a> directive pipes the message to an external script for processing. A rudimentary example reverses the message text. Naturally, as this is a shell script it should be directly executable from the shell, so ensure the permissions are at least 700 (<code>chmod 700 reverse.sh<\/code>).<\/p>\n<div><code><span class=\"geshi-header\">.mailfilter<\/span><\/code><\/div>\n<pre class=\"maildrop code\">xfilter \"$HOME\/reverse.sh\"<\/pre>\n<div><code><span class=\"geshi-header\">reverse.sh<\/span><\/code><\/div>\n<pre class=\"bash code\" data-language=\"shell\"><code>#!\/bin\/sh\r\nexec 6&lt;&amp;0\r\nwhile read -u 6 line\u00a0;\r\ndo \r\n        echo $line | rev\r\ndone\r\nexec 6&lt;&amp;-\r\nexit 0<\/code><\/pre>\n<h3 id=\"creating-a-spam-trap\" ><span id=\"Creating_a_spam_trap\" class=\"mw-headline\">Creating a spam trap<\/span><\/h3>\n<p>Spam traps are useful addresses deliberately listed on Web pages hidden from public view. Spam bots harvest these addresses and deliver spam. You can use this knowledge to feed all e-mail destined to a particular address directly to <a class=\"mw-redirect\" title=\"SpamAssassin\" href=\"http:\/\/wiki.apnscp.com\/index.php\/SpamAssassin\">SpamAssassin<\/a> with the <code>an<\/code> directive. In addition to delivering to mailboxes, <code>an<\/code> can forward outbound to another address (!) or to another program (|) with a simple prefix. The following assumes <code>spam@mydomain.com<\/code> maps to a virtual mailbox on the server owned by the user <code>myuser<\/code><\/p>\n<table border=\"1\">\n<caption><b>Mailbox Routes<\/b><\/caption>\n<tbody>\n<tr>\n<th>Username<\/th>\n<th>Domain<\/th>\n<th>Destination<\/th>\n<th>Type<\/th>\n<\/tr>\n<tr>\n<td>spam<\/td>\n<td>mydomain.com<\/td>\n<td>\/home\/myuser\/Mail<\/td>\n<td>V<\/td>\n<\/tr>\n<tr>\n<td>myuser<\/td>\n<td>mydomain.com<\/td>\n<td>\/home\/myuser\/Mail<\/td>\n<td>V<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>And for the recipe<\/p>\n<pre class=\"maildrop code\">if (hasaddr(\"spam@mydomain.com\")) \r\n{\r\nto \"|\/usr\/bin\/spamc --spam -u $RECIPIENT\"\r\n}<\/pre>\n<h3 id=\"using-a-single-spamassassin-instance\" ><span id=\"Using_a_single_SpamAssassin_instance\" class=\"mw-headline\">Using a single SpamAssassin instance<\/span><\/h3>\n<p>One user account may be delegated to handle all SpamAssassin filtering settings for all e-mail accounts. Replace the e-mail-specific variable, <code>$RECIPIENT<\/code> with the full user&#8217;s login<code>\/etc\/maildroprc<\/code>. For example, to let the user named <var>example<\/var> on the domain <var>Beispiel.com<\/var> handle spam filtering for all users on the domain <var>Beispiel.com<\/var>:<\/p>\n<div><span class=\"geshi-header\">File:<\/span><code><span class=\"geshi-header\"> \/etc\/maildroprc<\/span><\/code><\/div>\n<pre class=\"maildrop code\"># Global maildrop rules go here\r\n# See http:\/\/www.courier-mta.org\/maildrop\/maildropfilter.html for syntax\r\nif ($SIZE &lt; 131072)\r\n{\r\n        exception {\r\n                xfilter \"\/usr\/bin\/spamc -u example@example.com\"\r\n        }\r\n}\r\n# rest of the rules ...<\/pre>\n<dl>\n<dt><strong>Pros<\/strong><\/dt>\n<\/dl>\n<ul>\n<li>Jumpstart filtering: newly-added users will have robust spam\/ham information already in place to immediately increase the effectiveness of spam filtering<\/li>\n<li>Low volume benefits from high volume: e-mail accounts that receive few e-mails will already have <a class=\"external text\" href=\"http:\/\/wiki.apache.org\/spamassassin\/BayesInSpamAssassin\" rel=\"nofollow\">SpamAssassin&#8217;s Bayesian classifier system<\/a> activated. Bayes scores add anywhere between -2 to 3 points per message depending upon how certain SpamAssassin is of its validity. Those points are generally enough to correctly rank a false positive as a true positive.<\/li>\n<\/ul>\n<dl>\n<dt><strong>Cons<\/strong><\/dt>\n<\/dl>\n<ul>\n<li>Privacy issues: bits of e-mail used by SpamAssassin may be viewable by the main user<\/li>\n<li>Dilution: SpamAssassin has a finite storage capacity of tokens from scanned messages. These special tokens may appear more readily in spam or non-spam. Introducing a high variability among several users may reduce SpamAssassin&#8217;s effectiveness as the token counts are removed to store new tokens.<\/li>\n<\/ul>\n<h3 id=\"complex-filtering\" ><span id=\"Complex_filtering\" class=\"mw-headline\">Complex filtering<\/span><\/h3>\n<p>Additional filtering examples may be found in the <a class=\"external text\" href=\"http:\/\/updates.apnscp.com\/2007\/09\/weekly-tip-3-categorizing-e-mails-with-maildrop\/\" rel=\"nofollow\">third installment<\/a> of the ephemeral Weekly Tip.<\/p>\n<h2 id=\"forwarding\" ><span id=\"Forwarding\" class=\"mw-headline\">Forwarding<\/span><\/h2>\n<p>Edit the file named <code>.mailfilter<\/code> within the user&#8217;s home directory and add:<\/p>\n<pre class=\"maildrop code\">to \"!user@mydomain.com\"<\/pre>\n<p>If you would like to forward <i>und<\/i> store a copy of the message on the server, then use the <code>cc<\/code> directive to maildrop:<\/p>\n<pre class=\"maildrop code\">cc \"!user@mydomain.com\"<\/pre>","protected":false},"excerpt":{"rendered":"<p>Overview Message filtering is done prior to delivery via maildrop. Each message goes through two levels of filters: (1) global &#8212; processed first in \/etc\/maildroprc followed by (2) local per-user filters in $HOME\/.mailfilter. Basic filtering recipes are provided below. Syntax and usage may be found in mailfilter(7). Important: on older&#8230;<\/p>","protected":false},"author":1,"comment_status":"open","ping_status":"closed","template":"","format":"standard","meta":{"footnotes":""},"ht-kb-category":[58],"ht-kb-tag":[],"class_list":["post-8506","ht_kb","type-ht_kb","status-publish","format-standard","hentry","ht_kb_category-e-mail"],"_links":{"self":[{"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb\/8506","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb"}],"about":[{"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/types\/ht_kb"}],"author":[{"embeddable":true,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/comments?post=8506"}],"version-history":[{"count":1,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb\/8506\/revisions"}],"predecessor-version":[{"id":8507,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb\/8506\/revisions\/8507"}],"wp:attachment":[{"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/media?parent=8506"}],"wp:term":[{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb-category?post=8506"},{"taxonomy":"ht_kb_tag","embeddable":true,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb-tag?post=8506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}