[Script] Send e-mail from bash

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 address, 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]

Script

#!/bin/bash
# Subject
SUBJECT="EMAIL-SUBJECT"
# To
TOEMAIL="[email protected]"
# Message
EMAILMESSAGE="/home/user/message.txt"
echo "This is email text" >>$EMAILMESSAGE
# Sending email using /bin/mail
/bin/mail -s "$SUBJECT" "$TOEMAIL" < $EMAILMESSAGE

6 Comments

  1. juriad May 21, 2012 Reply
  2. stderr May 21, 2012 Reply
  3. Murdej Ukrutny May 21, 2012 Reply
  4. Ash May 21, 2012 Reply
  5. find-way.net August 25, 2016 Reply
  6. umesh January 18, 2017 Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.