How to send an email with attachment through Shell script

When we run cron for sending disk, cpu or memory alerts, we are supposed to send an email with those alerts. Below is the quick way to send an email with attachments through shell script.

#!/bin/bash
# Subject of an email
SUBJECT=”Test email with attachment from a bash script”
# Which address to send
TO_ADDRESS=”to-address@gmail.com”
# Where the attachment
ATTACHMENT_FILE=”/tmp/attachment.txt”
# We just put some contents to our attachment file
echo “This goes into the file.” > $ATTACHMENT_FILE
echo “This appends to the file.” >> $ATTACHMENT_FILE
# Now send the message
/bin/mail -s “$SUBJECT” “$TO_ADDRESS” < $ATTACHMENT_FILE>

Post a Comment

Previous Post Next Post