How to send mail in terminal


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"

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 =

This configuration allows the user to send e-mail through 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 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

sudo vi /etc/postfix/

For mynetworks, the host IP or hostname that is allowed to send emails is added.

mynetworks =

In this example, 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" < 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 '')"; ipcalc "Asubnet" | mail -s "ssubnet"

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 '' | mail -s "ifconfig"

ifconfig is the traditional command, but newer distributions require the net-utils package to be installed.

hostnamectl | mail -s "my host"

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)

       mailx - send and receive Internet mail

              ] 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 ] 

   [-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

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published.