E-mail

2025-02-28 13:23

I haven't really outlined this anywhere so I thought I'd write about my email setup.

I run my own email servers (Yes, it's still possible to self-host in 2025) but it now requires--above making sure that they're not open relays or otherwise vulnerable--that you set up TLS1, DKIM2, DMARC3 and perhaps SPF4 to be able to interact with the big email providers.

This post isn't about that though, what I wanted to talk about was the management of emails; I follow an number of mailinglists for instance (particularily those for NetBSD) and volume tends to be fairly high so having them all pour into my inbox is not a great idea.

Hence procmail. Procmail is a program that can scan emails and place them into folders. In ages past, it was run from your .forward file, but since my mail server now is a virtual machine and it wouldn't know how to read the .forward file, I run procmail directly from the [postfix][postfix] mailbox_command.

This means that it wouldn't also be able to read a .procmailrc, and as all users must now have a configuration on the mailserver which I don't wan to give them shell access to I had to further configure.

Here's the default procmailrc:

ORGMAIL=/data/mail/spool/$LOGNAME
MAILDIR=/data/mail/user/$LOGNAME
DEFAULT=$ORGMAIL

LOGFILE=/var/log/procmail

RC_USER=/data/mail/procmail/$LOGNAME

# Run before DROPPRIVS so that the bmf db files are
# still owned by root.
INCLUDERC=/usr/pkg/etc/bmf/tag.procmail

# This prevents new mailboxes being owned by root
DROPPRIVS=yes

# Since we're not using standard maildirs, make sure it exists
#
:0 c
| (if [ ! -d ${MAILDIR} ];then mkdir -p ${MAILDIR};fi)

INCLUDERC=$RC_USER/procmailrc

This configures the incoming mail directory (ORGMAIL) the user mail directory for non-inbox mail (MAILDIR) and sets the default to ORGMAIL to return mail to the inbox in case of problems. /var/mail/ is not used here as I wanted to keep all data on my more resilient data partition.

As this housekeeping is now set up, I load the global bmf configuration to tag all incoming mail according to the global bayes database.

This is a simple procmail filter rule:

:0 fw
| /usr/pkg/bin/bmf -d /usr/pkg/etc/bmf -p

This only tags mail if they get high scores by the filter, it's up to the recipient to do anything with it. After that, privileges are dropped and the maildir is made sure to exist, after which the user's procmailrc is invoked.

I have a lot of filter rules and they're fairly personal so I won't be posting them here, but I will add some notable rules:

The first one is that having mailboxes that constantly fill up with more and more mail is a pain so I automatically sort mine by year by having an RC file included by my personal procmailrc that changes MAILDIR by year:

NEWDIR=/data/mail/archive/staffan/`date +%Y`

:0 wc
| (if [ ! -d ${NEWDIR} ];then mkdir -p ${NEWDIR};fi;true)

MAILDIR=$NEWDIR

This first creates the directory if it doesn't exist, and then sets MAILDIR to it. I have a symlink from my spool directory which points to this and is changed every year. Thus every year starts on a blank slate.

Additionally I have an automatic rule, not invented by me, which automatically filters netbsd mailinglist mail to folders of their own:

# Make sure the mailinglists directory exists

:0 Wc
| (if [ ! -d ${MAILDIR}/mailinglists ];then mkdir ${MAILDIR}/mailinglists;fi;true)

# logix' dynamic mailinglist tool
:0 i
* ^Delivered-To:.\/[^@]+@(netbsd.org|pkgsrc.org)
{
    LISTNAME=${MATCH}

    # Make sure that the directory exists
    :0 Wc
    | (if [ ! -d ${MAILDIR}/mailinglists/netbsd ];then mkdir ${MAILDIR}/mailinglists/netbsd;fi)

    :0:
    * LISTNAME??^\/[^@]+
    mailinglists/netbsd/`echo ${MATCH} | tr A-Z a-z`
}

This is of course fine, but I do want to access old mail via IMAP, so for that I've set my IMAP server to specifically look at the archive directory and present it as a separate namespace. This lets me keep the old email available but not constantly in the way slowing things down (although Thunderbird really likes to index all the namespaces).

So there you go, not complicated at all.

Staffan


  1. Transport Layer Security 

  2. DomainKeys Identified Mail (what a dumb acronym) 

  3. Domain-based Message Authentication, Reporting and Conformance 

  4. Sender Policy Framework