...
  View open merge request
Commits (3)
---
title: Efficient and modern open source mail setup
authors: Alain M. Lafon
category: 200ok
date-created: 2018-04-19
tags: 200ok
uuid: a39bdfe1-00e9-4102-b84c-7272622b3b1a
---
Involved components:
- Local copy: `offlineimap`
- Filtering/Spam Detection: postsynchook: `.bin/process-new-mail`
- `procmail`
- Integrity/History/Backup: `git commit`
- MUA: `mu4e`
```
[general]
ui = ttyui
accounts = 200ok, zhaw, dispatched, zen-tempel
# THIS Section will add options for every Account section
[DEFAULT]
sslcacertfile = /etc/ssl/certs/ca-certificates.crt
[Account 200ok]
localrepository = 200ok-Local
remoterepository = 200ok-Remote
postsynchook = ~/bin/process-new-mail 200ok INBOX.spambucket
[Repository 200ok-Local]
type = Maildir
localfolders = ~/Maildir/200ok
[Repository 200ok-Remote]
type = IMAP
remotehost = [YOUR_IMAP_SERVER]
remoteuser = [YOUR_USER]
remotepass = [YOUR_PASSWORD]
realdelete = no
```
`.offlineimaprc`
```
#!/bin/zsh
# This shellscript requires two parameter:
# $1: The Mail account name (ie # 200ok|zen-tempel)
# $2: The name of the Mailbox spam folder
check_spam() {
# Find all new files (updated in the last 5min) in the inbox
for file in `find ~/Maildir/$1/INBOX/new -type f -cmin -5`
do
echo "Processing $file with procmail"
# `procmail` requires three parameters:
# `procmail -a "200ok" -a "/home/munen/Maildir_bak/200ok/INBOX/new/test_mail" -a "test_mail" "INBOX.spambucket" < test_mail`
# $1: The name of the Mailbox (i.e. 200ok|zen-tempel)
# $2: The path to the file that's being processed
# $3: The original mails filename
# $4: The Spam Bucket folder name
FILENAME=`basename $file`
echo "procmail -a $1 -a '$file' -a $FILENAME -a '$2' < $file"
procmail -a $1 -a "$file" -a $FILENAME -a "$2" < "$file"
done
}
check_spam $1 $2
cd ~/Maildir/
git add .
git commit -m"Automated commit: $(date)"
```
`.bin/process-new-mail`
```
# This configuration requires three parameters:
# `procmail -a "200ok" -a "/home/munen/Maildir_bak/200ok/INBOX/new/test_mail" -a "test_mail" -a "INBOX.spambucket" < test_mail`
# $1: The name of the Mailbox (i.e. 200ok|zen-tempel)
# $2: The path to the file that's being processed
# $3: The original mails filename
# $4: The name of the spam bucket (i.e. INBOX.spambucket)
DEFAULT=$HOME/Maildir
LOGFILE=$HOME/.procmail/log
MAILDIR=$HOME/Maildir
SPAMFOLDER=$HOME/Maildir/$1/$4/cur/$3
# BEGIN_SPAMASSASSIN
# The condition line ensures that only messages smaller than 250 kB
# (250 * 1024 = 256000 bytes) are processed by SpamAssassin. Most spam
# isn't bigger than a few k and working with big messages can bring
# SpamAssassin to its knees.
#
# The lock file ensures that only 1 spamassassin invocation happens
# at 1 time, to keep the load down.
#
:0fw: spamassassin.lock
* < 256000
| spamassassin
# Mails with a score of 15 or higher are almost certainly spam (with 0.05%
# false positives according to rules/STATISTICS.txt). Let's put them in a
# different mbox. (This one is optional.)
:0
* ^X-Spam-Level: \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
{
:0 wc
"$SPAMFOLDER"
:0 w
| rm $2
}
# All mail tagged as spam (eg. with a score higher than the set threshold)
# is moved to "probably-spam".
:0
* ^X-Spam-Status: Yes
{
:0 wc
"$SPAMFOLDER"
:0 w
| rm $2
}
# Work around procmail bug: any output on stderr will cause the "F" in "From"
# to be dropped. This will re-add it.
:0
* ^^rom[ ]
{
LOG="*** Dropped F off From_ header! Fixing up. "
:0 fhw
| sed -e '1s/^/F/'
}
# END_SPAMASSASSIN
:0
* ^From.*
* ^Subject.*Lower your website bounce rate with an Animation for
{
# `c` means to `copy` the message, so that the next action will also run
# `w` means wait until the write is finished
:0 wc
# Copies the message to the the respective Mail accounts spambucket.
# Also, it keeps the existing filename. Without $2, procmail would
# invent a new name.
"$SPAMFOLDER"
# Delete the original file, effectively moving the file to the spambucket
:0 w
| rm $2
}
# For all other mails without matching rules, don't copy them anywhere. Just leave them be.
:0
/dev/null
```
`.procmailrc`
# Related documentation
- [ Intro to procmail ](http://porkmail.org/era/mail/procmail-presentation.html#syntax)
- [ Good procmail recipes ](http://www.netikka.net/tsneti/info/proctips.php#subject)
- [ Spamassassin via Procmail ](https://wiki.apache.org/spamassassin/UsedViaProcmail)