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
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed.
Share