search
HomeOperation and MaintenanceLinux Operation and MaintenanceA 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.

A summary of how to send emails under Linux

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!

Statement
This article is reproduced at:juejin. If there is any infringement, please contact admin@php.cn delete
Linux's Essential Components: Explained for BeginnersLinux's Essential Components: Explained for BeginnersApr 17, 2025 am 12:08 AM

The core components of Linux include the kernel, file system, shell and common tools. 1. The kernel manages hardware resources and provides basic services. 2. The file system organizes and stores data. 3. Shell is the interface for users to interact with the system. 4. Common tools help complete daily tasks.

Linux: A Look at Its Fundamental StructureLinux: A Look at Its Fundamental StructureApr 16, 2025 am 12:01 AM

The basic structure of Linux includes the kernel, file system, and shell. 1) Kernel management hardware resources and use uname-r to view the version. 2) The EXT4 file system supports large files and logs and is created using mkfs.ext4. 3) Shell provides command line interaction such as Bash, and lists files using ls-l.

Linux Operations: System Administration and MaintenanceLinux Operations: System Administration and MaintenanceApr 15, 2025 am 12:10 AM

The key steps in Linux system management and maintenance include: 1) Master the basic knowledge, such as file system structure and user management; 2) Carry out system monitoring and resource management, use top, htop and other tools; 3) Use system logs to troubleshoot, use journalctl and other tools; 4) Write automated scripts and task scheduling, use cron tools; 5) implement security management and protection, configure firewalls through iptables; 6) Carry out performance optimization and best practices, adjust kernel parameters and develop good habits.

Understanding Linux's Maintenance Mode: The EssentialsUnderstanding Linux's Maintenance Mode: The EssentialsApr 14, 2025 am 12:04 AM

Linux maintenance mode is entered by adding init=/bin/bash or single parameters at startup. 1. Enter maintenance mode: Edit the GRUB menu and add startup parameters. 2. Remount the file system to read and write mode: mount-oremount,rw/. 3. Repair the file system: Use the fsck command, such as fsck/dev/sda1. 4. Back up the data and operate with caution to avoid data loss.

How Debian improves Hadoop data processing speedHow Debian improves Hadoop data processing speedApr 13, 2025 am 11:54 AM

This article discusses how to improve Hadoop data processing efficiency on Debian systems. Optimization strategies cover hardware upgrades, operating system parameter adjustments, Hadoop configuration modifications, and the use of efficient algorithms and tools. 1. Hardware resource strengthening ensures that all nodes have consistent hardware configurations, especially paying attention to CPU, memory and network equipment performance. Choosing high-performance hardware components is essential to improve overall processing speed. 2. Operating system tunes file descriptors and network connections: Modify the /etc/security/limits.conf file to increase the upper limit of file descriptors and network connections allowed to be opened at the same time by the system. JVM parameter adjustment: Adjust in hadoop-env.sh file

How to learn Debian syslogHow to learn Debian syslogApr 13, 2025 am 11:51 AM

This guide will guide you to learn how to use Syslog in Debian systems. Syslog is a key service in Linux systems for logging system and application log messages. It helps administrators monitor and analyze system activity to quickly identify and resolve problems. 1. Basic knowledge of Syslog The core functions of Syslog include: centrally collecting and managing log messages; supporting multiple log output formats and target locations (such as files or networks); providing real-time log viewing and filtering functions. 2. Install and configure Syslog (using Rsyslog) The Debian system uses Rsyslog by default. You can install it with the following command: sudoaptupdatesud

How to choose Hadoop version in DebianHow to choose Hadoop version in DebianApr 13, 2025 am 11:48 AM

When choosing a Hadoop version suitable for Debian system, the following key factors need to be considered: 1. Stability and long-term support: For users who pursue stability and security, it is recommended to choose a Debian stable version, such as Debian11 (Bullseye). This version has been fully tested and has a support cycle of up to five years, which can ensure the stable operation of the system. 2. Package update speed: If you need to use the latest Hadoop features and features, you can consider Debian's unstable version (Sid). However, it should be noted that unstable versions may have compatibility issues and stability risks. 3. Community support and resources: Debian has huge community support, which can provide rich documentation and

TigerVNC share file method on DebianTigerVNC share file method on DebianApr 13, 2025 am 11:45 AM

This article describes how to use TigerVNC to share files on Debian systems. You need to install the TigerVNC server first and then configure it. 1. Install the TigerVNC server and open the terminal. Update the software package list: sudoaptupdate to install TigerVNC server: sudoaptinstalltigervnc-standalone-servertigervnc-common 2. Configure TigerVNC server to set VNC server password: vncpasswd Start VNC server: vncserver:1-localhostno

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools