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

How to delete the container created in Docker

How to delete the container created in Docker 1. ...

Example code for implementing the "plus sign" effect with CSS

To achieve the plus sign effect shown below: To a...

N ways to achieve two-column layout with CSS

1. What is a two-column layout? There are two typ...

Detailed explanation of how to use eslint in vue

Table of contents 1. Description 2. Download rela...

Detailed explanation of the use of MySQL paradigm

1. Paradigm The English name of the paradigm is N...

Vue implements an example of pulling down and scrolling to load data

Table of contents Step 1: Installation Step 2: Ci...

Explanation on whether to choose paging or loading in interactive design

The author of this article @子木yoyo posted it on hi...

Completely uninstall mysql. Personal test!

Cleanly uninstall MySQL. Personally tested, this ...

Implementation of Docker deployment of Tomcat and Web applications

1. Download docker online yum install -y epel-rel...

MySQL batch removes spaces in a certain field

Is there any way to remove spaces from a certain ...

How to expand the disk partition for centos system

Problem/failure/scenario/requirement The hard dis...