There are plenty of ways for sending an email while using GUI, using a browser or with an email client. But options are limited when it comes to command-line interface aka CLI. In this tutorial, we are going to discuss how to send mail from terminal or CLI of a Linux system.

There are plenty of commands that can be used to send mail from the terminal, like Sendmail, mutt, etc but for this tutorial, we will be discussing the use of MAIL command to send mail from the terminal. Mail command can be used directly from the terminal or we can also use it in our BASH scripts.
So let’s start the tutorial with the installation of mail command,

Recommended Read mail-command-in-linux-with-examples

On many Linux distributions, mail command is available by default but if that’s not the case with your Linux system, we can easily install it using the following commands,

CentOS/RHEL/Oracle Linux/Amazon Linux
We can use yum for the installation mail command on any of the Linux distros mentioned above,

# yum install mailx

Ubuntu/Debian/Linux Mint
For these Linux distros, we can use apt-get to install mail command on our systems,

# Sudo apt-get install mailutils

For installing mail command on fedora, execute the following command from the terminal,

# dnf install mailx

Now let’s discuss some examples on how to use mail command for sending mails from CLI,

Examples to send mail from terminal
1- Sending a simple mail
To send a simple mail with some content in the body, execute

$ mail -s “test mail”

here, -s option is used for mentioning the subject of mail followed by email address on which we need to send the mail. Now after you execute the above command, we need to enter the body content & once we are done, press CTRL + D to exit and send the mail.

We can also use the following single line command to send mail,

$ mail -s “test mail” <<< “This is the body of the mail”

2- Send mail to multiple recipients
For sending mail to more than once user, mention all email addresses followed by a comma. For example,

$ mail -s “test mail”,,com,

3- Sending a mail with attachment
Now to send a mail with a file as an attachment, we will use option ‘A’ with the mail command. For example, we need to send a file named ‘test.txt’, so we will use the following command,

$ mail -s “File Attached” -A test.txt

4- Sending a file content with mail
To send the contents of the file using mail command, we will use the following,
$ mail -s “File output”< /home/linuxteacher/file.txt

5- Sending a mail with an output of a command
We can also send the output of a command as the body content of a mail. For example, we need to send output of ‘du -h’ on a remote system, use

$ du -h | mail -s “HDD USAGE”

Similarly, we can also make use of echo command to send a mail,

$ echo “This is a body of the mail” | mail -s “test mail”

6- Sending mail with additional headers like from address
To send a mail with additional headers, we will use the option ‘a’ with the mail command. For example, we need to mention the from address & send it with our mail,

$ mail -s “Test mail “ -aFrom:DAN\

That’s it guys, we now end this tutorial on how to use the mail command to send mail from terminal or CLI. These were some common examples only, there are plenty more applications for the mail command that you can achieve. Please let us know if you need something specific you want to achieve, you can send your queries using the comment box below

Author's Bio: 

I'm Professional Digital Marketer