Five ways to send mail from Linux command line (recommended)

Time:2020-9-2

When you need to create mail in a shell script, you need to use the knowledge of sending mail from the command line. There are many commands in Linux that can send mail. You can choose one of the five most popular command-line mail clients in this tutorial. The five commands are:

  • mail / mailx
  • mutt
  • mpack
  • sendmail
  • ssmtp

working principle

I’ll first explain how the mail command in Linux delivers the mail to the recipient. The mail command writes the message and sends it to a local mail transfer agent (MTA, such as sendmail, postfix). Communication between the mail server and the remote mail server to actually send and receive mail. The following process can be seen in more detail.

1) How to install mail / mailx command on Linux

Mail command is the most used command in Linux terminal to send mail. Mailx is an updated version of the mail command, which is based on Berkeley mail 8.1. It aims to provide the function of POSIX mailx command, and supports mime, IMAP, POP3, SMTP and S / MIME extensions. Mailx is more powerful in some interactive features, such as buffering email messages, spam rating and filtering. On Linux distributions, the mail command is a soft link to the mailx command. You can run the following command to install the mail command from the official distribution repository.

For Debian / Ubuntu systems, use the apt get command or apt command to install mailutils.

$ sudo apt-get install mailutils

For RHEL / CentOS systems, use the yum command to install mailx.

$ sudo yum install mailx

For Fedora systems, use the DNF command to install mailx.

$ sudo dnf install mailx

1A) how to use mail command to send mail on Linux

The mail command is easy to use. If you don’t need to send an attachment, use the following mail command format to send mail:


$ echo "This is the mail body" | mail -s "Subject" [email protected]

If you want to send an attachment, use the following mail command format:


$ echo "This is the mail body" | mail -a test1.txt -s "Subject" [email protected]

-a: Used to add attachments to a red hat based system.
-A: Used to add attachments on Debian based systems.
-s: Specifies the message title.

2) How to install the mutt command on Linux

Mutt is another popular email command on Linux terminals. Mutt is a small and powerful text-based program, which is used to read and send e-mail under UNIX operating system, and supports color terminal, mime, OpenPGP and sorting mode by mail clue. You can run the following command to install the mutt command from the official distribution repository.

For Debian / Ubuntu systems, use the apt-get command or apt command to install mutt.

$ sudo apt-get install mutt

For RHEL / CentOS systems, use the yum command to install mutt.

$ sudo yum install mutt

For Fedora systems, use the DNF command to install mutt.

$ sudo dnf install mutt

2b) how to use the mutt command to send mail on Linux

It’s just as easy to use as mutt. If you don’t need to send attachments, use the following mutt command format to send mail:


$ echo "This is the mail body" | mutt -s "Subject" [email protected]

If you want to send an attachment, use the following mutt command format:


$ echo "This is the mail body" | mutt -s "Subject" [email protected] -a test1.txt

3) How to install mpack command on Linux

Mpack is another popular email command on Linux terminals. The mpack program encodes the named file in one or more mime messages. The encoded message is sent to one or more recipients. You can run the following command to install the mpack command from the official distribution repository.

For Debian / Ubuntu systems, use the apt-get command or apt command to install mpack.


$ sudo apt-get install mpack

For RHEL / CentOS systems, use the yum command to install mpack.

$ sudo yum install mpack

For Fedora systems, use the DNF command to install mpack.

$ sudo dnf install mpack

3a) how to use mpack command to send mail on Linux

Mpack is also easy to use. If you don’t need to send an attachment, you can send an email using the following mpack command format:


$ echo "This is the mail body" | mpack -s "Subject" [email protected]

If you want to send an attachment, use the following mpack command format:


$ echo "This is the mail body" | mpack -s "Subject" 2[email protected] -a test1.txt

4) How to install sendmail command on Linux

Sendmail is a widely used general-purpose SMTP server on. You can also use sendmail to send e-mail from the command line. You can run the following command to install the sendmail command from the official distribution repository.

For Debian / Ubuntu systems, use the apt get command or apt command to install sendmail.

$ sudo apt-get install sendmail

For RHEL / CentOS systems, use the yum command to install sendmail.

$ sudo yum install sendmail

For Fedora systems, use the DNF command to install sendmail.

$ sudo dnf install sendmail

4a) how to send mail using sendmail command on Linux

Sendmail is also easy to use. Use the sendmail command below to send mail.

$ echo -e "Subject: Test Mail\nThis is the mail body" > /tmp/send-mail.txt
$ sendmail [email protected] < send-mail.txt

5) How to install ssmtp command on Linux

Ssmtp is a sendmail like tool that only sends but does not receive. It can deliver mail from the local computer to the configured mail hub. Users can use ssmtp to send mail to the SMTP server on the Linux command line. You can run the following command to install the ssmtp command from the official distribution repository.

For Debian / Ubuntu systems, use the apt get command or apt command to install ssmtp.

$ sudo apt-get install ssmtp

For RHEL / CentOS systems, use the yum command to install ssmtp.

$ sudo yum install ssmtp

For Fedora systems, use the DNF command to install ssmtp.

$ sudo dnf install ssmtp

5a) how to use ssmtp command to send mail on Linux

Ssmtp is also easy to use. Use the ssmtp command format below to send the message.


$ echo -e "Subject: Test Mail\nThis is the mail body" > /tmp/ssmtp-mail.txt
$ ssmtp [email protected] < /tmp/ssmtp-mail.txt

summary

The above is a small editor to introduce you in the Linux command line to send mail five methods, I hope to help you, if you have any questions, please leave me a message, Xiaobian will reply to you in time. Thank you very much for your support to the developeppaer website!
If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!