Wednesday, May 1, 2024

Nullmailer Single-User Local Mail Setup

This is a way to setup local mail on a single-user Linux machine.

 I wanted to quickly jot down my notes on how to get nullmailer to route all local mail to my main user, for example for cron. This uses procmail. The mail client is neomutt.

Be sure to add a local mailbox or two to ~/.neomuttrc or similar files: mailboxes +local +Junk

Look for Nullmailer's protocol binary files. On Ubuntu this is at /usr/lib/nullmailer/, In this directory, write the following file and chmod +x it:


/usr/lib/nullmailer/local
#!/bin/bash
# Copyright 2018,2024 M Bazz <bazz@bazz1.com>
#
# Mail messages come in on fd3. Capture into a var, trim it so procmail
# recognizes it (nullmailer has the first 2 lines sender/receiver and a
# blank line, which procmail doesn't recognize).

# This is the mail as nullmailer expects it. See `man nullmailer-queue` to
# understand that format.
mail="`cat <&3`"

cat > /tmp/mail.log <<<"$mail"

n=0 # trigger for when blank line is found (sender/recipients lines done)
{ while IFS= read -r line; do
  if test $n = 0 ; then
    test -z "$line" && n=1
  else
    echo "$line"
  fi
done | /usr/bin/procmail -m /etc/procmailrcs/bazz ; } <<<"$mail"

let's look at the next file.

/etc/procmailrcs/bazz:
VERBOSE=1
SHELL=/bin/sh
PATH=$HOME/.local/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
MAILDIR=$HOME/Maildir
INBOX=$MAILDIR/bazz1/INBOX
LIST=$MAILDIR/list
NEWS=$MAILDIR/news
DEFAULT=$MAILDIR/Junk/
LOGFILE=/var/log/bazz/procmail.log
LOG=""
#octal u=rw,g=rw
UMASK=007


:0
* ^(From|To|Cc).*@(localhost|beastlin.int.bazz1.com)
{
  :0cWhi
  | newmail-hook local

  :0
  $MAILDIR/local/
}

Note that if the file is a symlink, then the symlink itself's ownership must be changed using `chown -h`

Essentially, if the rule matches then it goes into the local inbox, but if it doesn't match at all then it goes into the Junk folder.

/etc/nullmailer/remotes
localhost localhost

This says mail destined to the localhost will use the "localhost" protocol script listed earlier.

That's essentially it. Hopefully these quick notes are helpful.

No comments:

Post a Comment