Send email from bash command line (CLI)


This is quite a simple one but it’s one that I need to do often, usually from scripts.

Grab yourself the tool “sendemail” (on Debian it’s just ‘apt install sendemail’).

The structure of sendemail is easy:
sendemail -f "from email address" -t "to email address" -u "Subject line" -m "your message" -s "smtp server" -o "options such as tls" -xu "username to login to mail server with" -xp "password to login to mail server with"

Okay, so that looks messy. But here is a full version which is part of a script I use to notify me if the server has a different IP than it shoud:
sendemail -f [email protected] -t [email protected] -u IP changed! -m "IP has changed to $getip" -s smtp.blueyonder.co.uk -o tls=no -xu [email protected] -xp PASSWORD

BTW, may username and email isn’t actually dude. Anyway, it works, simple and effective. Just for bonus points you can have the script I use to monitor for change of IP and email me from a bash script and cron job:

#!/bin/bash
homeip="1.2.28.248"
getip=$(curl ipinfo.io/ip)

if [ $getip == $homeip ]; then
echo IP is correct
else
echo bugger
sendemail -f [email protected] -t [email protected] -u IP changed! -m "IP has changed to $getip" -s smtp.blueyonder.co.uk -o tls=no -xu [email protected] -xp BooYaaPassWord
fi

(Still not my password ;))

Now just save that as /usr/local/bin/ipcheck.sh and chmod +x. Add to cron (crontab -e) and set to every so often, mine is 10 minutes. Have a nice day.

Leave a comment