Shell command use send mail

0
(0)

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:

Use mail command on Debian, Ubuntu

For Debian and Linux Mint or other Debian forkes, the following apt-get command can be performed:

The CLI command mail is now applicable in the shell:

When you try to send an e-mail, you will probably get an error first!

The error is solved by creating the esmtprc configuration file, using vi or nano or the editor of choice:

Add an SMTP gateway, which allows us to accept email from our host.

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:

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.

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

In this example, 192.168.2.3 is our host to be sent from this email.

Apply changes of the Postfix main configuration:

Examples: Send Mail in Command-Line

Sending a previously created SQL dump should be delivered after the backup.

Here the IPv4 configuration is determined with the command ip, it is attempted to filter out the virtual interfaces by grep and awk.

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

The hostnamectl command provides useful information, available on newer Linux versions.

The call to mail shows the extensive possibilities of mailx.

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. Required fields are marked *