How to send an email from UNIX using mailx.


#!/bin/sh
#
# Purpose: Demonstrate how to send an email from UNIX using mailx.
############################################################

# Example 1 – Simple:
echo “This is the body.”| mailx -s “mailx Test1” sample@sample.com

# Example 2 – Using Variables:
SUBJECT=”mailx Test2″
EMAIL_ADDRESS=”sample@sample.com
BODY=”This is the body of the message.”

echo “$BODY” | mailx -s “$SUBJECT” “$EMAIL_ADDRESS”

# Example 3 – Attached File:
SUBJECT=”mailx Test3″
EMAIL_ADDRESS=”sample@sample.com
BODY=”Test:This is the body of the message.”
ATTACHED_FILE=”/etc/hosts”

cat “$ATTACHED_FILE” | mailx -s “$SUBJECT” “$EMAIL_ADDRESS”

Leave a Reply

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