How to Send Email From the Linux Terminal

Send Mail Featured

The Linux terminal gives us the power and capacity to perform tons of tasks with a few keyboard strokes. For those who spend most of your time in the terminal, you can also send email directly from the Terminal. This guide takes you by the hand and shows you how you can use various methods and tools to send email straight from the Linux terminal.

Note: To send email from the terminal, ensure you have a mail server set up and working correctly.

Mail

The first and simplest way to send email from the Linux terminal is to use the mail utility. This simple utility allows you to specify the recipient, email subject and even add attachments with a few options.

By default, the mail utility comes preinstalled in most Linux distributions. You can verify whether you have it installed by executing the which command as follows:

which mail

if you get a result like “/usr/bin/mail”, then it is already installed on your distro. If you don’t have mail installed by default on your Linux distro, you can install it using the package manager for your distro. For example, on Debian, you would run the command:

sudo apt-get update && sudo apt-get install mailutils

To use mail to send an email, use the mail command followed by the -s option and specify the email subject. For example, to send a message stored in the file “message.txt”, use the command:

mail -s "Hello world" info@mail.com < message.txt

The above command will read the contents of the file and use it as the message body.

You can also pass the message body from a command such as echo. For example:

echo "This is the message body" | mail -s "Hello world" info@mail.com

To add attachments to the email, use the -A option. For example:

echo "Sample odt file" | mail -s "Attachments" info@mail.com -A ~/Documents/sample.odt

Sendmail

The next utility you can use to send mail from the terminal is Sendmail, a simple yet powerful utility that can help you send email from the terminal.

If you do not have the Sendmail utility installed, you can install it:

sudo apt-get install sendmail sendmail-cf -y

To use this utility, start by creating a file containing the following as email content:

Subject: Hello World!
This is the message body
....
.....
....
...
close.

The Sendmail utility will locate the subject header and use it as the subject title for your email. You can pass this by using the command:

cat sendmail.txt | /usr/sbin/sendmail info@mail.com

Telnet

For those who spend much of their time working with remote servers, telnet is probably the go-to tool to send email. To use it, start by launching the terminal and entering the command:

telnet test.server.net 25

If you have the mail server running on a different port, replace 25 with the target port. Once connected, use telnet to say hello to a server:

helo example.com
Telnet Helo

Note that some servers will also reply to ehlo instead of helo or sometimes either.

Next, set the email sender:

MAIL FROM: info@example.com

Set the recipient of the email:

RCPT TO: demo@info.com

Compose the mail with the following format:

DATA
Subject: Hello world
Hello world,
This is the body of the email
Proceed here and terminate with
.
Finally, close the telnet session with quit.
QUIT
Telnet Compose Mail

Mutt

Mutt is another helpful utility for sending and reading email from the terminal. You may find it similar to the mail command. To install it, run the command:

sudo apt-get install mutt

To send an email with mutt, use the command:

cat sendmail.txt | mutt -s "Hello world" info@mail.com

The above command passes to the mutt utility the content of the sendmail.txt as the email body.

Wrapping Up

It can come in very handy when you’re working within a terminal-only environment to be able to send email from the Linux terminal. Read on to learn how to use the lp command to print files from the Terminal and search the Web from the terminal with S.

John Wachira

John is a technical writer at MTE, when is not busy writing tech tutorials, he is staring at the screen trying to debug code.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox