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:

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:

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:

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:

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

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:

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

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:

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:

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:

Telnet Helo

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

Next, set the email sender:

Set the recipient of the email:

Compose the mail with the following format:

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:

To send an email with mutt, use the command:

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.

3 comments

  1. So I tried this:
    echo “This is the message body” | mail -s “Hello world” eightwt@gmail.com

    and got this back:
    mail: cannot send message: Process exited with a non-zero status

    Yes, mail IS installed.eoff

  2. Can you check the logs in /var/log/mail.err. This might give a clear message as to what caused the issue.
    In most cases, check if the Postfix configuration is available in the /etc/postfix/main.cf.

  3. “To add attachments to the email, use the -a option. For example:”

    It should be ‘-A (uppercase, not lowercase). The example has it correct.

Leave a Comment

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.