index.html :: rss :: github :: telegram :: email

cron: faking sendmail

05 Oct 2024

The sendmail binary must be in /usr/sbin/sendmail, otherwise cron won't find it.

Simple Go program that emulates sendmail (just send its inputs to a file):

package main

import (
	"fmt"
	"io"
	"os"
	"strings"
	"time"
)

func main() {
	now := time.Now()
	args := strings.Join(os.Args, " ")
	stdin, _ := io.ReadAll(os.Stdin)

	s := fmt.Sprintf("time: %s\nargs: %s\nbody: %s", now.Format(time.Stamp), args, stdin)
	fmt.Println(s)
	if err := os.WriteFile("/tmp/fake-smtp", []byte(s), 0o666); err != nil {
		panic(err)
	}
}

build it and hijack:

go build -o sendmail main.go
sudo ln -s $(pwd)/sendmail /usr/sbin/sendmail
watch cat /tmp/fake-smtp

add testing line to your crontab:

* * * * * date

input (on my desktop machine):

args: /usr/sbin/sendmail -FCronDaemon -i -B8BITMIME -oem [email protected]
body: From: root (Cron Daemon)
To: [email protected]
Subject: Cron <alex@redot> date
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Cron-Env: <[email protected]>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/alex>
X-Cron-Env: <LOGNAME=alex>

Sat Oct  5 09:15:01 PM +04 2024

using this, now you may write a code that relays cron mail to any destination, e.g. telegram bot.