Send Mail 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.
How to 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:
sudo yum install -y mail
How to use mail command on Debian – Ubuntu
For Debian and Linux Mint or other Debian forkes, the following apt-get command can be performed:
sudo apt-get install heirloom-mailx
The CLI command mail is now applicable in the shell:
mail -s "Hello World" email@example.com
When you try to send an e-mail, you will probably get an error first!
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:
vi /.esmtprc
Add an SMTP gateway, which allows us to accept email from our host.
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:
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.
sudo vi /etc/postfix/main.cf
For mynetworks, the host IP or hostname that is allowed to send emails is added.
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:
sudo postfix reload

Examples: Send Mail in Command-Line
echo "message here" | mail -s "subject" email@example.com < dump.sql.gz
Sending a previously created SQL dump should be delivered after the backup.
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.
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.
hostnamectl | mail -s "my host" email@domain.com
The hostnamectl command provides useful information, available on newer Linux versions.
The mail man page shows the extensive possibilities of mailx.
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