Automatic Email and Text Notifications
From TheBeard Science Project Wiki
These tutorials describe my method of automatic messaging via email and text (SMS) from my Linux servers.
Required Packages
These packages are necessary for the all the following sections of this tutorial. In Debain, install them with apt-get:
apt-get install ssmtp procmail fetchmail fetchyahooEmail and Text Messaging with SSMTP
add crontab, user/group
/
├── etc
│ └── ssmtp
│ └── ssmtp.conf
└── scripts
├── config
│ ├── accounts.conf
│ ├── ddns-fetchmailrc
│ └── ddns-mailprocess.sh
├── ddns.sh
├── email.sh
├── external-ip.sh
├── notifications.sh
└── sms.shssmtp
procmail
fetchmail
fetchyahoossmtp.conf
#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=myaccount
# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
#mailhub=mail
# Where will the mail seem to come from?
rewriteDomain=gmail.com
# The full hostname
hostname=localhost
# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES
mailhub=smtp.gmail.com:587
UseStartTLS=YES
UseTLS=YES
AuthMethod=LOGIN
AuthUser=myaccount@gmail.com
AuthPass=PASSWORD
TLS_CA_File=/etc/pki/tls/certs/ca-bundle.crt
FromLineOverride=YES
notifications.sh
#!/bin/bash
#
# Send regular notifications.
# Currently set to run daily at NOON.
#
# Get current datetime values
dt=(`date +%Y\ %m\ %d\ %H\ %M\ %S\ %a\ %s\ %I\ %p\ %Z`)
y=$((10#${dt[0]})) #year
M=$((10#${dt[1]})) #month
d=$((10#${dt[2]})) #day
h=$((10#${dt[3]})) #hour(24)
m=$((10#${dt[4]})) #minute
s=$((10#${dt[5]})) #second
w=${dt[6]} #day of week (eg. Sun)
e=$((10#${dt[7]})) #epoch
t=$((10#${dt[8]})) #hour(12)
p=${dt[9]} #meridian (AM/PM)
z=${dt[10]} #timezone (eg. PDT)
# Schedule command when date ($1) matched regex ($2) with extra grep parameters ($3)
# Use parameter -P at $3 for perl regex: in case you need to use (str1|str2).
# Example: schedule $M$d "(115|21)" -P && sms "Subject" "Message"
# Don't use -P when you want a range of numbers including 2 or more digit numbers (ie. [1-15]).
schedule(){ echo "$1" | grep -wq $3 "$2";}
sms(){ /scripts/sms.sh "$1" "$2";}
eml(){ /scripts/email.sh "$1" "$2";}
notify(){ sms "$1" "$2";eml "$1" "$2";}
# =================================
# ==== Temporary Notifications ====
# =================================
# Dentist appointment
#schedule $M$d "58" && notify "Dentist" "Appointment 4:00pm 10/31/2017"
# =================================
# ====== Chore Notifications ======
# =================================
# LAUNDRY DAY
# Day before
schedule $w$d "Sun[1-7]" && notify "LAUNDRY DAY" "Tomorrow is LAUNDRY. Gather laundry/laptop, make todo/shopping list."
schedule $w$d "Sun(15|16|17|18|19|20|21)" -P && notify "LAUNDRY DAY" "Tomorrow is LAUNDRY. Gather laundry/laptop, make todo/shopping list."
# Day of
schedule $w$d "Mon[1-7]" && notify "LAUNDRY DAY" "WORK: Check gas. HOME: Do laundry, put laundry away."
schedule $w$d "Mon(15|16|17|18|19|20|21)" -P && notify "LAUNDRY DAY" "WORK: Check gas. HOME: Do laundry, put laundry away."
# WEEKLY CLEANING DAY
schedule $w "Tue" && notify "WEEKLY CLEANING DAY" "WORK: Fill gas, shopping. HOME: Clean, litter, sweep/vac, fridge, trash, check meds."
# Day after
schedule $w "Wed" && notify "WEEKLY REMINDER" "Order meds if necessary."
# MONTHLY CLEANING DAY
schedule $w$d "Tue[1-7]" && notify "MONTHLY CLEANING DAY" "WORK: Car stuff. HOME: Dishes, kitchen, bathroom, dust, inventory."
# BUSINESS DAY
schedule $d "[1-4]" && notify "BUSINESS DAY" "Rent due before 5th (\$720). Fedloan, budgeting, donations, next-buy."
# TECH DAY
schedule $w$d "Mon[1-7]" && notify "TECH DAY" "WORK: Updates, phone, healthcheck, security scan, organize files. HOME: Organize Entro/Winter, USB drives."
# SHOPPING DAY
schedule $w$d "Wed[1-7]" && notify "SHOPPING DAY" "Shopping, carwash. Maybe do during lunch."
# GROOMING DAY
schedule $w$d "Sun[1-7]" && notify "GROOMING DAY" "Shave/trim, brush/floss/whiten, clip nails."
schedule $w$d "Sun(15|16|17|18|19|20|21)" -P && notify "GROOMING DAY" "Shave/trim, brush/floss/whiten, clip nails."
# ELIMINATION DAY
schedule $w$d "Sun(15|16|17|18|19|20|21)" -P && notify "ELIMINATION DAY" "Do at least one item on TO_ELIMINATE list."
# INVENTORY DAY
# Disabled. Inventory is now included in monthly cleaning day.
# schedule $w$d "Tue[1-7]" && notify "INVENTORY DAY" "Do home, car, and office inventory."
# =================================
# = Other Permanant Notifications =
# =================================
# Air filter replacement notifications
schedule $M$d "71" && notify "Air Filter" "Holmes HAP726: Replace HEPA filters (model:HAPF600,qty:2)"
schedule $M$d "[1|7]1" && notify "Air Filter" "Holmes HAP726: Replace Carbon filters (model:HAPF600,qty:2)"
external-ip.sh
#!/bin/bash
log="/logs/external-ip.log"
ipsrc="http://checkip.dyndns.org"
date="`date`"
addr="myaccount@yahoo.com"
ip=`curl -s --connect-timeout 3 $ipsrc 2>/dev/null|grep -Po "Current IP Address: .*?\<"|tr '<' '\0'`
echo "IP: $ip"
if [ -z "$ip" ];then
echo "$date ERROR: Could not get external IP from $ipsrc" >>$log
else
echo -e "Subject:[neuro] $date $ip" | /usr/sbin/ssmtp $addr
if [ "$?" = "0" ];then
echo "$date SUCCESS: $ip sent to $addr" >>$log
else
echo "$date ERROR: Could not send to $addr" >>$log
fi
fi
email.sh
#!/bin/bash
# Send email message:
# Usage: $0 "<Subject>" "<Message>"
default_subject="Alert from Neuromancer (`date`)"
subject=""
message=""
case $# in
2)
subject="$1"
message="$2"
;;
1)
subject="$default_subject"
message="$1"
;;
*)
echo "Send Email Message:"
echo " Usage: `basename $0` \"Message\""
echo " `basename $0` \"Subject\" \"Message\""
exit 1
;;
esac
dir="$(cd $(dirname ${BASH_SOURCE[0]})&&pwd)"
. $dir/config/accounts.conf
echo -e "Subject: $subject\n\n$message" | /usr/sbin/ssmtp $mailto
sms.sh
#!/bin/bash
# Send SMS message:
# Usage: $0 "<Subject>" "<Message>"
default_subject="Alert"
subject=""
message=""
case $# in
2)
subject="$1"
message="$2"
;;
1)
subject="$default_subject"
message="$1"
;;
*)
echo "Send SMS Message:"
echo " Usage: `basename $0` \"Message\""
echo " `basename $0` \"Subject\" \"Message\""
exit 1
;;
esac
dir="$(cd $(dirname ${BASH_SOURCE[0]})&&pwd)"
. $dir/config/accounts.conf
echo -e "To: $smsto\nSubject: $subject\n\n$message" | /usr/sbin/ssmtp $smsto
ddns.sh
#!/bin/bash
/usr/bin/fetchmail -N -d0 -f "/scripts/config/ddns-fetchmailrc" -m "/scripts/config/ddns-mailprocess.sh"
alertfile="/shares/Private/000__DDNS-ALERT__.txt"
if [ -e $alertfile ];then
chown root:root $alertfile
chmod 777 $alertfile
fi
ddns-mailprocess.sh
#!/bin/bash
mailto="17775551234@myprovider.net"
date="`date`"
log="/logs/ddns.log"
alertfile="/shares/Private/000__DDNS-ALERT__.txt"
sms="/scripts/sms.sh"
cat /dev/stdin | grep -i "^Subject:" | sed 's/Subject\://g' | $sms "DDNS" "`cat /dev/stdin` ( $date )" &&\
echo "$date - DDNS notification sent to $mailto" | tee -a $log | sed "s/$mailto/cell\ phone/g" >$alertfile &&\
chmod 777 $alertfile
ddns-fetchmailrc
#
# This file MUST have user root and chmod 700
#
#set syslog;
#set daemon 90;
set postmaster "username";
poll "imap.mail.yahoo.com" protocol IMAP username "myaccount@yahoo.com" password "PASSWORD" is "username" here keep folder "DDNS-Mailbox" ssl
accounts.conf
#!/bin/bash
#
# Secure account information
# Permissions must be 750 root:texters
smsto="17775551234@myprovider.net"
mailto="myaccount@yahoo.com"