Send Email from Linux Command Line

Published: 12 years ago

Sending emails from the linux command line can be very useful when creating shell scripts to inform people of the completion of a scheduled crontab.

Using Linux's "mail" command

You can send emails by the following;

mail -s "Subject of Message" your@emailaddress.co.uk < /dev/null

This simplys sends a blank message to the email address of your choice.

Sending Emails with message body

In order to send emails with content in the body you can echo some output in order to pass it to the mail command

# setup the variables
$MSG="This is the body of the email address"
$SUBJET="Test Email Subject"
$ADDRESS="your@emailaddress.co.uk"

echo $MSG | mail -s $SUBJECT $ADDRESS

Alternatively if you wish to send an email, with the body text from a file you can use the following command

mail -s $SUBJECT $ADDRESS < /path/to/file.txt

comments powered by Disqus
:?>