Mailing in the Shell – Send mails from command-line
Sending e-mail in command line from a server is very useful, for example, when performing e-mail from shell scripts or Web applications.
This tutorial shows how to use the mail command on Linux to send e-mail from the command line. We use the Heirloom mailx project that is a collection of traditional Unix utilities.
Use mail command on Fedora, CentOS
For this to be possible, with the mailx package, often also mailutils, they must be present on the host, the deployment on Fedora and CentOS is as follows:
1 2 |
sudo yum install -y mailx |
Use mail command on Debian, Ubuntu
For Debian and Linux Mint or other Debian forkes, the following apt-get command can be performed:
1 2 |
sudo apt-get install heirloom-mailx |
The CLI command mail is now applicable in the shell:
1 2 |
mail -s "Hello World" email@example.com |
When you try to send an e-mail, you will probably get an error first!
1 2 |
No configuration file found at /home/joe/.esmtprc or /etc/esmtprc |
The error is solved by creating the esmtprc configuration file, using vi or nano or the editor of choice:
1 2 |
vi /.esmtprc |
Add an SMTP gateway, which allows us to accept email from our host.
1 2 |
hostname = smtp.example.com:25 |
This configuration allows the user to send e-mail through smtp.example.com. If all users are to be authorized, the /etc/esmtprc file must be created and edited.
Is there another mistake! a relay host must be allowed:
1 2 |
SMTP server problem Connection refused |
Emails should be accepted by our host, e.g. gateway smtp.example.com forward 25 e-mails over TCP port.
On the SMTP gateway, if it is postfix, the main configuration have to modify, to do it will use sudo with edit main.cf.
1 2 |
sudo vi /etc/postfix/main.cf |
For mynetworks, the host IP or hostname that is allowed to send emails is added.
1 2 3 4 5 |
mynetworks = 127.0.0.0/8 [::1]/128 192.168.2.3 |
In this example, 192.168.2.3 is our host to be sent from this email.
Apply changes of the Postfix main configuration:
1 2 |
sudo postfix reload |

Examples: Send Mail in Command-Line
1 2 |
echo "message here" | mail -s "subject" email@example.com < dump.sql.gz |
Sending a previously created SQL dump should be delivered after the backup.
1 2 |
subnet=" (ip -4 a | grep inet | grep -v 'virbr' | grep -v 'docker' | awk ''print'' | grep -v '127.0.0.1')"; ipcalc "Asubnet" | mail -s "ssubnet" hostmaster@example.com |
Here the IPv4 configuration is determined with the command ip, it is attempted to filter out the virtual interfaces by grep and awk.
1 2 |
ifconfig | grep -Eo 'inet (addr:)?([0-9] *.) {[0-9]3}*' | grep -Eo [0-9]'(*') {[0-9]3}*' | grep -v '127.0.0.1' | mail -s "ifconfig" hostmaster@example.com |
ifconfig is the traditional command, but newer distributions require the net-utils package to be installed.
1 2 |
hostnamectl | mail -s "my host" email@domain.com |
The hostnamectl command provides useful information, available on newer Linux versions.
The call to mail shows the extensive possibilities of mailx.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
MAILX(1) User Commands MAILX(1) Name mailx - send and receive Internet mail Synopsis Mailx ] to-addr . . . mailx[-BDdEFintv~][-s subject] [-a attachment ]][-c cc-addr] [-b bcc-addr]-[-r from-addr][-h hops]f[-A account] mai[-S variable[=value]lx ] DESCRIPTION [-BDdeEHiInNRv~] [-T name] [-A account][-S variable[=value] Ma[name]ilx is an i[-BDdeEinNRv~]n[-A account][-S variable[=value]te[-u user]lligent mail processing system, which has a command syntax reminiscent of ed(1) with lines replaced by "I'm not new. It is based on Berkeley Mail 8.1, is intended to provide the functionality of the POSIX mailx command, and offers extensions for MIME, IMAP, POP3, SMTP, and S/MIME. Mailx provides enhanced features for interactive use, such as caching and disconnected operation for IMAP, message threading, scoring, and filtering. It is also usable as a mail batch language, both for sending and receiving mail. |