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.

Code after the jump.

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 🙂



 

Esau Silva
Software Engineer at Region One ESC
Full Stack Software Engineer working with Microsoft technologies, ReactJS is awesome and avid Brazilian Jiu-Jitsu practitioner
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 *