Tag Archives: mail bin

Send mail bash script

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' you@example.com

Sample Shell Script

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

  1. #!/bin/bash
  2. # script to send simple email
  3. # email subject
  4. SUBJECT="SET-EMAIL-SUBJECT"
  5. # Email To ?
  6. EMAIL="admin@somewhere.com"
  7. # Email text/message
  8. EMAILMESSAGE="/tmp/emailmessage.txt"
  9. echo "This is an email message test"> $EMAILMESSAGE
  10. echo "This is email text" >>$EMAILMESSAGE
  11. # send an email using /bin/mail
  12. /bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE
  13.  

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