Raspberri Pi email notifications setup with SendGrid
Came across a stupid problem: my scraper raspberry is getting full.
Instead of hooking the db file to the hard drive, I first wanted to fix a more generic issue: send notifications from my raspberry. What a rabbit hole.
Sendgrid lets you send 100 emails per day, that should do it. Registered.
Created an API key, then authenticated my domain under Settings/Sender Authentication
. -> this is essential for sending emails - I had to create CNAME and TXT records as Sendgrid instructed me to do.
I will refer to your domain you set here as YOUR.DOMAIN
Then log into your rPi and install postfix (steps in their docs)
sudo apt install postfix libsasl2-modules mailutils -y
On the configuration screen, select the second, "Internet Site" option:
Edit /etc/postfix/main.cf
If it's empty or does not exists, maybe you accidentally selected No Configuration.
Append the file with this:
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
smtp_tls_security_level = encrypt
header_size_limit = 4096000
relayhost = [smtp.sendgrid.net]:587
Then edit /etc/postfix/sasl_passwd
Add
[smtp.sendgrid.net]:587 apikey:yourSendGridApiKey
to it, then
sudo chmod 600 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd
To set your FROM address, create the file sudo nano /etc/mailutils.conf
and add this as the content:
address {
email-domain YOUR.DOMAIN;
};
By default, it'd use your hostname, that's rejected by sendgrid (stackoverflow info).
Finally restart your service:
sudo systemctl restart postfix
Test
echo "This is the mail body" | mail -s "new-subject" "user@mailinator.com"
You can also set the from field via the command line:
echo "Email Body" | mail -s "subject" -r "any-address-within@YOUR.DOMAIN" "user@mailinator.com"
I currently set an alarm for my pi to notify me if the memory card fills up with a simple crontab bash script, that sends a reminder every day (chatGPT generated)
#!/bin/bash
# Notify the user over this percentage:
threshold=70
# Where the email comes from: I name my Pis differently every time, so the hostname identifies which pi is having problems
FROM="$HOSTNAME.YOUR.DOMAIN"
# Flag file to track if notification has been sent today
flag_file="/tmp/memory_card_notification_sent"
# Check if flag file exists and if it's older than one day
if [ -f "$flag_file" ]; then
# Get the modification time of the flag file in seconds since the epoch
mod_time=$(date -r "$flag_file" +%s)
# Get the current time in seconds since the epoch
current_time=$(date +%s)
# Calculate the difference in seconds between current time and modification time
time_diff=$((current_time - mod_time))
# If the flag file is older than one day (86400 seconds), remove it
if [ "$time_diff" -gt 86400 ]; then
rm "$flag_file"
fi
fi
# Check if notification has been sent today
if [ -f "$flag_file" ]; then
# Notification has already been sent today, exit script
exit 0
fi
# Get the disk usage percentage
usage=$(df -h "/" | awk 'NR==2 {print $5}' | sed 's/%//')
# Check if disk usage exceeds the threshold
if [ "$usage" -ge "$threshold" ]; then
# Send an email notification
echo "Warning: Memory card is $usage% full." | mail -r $FROM -s "Memory card usage alert" "symunona+status@gmail.com"
# Create flag file to indicate notification has been sent today
touch "$flag_file"
fi
raspberry
mail