Send an email in bash

Posted in Tutorials, Linux by Rz on the November 4th, 2005

Quick tutorial for sending an email in bash.

To work, your system must have the following : postfix or sendmail, mailx package (and dependencies).

Define the variables :
#!/bin/bash
# script to send simple email
# email subject
SUBJECT="SET-EMAIL-SUBJECT"
# Email To ?
EMAIL="admin@somewhere.com"
# Email text/message
EMAILMESSAGE="/tmp/emailmessage.txt"

Start the message :
echo "This is an email message test">$EMAILMESSAGE
echo "This is a second line">>$EMAILMESSAGE
echo "This is a third line">>$EMAILMESSAGE

Send the email :
# send an email using /bin/mail
/bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE

In this script, you can add all the variables you want (ie : df, free, uptime, hostname, date…) ;)