03 Aug 2021

Email

Send automated email notifications

Video killed the radio star

Why?

Some decades back, it must have been easy to quickly fire off an email from a script. We can still see the remanants of the mail command in modern distros, but the likelyhood of that working out of the box (for a home server) is about 0%.

I am guessing this is due to email’s open nature being just overrun with spam. This leads to ISPs block SMTP’s default port 25. Firewalls block it locally. Open SMTP servers don’t accept mail from dynamic IPs. It is an uphill battle.

But I would really like to send notifications from my home servers! The solution is to hook in to an open SMTP server hosted by the provider of your choice. Then you can send emails through them instead of directly from the box.

Migadu and msmtp

I roll with Migadu as my email provider. Its privacy focused, has a good reputation, lets me have my custom domain, and isn’t Google.

In order to send email from my homeserver I need to authenticate with my Migadu account and ask it to send something on my behalf. msmtp is a lightweight program that can do just that. It is not a full fledged email server like postfix, just a shim.

Here are the configs I use for my arch boxes (the tls_starttls setting is key btw):

defaults
auth           on
tls            on
tls_starttls   off
tls_trust_file /etc/ssl/certs/ca-certificates.crt

account migadu
host smtp.migadu.com
port 465
user gemini@yonson.dev
from gemini@yonson.dev
password CHANGE

account default : migadu

I can then fire off emails:

$ echo "hack the planet" | msmtp nick@yonson.dev

Since Migadu rightly doesn’t let me set the from setting to something other than accounts that I own, I went ahead and just made mailboxes for each of my machines (gemini above).

I plan to store the password more securely, just haven’t picked an implementation yet.

Timers

Now to automate notifications. Since I am all in on systemd I went with systemd timers instead of cron.

I could register the timers as --user services, but by default, these services only run when the user is logged in. You can enable linger to have them run normally, but this has side effects with PAM things (and maybe others). So I just have normal system timers which use the User= setting.

Script

#!/bin/bash

subject () {
    echo "Subject: Daily belafonte node report"
}

report () {
    podman run -it --rm --network="host" -v $HOME/.bos:/root/.bos -v $HOME/.lnd:/root/.lnd:ro localhost/balanceofsatoshis report --styled 2>&1
}

( subject ; report ) | msmtp nick@yonson.dev

report.service

[Unit]
Description=Run BOS node report

[Service]
User=lightning
Group=lightning
ExecStart=/home/lightning/balanceofsatoshis/daily-report.sh

report.timer

[Unit]
Description=Run report daily

[Timer]
OnCalendar=daily

[Install]
WantedBy=timers.target

Enable

sudo systemctl enable report.timer
sudo systemctl start report.timer

yonson