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

Recommended Read: Complete guide to install Zimbra Mail on CentOS & RHEL

Also Read: Complete guide for setting up a mail server using Postfix, Dovecot & SquirrelMail 


Install Mail Command on Linux

On many Linux distributions, the 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

Fedora

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 the mail command for sending mails from CLI,


Linux: 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” [email protected]

here, -s option is used for mentioning the subject of mail followed by the 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” [email protected] <<< “This is the body of the mail”

2- Send mail to multiple recipients

For sending mail to more than one user, mention all email addresses followed by a comma. For example,

$ mail -s “test mail” [email protected],[email protected],com,[email protected]

3- Sending a mail with an 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” [email protected] -A test.txt

4- Sending a file content with mail

To send the contents of the file using the mail command, we will use the following,

$ mail -s “File output” [email protected] < /home/linuxtechlab/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 the output of ‘du -h’ on a remote system, use

$ du -h | mail -s “HDD USAGE”[email protected]

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

$ echo “This is the body of the mail” | mail -s “test mail” [email protected]

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\<[email protected]\> [email protected]

That’s it guys, we now end this tutorial on how to use Send mail from the terminal in Linux. 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 you queries using the comment box below.

If you think we have helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter

Linux TechLab is thankful for your continued support.