There are plenty of ways for sending email while using GUI , using 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 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 terminal or we can also use it in our BASH scripts.
So let’s start the tutorial with the installation of mail command.
Installation
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
Fedora
For installing mail command on fedora, execute the following command from 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 body, execute
$ mail -s “test mail” admin@linuxtechlab.com
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” admin@linuxtechlab.com <<< “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” user1@linuxtechlab.com,user2@linuxtechlab.,com,user3@linuxtechlab.,com
3- Sending a mail with attachment
Now to send a mail with a file as attachment, we will use option ‘A’ with mail command. For example, we need to send a file named ‘test.txt’, so we will use the following command,
$ mail -s “File Attached” admin@linuxtechlab.com -A test.txt
4- Sending a file content with mail
To send the contents of file using mail command, we will use the following,
$ mail -s “File output” admin@linuxtechlab.com < /home/linuxtechlab/file.txt
5- Sending a mail with output of a command
We can also send 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”admin@linuxtechlab.com
Similarly, we can also make use of echo command to send a mail,
$ echo “This is body of the mail” | mail -s “test mail” admin@linuxtechlab.com
6- Sending mail with additional headers like from address
To send a mail with additional headers, we will use option ‘a’ with mail command. For example, we need to mention the from address & send it with our mail,
$ mail -s “Test mail “ -aFrom:DAN\<dan@linuxtechlab.com\> admin@linuxtechlab.com
That’s it guys, we now end this tutorial on how to use mail command to send mail from terminal or CLI