How to send an email from command line in Linux

Sending an email from command line in Linux is very easy. You can use “mail” program in Linux to acomplish your tasks. This method requires an active (or forwarding) SMTP service running in your local machine. Couple of ways I have used this program are listed as follows,

Directly from Shell

echo “This is Mail Body” | mail -s “This is Subject” mail@example.com

 

Using a Shell Script

This method is more organized and uses a temporary file where u can put your email body. Make sure after creating this script it has executable permission to run it.

#!/bin/bash

# email subject

SUBJECT=”This is Subject”

# Email To

EMAIL=”mail@example.com”

# Email body

MESSAGE=”/tmp/email.txt”

# send the mail

mail -s “$SUBJECT” “$EMAIL” < $MESSAGE

One thought on “How to send an email from command line in Linux

Leave a Reply

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