5 Ways to Send Emails in Linux Command Line (Recommended)

5 Ways to Send Emails in Linux Command Line (Recommended)

When you need to create an email in a shell script, you need to use the knowledge of sending emails from the command line. There are many commands in Linux that can send emails. This tutorial covers 5 of the most popular command-line mail clients, and you can choose one of them. The five commands are:

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

How it works

Let me first explain in general how the mail command in Linux delivers emails to recipients. The mail command composes mail and sends it to a local mail transfer agent (MTA, such as sendmail, Postfix). The mail server communicates with 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

The mail command is the most commonly used command for sending emails in Linux terminals. mailx is an updated version of the mail command, based on Berkeley Mail 8.1, that aims to provide the functionality of the POSIX mailx command and supports MIME, IMAP, POP3, SMTP, and S/MIME extensions. mailx is more powerful in some interactive features, such as buffering of mail messages, spam scoring and filtering, etc. On Linux distributions, the mail command is a soft link to the mailx command. You can install mail command from the official distribution repository by running the following command.

For Debian/Ubuntu systems, use APT-GET Command or APT Command to install mailutils.

$ sudo apt-get install mailutils

For RHEL/CentOS systems, use YUM Command to install mailx.

$ sudo yum install mailx

For Fedora systems, use DNF Command to install mailx.

$ sudo dnf install mailx

1a) How to send emails using mail command in Linux

The mail command is simple and easy to use. If you do not need to send attachments, you can use the following mail command format to send emails:

$ 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 add-ons on Red Hat based systems.
-A: Used to add attachments on Debian based systems.
-s: Specifies the message title.

2) How to install mutt command on Linux

mutt is another popular command for sending emails from Linux terminal. mutt is a small but powerful text-based program for reading and sending email under the Unix operating system, with support for color terminals, MIME, OpenPGP, and mail thread sorting mode. You can install mutt command from the official distribution repository by running the following command.

For Debian/Ubuntu systems, use APT-GET Command or APT Command to install mutt.

$ sudo apt-get install mutt

For RHEL/CentOS systems, use YUM Command to install mutt.

$ sudo yum install mutt

For Fedora systems, install mutt using DNF Command.

$ sudo dnf install mutt

2b) How to send emails using mutt command in Linux

It is just as easy to use as mutt. If you don't need to send attachments, you can use the following mutt command format to send emails:

$ 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 command for sending emails from Linux terminal. The mpack program encodes the named files in one or more MIME messages. The encoded message is sent to one or more recipients. You can install mpack command from the official distribution repository by running the following command.

For Debian/Ubuntu systems, use APT-GET Command or APT Command to install mpack.

$ sudo apt-get install mpack

For RHEL/CentOS systems, use YUM Command to install mpack.

$ sudo yum install mpack

For Fedora systems, use DNF Command to install mpack.

$ sudo dnf install mpack

3a) How to send emails using mpack command on Linux

mpack is also simple and easy to use. If you don't need to send attachments, you can use the following mpack command format to send emails:

$ 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" [email protected] -a test1.txt

4) How to install sendmail command on Linux

sendmail is a general-purpose SMTP server widely used on the Internet. You can also use sendmail to send emails from the command line. You can install sendmail command from the official distribution repository by running the following command.

For Debian/Ubuntu systems, use APT-GET Command or APT Command to install sendmail.

$ sudo apt-get install sendmail

For RHEL/CentOS systems, use YUM Command to install sendmail.

$ sudo yum install sendmail

For Fedora systems, use DNF Command to install sendmail.

$ sudo dnf install sendmail

4a) How to send emails using sendmail command in Linux

sendmail is also simple and easy to use. Use the sendmail command below to send the 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 tool similar to sendmail that only sends but does not receive mail. It can transfer mails from the local computer to a configured mail host (mailhub). Users can use ssmtp in the Linux command line to send emails to an SMTP server. You can install ssmtp command from the official distribution repository by running the following command.

For Debian/Ubuntu systems, use APT-GET Command or APT Command to install ssmtp.

$ sudo apt-get install ssmtp

For RHEL/CentOS systems, use YUM Command to install ssmtp.

$ sudo yum install ssmtp

For Fedora systems, use DNF Command to install ssmtp.

$ sudo dnf install ssmtp

5a) How to send emails using ssmtp command in Linux

ssmtp is also simple and easy to use. Use the following ssmtp command format to send emails.

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

Summarize

The above are 5 methods of sending emails in Linux command line introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • Connect to Linux command line code example via Python
  • Linux command line quick tips: How to locate a file
  • Some functions of using tcpdump to capture packets in the Linux command line
  • Two tools for splitting the screen in the Linux command line terminal
  • How to package Android applications through the command line in Linux
  • How to modify IP, DNS and routing command line configuration in Linux
  • How to Communicate with Other Users on the Linux Command Line

<<:  Ant Design Blazor component library's routing reuse multi-tab function

>>:  MySQL index for beginners

Recommend

Summary of pitfalls encountered in installing mysql and mysqlclient on centos7

1. Add MySQL Yum repository MySQL official websit...

Introduction to the use of several special attribute tags in HTML

The following attributes are not very compatible w...

Detailed Tutorial on Installing VirtualBox 6.0 on CentOS 8 / RHEL 8

VirtualBox is a free and open source virtualizati...

How to query duplicate data in mysql table

INSERT INTO hk_test(username, passwd) VALUES (...

Detailed introduction to nobody user and nologin in Unix/Linux system

What is the nobody user in Unix/Linux systems? 1....

MySQL establishes efficient index example analysis

This article uses examples to describe how to cre...

jQuery achieves large-screen scrolling playback effect

This article shares the specific code of jQuery t...

CSS3 uses transform to create a moving 2D clock

Now that we have finished the transform course, l...

Detailed analysis of the MySQL slow log opening method and storage format

In development projects, we can monitor SQL with ...

Summary of CSS3 practical methods (recommended)

1. Rounded border: CSS CodeCopy content to clipbo...

Vue implements simple data two-way binding

This article example shares the specific code of ...

Several solutions for forgetting the MySQL password

Solution 1 Completely uninstall and delete all da...

25 Tools to Improve Website Usability and Conversion Rates

For a website, usability refers to whether users c...

MySQL 8.0 DDL atomicity feature and implementation principle

1. Overview of DDL Atomicity Before 8.0, there wa...