How to send email from the command line

A quick and easy test to make sure SMTP is working on a server is to send an email, without a desktop environment this can be done via the command line. This how to demonstrates how to do so on Ubuntu.

First, make sure you have mailutils installed, otherwise you will not be able to use the mail command.

sudo apt-get install mailutils

Once installed the basic syntax is:

mail -s “Subject” alias@domain.com < message.txt

Where the -s parameter signifies the subject and then you direct the contents of message.txt into the email.

However, usually when testing SMTP you don’t particularly want to have to create a file with the test message in. If this is the case you can always echo the message and pipe it through to the mail command, like so: