How to Send Email – Unix Shell Snippet

I’m sharing two code snippets that will send an email. These snippets accept three parameters: a subject, an email address and a text file containing the email message.

[topads][/topads]

Version 1:

#!/bin/bash
#############################
# Snippet that send an email
#############################

sendMail() {
  SUBJECT=$1
  EMAIL=$2
  EMAILMESSAGE=$3

  /usr/bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE
}

Version 2:

#!/bin/bash
##############################
# Snippet that send an email
##############################

sendMail() {
  SUBJECT=$1
  EMAIL=$2
  EMAILMESSAGE=$3

  cat $EMAILMESSAGE | /usr/bin/mail -s "$SUBJECT" $EMAIL
}

My ‘mail’ tool is located ‘/usr/bin/mail’, but if yours is in a different location, change the path.

Hope this was helpful to you…

Share this article 🙂

[bottomads][/bottomads]

Spread the love

2 thoughts on “How to Send Email – Unix Shell Snippet

  1. […] In above code 'sendMail' is a function in 'sendMail.sh' that accepts three arguments: subject, email, and a text file for the email message. You can view the snippet here: send email snippet […]

  2. Esau Silva says:

    RT @jgezau: How to #SendEmail – #Unix #Shell #Snippet #Linux http://t.co/3dHTBFJr

Leave a Reply

Your email address will not be published. Required fields are marked *

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