Mailx: send mail using command line

mail (mailx) is a command under Linux that lets you send e-mails from the command line. In this article we’ll look at how to send mail from a RHEL/Centos or Ubuntu mail server.

mailx installation

To install mailx on RHEL 8/CentOS 8, just use dnf commande as bellow :

[root@RHEL8 ~]# dnf install mailx
Puppet Labs PC1 Repository el 7 - x86_64 973 kB/s | 12 MB 00:12
CentOS-7 - Base 729 kB/s | 10 MB 00:14
CentOS-7 - Updates 1.0 MB/s | 10 MB 00:10
CentOS-7 - Extras 550 kB/s | 292 kB 00:00
Dependencies resolved.
Package Arch Version Repository Size
mailx x86_64 12.5-19.el7 base 245 k
Transaction Summary
Install 1 Package
Total download size: 245 k
Installed size: 466 k
Is this ok [y/N]: y
Downloading Packages:
mailx-12.5-19.el7.x86_64.rpm 149 kB/s | 245 kB 00:01
Total 136 kB/s | 245 kB 00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : mailx-12.5-19.el7.x86_64 1/1
Verifying : mailx-12.5-19.el7.x86_64 1/1

For Ubuntu, you can install it by running the command bellow :

[root@postfix ~]# apt-get install bsd-mailx

Send a simple email :

To make a simple test , run the commande bellow to send an email to « » :

[root@RHEL8 ~]# mail -s "subject"

When you have finished typing the message, press Ctrl+D and the word EOT will be displayed :

[root@RHEL8 ~]# mail -s "mail subject"
Your message

Mail from a file with mailx

You can send an email existing in a file by running the command bellow :

[root@RHEL8 ~]# mail -s "Sujet du mail" < /path/to/file

Send an e-mail to multiple recipients

[root@RHEL8 ~]# echo "Example of mail" | mail -s "Subject",

CC and BB :

The -a and -b options can be used to add CC and BB addresses respectively :

[root@RHEL8 ~]# echo "Example of mail" | mail -s "Mail Object" -c

Attach a file :

The attachment can be added with the -a option :

[root@RHEL8 ~]# echo "Example of mail" | mail -s "Mail object" -r "pierre<>" -a /path/to/file

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *