Send mail bash script

Explains how to write a shell script to send an email from command prompt under Linux / UNIX operating systems.

The mail command can be used under Linux or UNIX bash / ksh / csh shell to send an email. To send a message to one or more people, mail can be invoked with arguments which are the names of people to whom the mail will be sent. You are then expected to type in your message, followed by an ‘control-D’ at the beginning of a line. However, using the following syntax one can send email easily:
mail -s ‘Subject’ [email protected] Sample Shell Script

Here is what you need to put in a shell script:

# script to send simple email 
# email subject
# Email To ?
EMAIL="[email protected]"
# Email text/message
echo "This is an email message test"> $EMAILMESSAGE
echo "This is email text" >>$EMAILMESSAGE
# send an email using /bin/mail
/bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE

See also: Sending mail with attachment from command line/shell