send mail mailx command


Send e-mail in the terminal shell using mail or mailx command

Sending e-mail in command line from a server is very useful, for example, when performing e-mail from shell scripts or web applications.

send mail mailx command

send e-mail in the command line

This tutorial shows how to use the mail and mailx 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 the mail command on Fedora

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

Using mailx utility 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 e-mail 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

Set up a postfix smtp relay

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

Example: Send e-mail in the 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.

more related in this post

Heirloom mailx is a mail user agent for Unix systems. Highlights are:

Derived from Berkeley Mail 8.1. An interface like the original Berkeley one is still optionally available. Is a free implementation of the System V mailx command and features an interface like that by default.

Is intended to comply to the mailx command specifications of IEEE Std 1003.2-1992 (POSIX.2) and IEEE Std 1003.1-2004 with XSI extensions (SUSv3). Supports the MIME specifications. Mailx command can send mail and receive attachments and code and decode international character strings.

If the system libraries provide appropriate support, conversion between different character encodings is performed. In combination with a UTF-8 terminal, nearly all international languages are supported. External converter programs can be automatically invoked e. g. to view messages in HTML format. Supports IMAP.

In combination with either OpenSSL or Mozilla NSS, IMAPS can also be used. With IMAP, folder hierarchies can be stored and managed on remote servers. This is particularly useful for people who use more than one computer to access their mail. Of course, it is also possible to use IMAP just for fetching mails and storing them locally.

Supports caching and disconnected operation of IMAP folders.

This speeds up operation while in online mode. It also makes it possible to transparently view and edit IMAP mailboxes without an active connection to the server. Supports POP3 to read messages on a remote server. In combination with OpenSSL or NSS, POP3S can also be used. Mailx can read and delete individual messages, thus POP3 accounts can almost be accessed like local mail folders.

Supports SMTP to send messages directly to a remote server.

A local sendmail interface setup is thus not necessary. In combination with OpenSSL or NSS, both the STARTTLS method and SMTPS can be used. SMTP AUTH is also supported. Supports S/MIME for signed and encrypted email (in combination with OpenSSL or NSS). In combination with NSS, certificates for S/MIME and SSL/TLS can be shared with Mozilla applications, and can be managed using them.

Can display message threads and supports operations on them. This is especially useful for handling mailing lists. It is also possible to sort messages by various other criteria. Provides a Bayesian junk mail filter mostly according to Paul Graham’s article “Better Bayesian Filtering”.

Chained tokens according to Jonathan A. Zdziarski’s “Advanced Language Classification using Chained Tokens” can optionally be used. Features a lot of detail improvements over previous implementations of Mail/mailx, such as IMAP-style search methods and flags for any types of folders, killing and scoring of messages, maildir folder support, an option to set the From: address directly, and the generation of References:

header fields to avoid annoyances on mailing lists. Can be used as a mail batch language in nearly the same way as it is used interactively. So it is possible to use mailx as a mailbox filter, for fetching mails, or for sending files as attachments. Is extensively described in its manual page mailx(1), which provides both introductory material and complete reference documentation.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *