Email – Minimal MTA for Local Mail Delivery for Cron

emailmail-transport-agentsendmail

On my laptop, I have set up a cron that performs a weekly job. If there is something to report, I would like to get an e-mail for it. Right now I am using KMail as MUA which is able to read mail from the Maildir directory at ~/.local/share/local-mail. Thus, I want local mail to be delivered to that folder (with a Maildir structure).

I was hoping that a dead simple program already exist that has a sendmail interface (such that echo "$REPORT" | mail -s "$SUBJECT" "$ME" can work with it). Installing exim or forwarding mail to my remote mailserver is considered overkill.

The question Simplest way of forwarding all mail from server? seems to target remote forwarding which does not suit my needs (I need local delivery). This old Gentoo thread ended up in crafting an old script in Perl. Surely there must exist a well-thought, dead-simple program?

Any recommendations? I am using Arch Linux.

Best Answer

Since I could not find an existing, small program, I decided to write my own one. Originally, I came up with:

#!/bin/bash
# Simple sendmail
# filename per spec at http://cr.yp.to/proto/maildir.html
rand=$((RANDOM % 1000))
msgname=$(date +%s).P$$R$rand.$(hostname | tr '/:' '\057\072')

# Safety measure: do not overwrite existing mail
set -o noclobber

cat > ~peter/".local/share/local-mail/inbox/new/$msgname"

That worked... except if the user executing the script is not "peter". The successor of this idea is a "small" C program, femtomail. From its README:

femtomail - minimal MDA with Maildir support

femtomail is a minimal Mail Delivery Agent (MDA) for local mail. Mail is accepted from standard input and placed in a Maildir box of a user. This software is intended for use on a single-user machine.

Remote delivery, daemonizing, sender verification, etc. is not implemented and won't be implemented due to its complexity. femtomail is not written because mail software did not exist, but because existing software were too large for the simple task of delivering cron mail to the local user.

The workflow of femtomail:

  1. Change the process user and group.
  2. Create a new file with a unique filename in the mail directory.
  3. Write a Received header to the file.
  4. Pass data from standard input to the file.
  5. Exit.

The source code and installation instructions are available at https://git.lekensteyn.nl/femtomail/.

Related Question