Skip to main content

Simple example of how to send an email from a shell script, and sendmail.

#!/bin/bash

email_message=/tmp/email-message-`date +%s`.txt
touch $email_message

email_subject="Hello from shell and sendmail!"
email_to="me@domain.com"
email_from="noreply@domain.com"
email_mime_ver="1.0"
email_content_type="text/plain; charset=utf-8"

email_body="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."

echo -e "To: $email_to" > $email_message
echo -e "From: $email_from" >>$email_message
echo -e "Content-Type: $email_content_type" >>$email_message
echo -e "MIME-Version: $email_mime_ver" >>$email_message
echo -e "Subject: $email_subject" >>$email_message
echo -e "$email_body" >>$email_message

/usr/sbin/sendmail -t < $email_message

rm -rf $email_message