E-mail notifications from the Raspberry Pi

I run my Pi headless, so all my interaction is through the terminal. However, I run a few things unmonitored (Public IP change detection, upload-glacier, etc.) and keep myself updated via e-mail. I've created a separate account for the Pi for security and to keep my mailbox clean.

Setup is easy. Install ssmtp via sudo apt-get install ssmtp, and edit /etc/ssmtp/ssmtp.conf:

mailhub=smtp.gmail.com:587
root=youremail@wherever.com
AuthUser=raspi-email@gmail.com
AuthPass=your-password
UseTLS=YES
UseSTARTTLS=YES
  • root: Catchall e-mail address to send to
  • AuthUser: Pi-owned gmail account
  • AuthPass: Password for AuthUser

This is fundamentally insecure, the password will be readable to anyone who can access your Pi or SD card, so make sure there's nothing important on this account, and use a unique password.

You can send mail with mail -s Subject to-user@example.com. I've created a wrapper script that mails using HTML so that plain-text console output is nicely formatted:

#!/bin/bash

if ! [ $# -eq 2 ]; then
    echo "Usage: $0 <title> <message>"
    exit 255
fi

_CONV_TEXT=$(printf "$2" | recode ascii..html | awk ' { gsub(/ /, "\\&nbsp;"); printf "%s<br>\n", $0 } ')
hostname=`hostname`
printf "<html><body><code style='font-size: 10pt'>%s</code></body></html>" "$_CONV_TEXT" | mail -a "Content-type: text/html" -s "[$hostname] $1" "me-myself@example.com"
exit $?

This is saved in ~/bin/mailout, and used whenever a script needs to send me output.