Home > Article > Operation and Maintenance > A summary of how to send emails under Linux
5 ways to send emails from the Linux command line
When you need to create emails 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 be used to send emails. This tutorial covers the 5 most popular command line mail clients, you can choose one of them. These 5 commands are:
mail / mailx mutt mpack sendmail ssmtp
Working principle
I will first explain how the mail command in Linux delivers the mail to the recipient as a whole. of. The mail command composes and sends mail 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 process can be seen in more detail below.
1) How to install the mail/mailx command on Linux
The mail command is the most commonly used command to send emails from the Linux terminal . mailx is an updated version of the mail command, based on Berkeley Mail 8.1, intended to provide the functionality of the POSIX mailx command and support MIME, IMAP, POP3, SMTP and S/MIME extensions. mailx is more powerful in some interactive features, such as buffering mail messages, spam scoring and filtering, etc. 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 the 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 the mail command to send emails on Linux
The mail command is simple and easy to use. If you do not need to send attachments, use the following mail command format to send emails:
$ echo "This is the mail body" | mail -s "Subject" 2daygeek@gmail.com
If you want to send attachments, use the following mail command format:
$ echo "This is the mail body" | mail -a test1.txt -s "Subject" 2daygeek@gmail.com
-a: use For adding add-ons on Red Hat-based systems.
-A: For adding attachments on Debian-based systems.
-s: Specify the message title.
2) How to install the mutt command on Linux
mutt is another popular command for sending emails from the Linux terminal. mutt is a small but powerful text-based program for reading and sending email under Unix operating systems, and supports color terminal, MIME, OpenPGP and sort-by-mail-thread modes. 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 the 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 mutt command to send emails on Linux
mutt is as simple and easy to use. If you do not need to send attachments, use the following mutt command format to send emails:
$ echo "This is the mail body" | mutt -s "Subject" 2daygeek@gmail.com
If you want to send attachments, use the following mutt command format:
$ echo "This is the mail body" | mutt -s "Subject" 2daygeek@gmail.com -a test1.txt
3 ) How to install mpack command on Linux
mpack is another popular command for sending emails on Linux terminal. The mpack program encodes named files 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 the 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 emails on Linux
mpack is also simple and easy to use. If you do not need to send attachments, use the following mpack command format to send emails:
$ echo "This is the mail body" | mpack -s "Subject" 2daygeek@gmail.com
If you want to send attachments, use the following mpack command format:
$ echo "This is the mail body" | mpack -s "Subject" 2daygeek@gmail.com -a test1.txt
4 ) How to install the sendmail command on Linux
sendmail is a universal SMTP server widely used in the world. You can also use sendmail to send emails 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 the 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 use the sendmail command to send emails on Linux
sendmail is also simple and 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 2daygeek@gmail.com < send-mail.txt
5) How to install ssmtp command on Linux
ssmtp 是类似 sendmail 的一个只发送不接收的工具,可以把邮件从本地计算机传递到配置好的 邮件主机(mailhub)。用户可以在 Linux 命令行用 ssmtp 把邮件发送到 SMTP 服务器。可以运行下面的命令从官方发行版仓库安装 ssmtp 命令。
对于 Debian/Ubuntu 系统,使用 APT-GET 命令 或 APT 命令安装 ssmtp。
$ sudo apt-get install ssmtp
对于 RHEL/CentOS 系统,使用 YUM 命令 安装 ssmtp。
$ sudo yum install ssmtp
对于 Fedora 系统,使用 DNF 命令 安装 ssmtp。
$ sudo dnf install ssmtp
5a) 如何在 Linux 上使用 ssmtp 命令发送邮件
ssmtp 同样简单易用。使用下面的 ssmtp 命令格式发送邮件。
$ echo -e "Subject: Test Mail\nThis is the mail body" > /tmp/ssmtp-mail.txt $ ssmtp 2daygeek@gmail.com < /tmp/ssmtp-mail.txt
推荐学习:《linux视频教程》
The above is the detailed content of A summary of how to send emails under Linux. For more information, please follow other related articles on the PHP Chinese website!