send mail mailx command

0
(0)

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" 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 e-mail 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

Set up a postfix smtp relay

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

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

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.

Wie hilfreich war dieser Beitrag?

Klicke auf die Sterne um zu bewerten!

Durchschnittliche Bewertung 0 / 5. Anzahl Bewertungen: 0

Bisher keine Bewertungen! Sei der Erste, der diesen Beitrag bewertet.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert