Tuesday 29 June 2010

Bash script to send email

Sometimes, on an linux machine, you need to frequently send a template email directly through command line. Here is a script to do so. You can also integrate this snippet into other scripts.




#! /bin/bash
#
# =======================
# Send Mail Script 0.1
# Written by VPSDiary
# http://vpsdiari.blogspot.com
# You may use, modify, and redistribute this script freely
# Released: June 2010
# =======================

function make_email
{
cat <<- _EOF_ Hi, The Body of your email comes here. Add whatever you want, including $variables _EOF_ }

#Input the SUBJECT
echo -n "What is the subject?" read SUBJECT echo "==> Email to whom? "
read EMAIL

# Make email body's text
touch /tmp/message.txt
make_email > /tmp/message.txt



# send email
mail -s "$SUBJECT" "$EMAIL" < /tmp/message.txt

#Say that it is sent echo "email sent to $EMAIL" echo "********"
To use this script, like any other, don't forget to first make it executable. Assuming that you have saved the script as 'sendit.sh'
chmod +x sendit.sh
And to use it:
bash sendit.sh
or
./sendit.sh

Don't use it to spam people! :)

No comments:

Post a Comment