惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog
D
Docker
J
Java Code Geeks
腾讯CDC
Blog — PlanetScale
Blog — PlanetScale
G
Google Developers Blog
M
MIT News - Artificial intelligence
L
LangChain Blog
T
The Blog of Author Tim Ferriss
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
博客园 - Franky
GbyAI
GbyAI
Hugging Face - Blog
Hugging Face - Blog
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
N
Netflix TechBlog - Medium
B
Blog RSS Feed
Y
Y Combinator Blog
阮一峰的网络日志
阮一峰的网络日志
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News

DigitalOcean Community Tutorials

It's Time to Break Up with Your Cloud: Why AI Teams are Switching We Built a Private-Document AI App to Test Platform Security. Here Is What We Could Actually Verify. PostgreSQL Explained: A Complete Beginner-to-Advanced Guide How To Install and Configure Postfix on Ubuntu How To Build a Web Application Using Flask in Python 3 Build AI Reading List with DigitalOcean Functions and Mistral How To Concatenate Strings in Python How to Allow MySQL Remote Access Securely How To Install and Use Docker on Rocky Linux How To Build a Multi-Agent AI System with Docker Agent DSPy Use Cases: Build Optimized LLM Pipelines How To Submit AJAX Forms with jQuery Build an AI-Powered GPU Fleet Optimizer with the DigitalOcean AI Platform ADK Monitor GPU Utilization in Real Time: A Complete Guide Reduce File Size of Images in Linux - CLI and GUI methods Reduce PDF File Size in Linux: Tools and Methods How To Set Up a Private Docker Registry on Ubuntu How To Troubleshoot Terraform: Errors and Fixes How to Use Go Modules Python Multiprocessing Example: Process, Pool & Queue Convert Class Components to Functional Components with React Hooks How To Install and Configure Ansible on Ubuntu LLM Tokenizers Simplified: BPE, SentencePiece, and More How To Monitor System Authentication Logs on Ubuntu How to Use Traceroute and MTR to Diagnose Network Issues How to Deploy Postgres to Kubernetes Cluster Importing Packages in Go: A Complete Guide Create RAID Arrays with mdadm on Ubuntu How To Make an HTTP Server in Go How To Set Up Time Synchronization on Ubuntu
How to Send Email in Linux from the Command Line
Anish Singh Walia · 2022-08-04 · via DigitalOcean Community Tutorials

Introduction

You send email from a Linux shell when you want cron alerts, backup notices, or script output in an inbox. The mail command handles plain text on a server with a local Mail Transfer Agent (MTA). mutt adds reliable attachments. msmtp connects to Gmail, Microsoft 365, or any SMTP host that needs a username and password.

This tutorial walks through installation steps, one-line tests, SMTP setup, and Bash automation.

The following commands were checked on Ubuntu 22.04 LTS, 24.04 LTS and 26.04 LTS. RHEL-family systems use dnf instead of apt.

Key takeaways

  • Install mailutils on Debian/Ubuntu (sudo apt install mailutils -y) or mailx on RHEL (sudo dnf install mailx).
  • Send a one-liner with echo "body" | mail -s "subject" user@example.com. Use ASCII hyphens in flags (-s), not special dash characters.
  • mail -A file attaches a file on GNU mailutils. mutt -a file is more reliable for MIME types in scripts.
  • External SMTP (Gmail, etc.) needs msmtp or a Postfix relay. ssmtp is obsolete and removed from current Debian/Ubuntu repos.
  • Gmail and Google Workspace need an App Password (with 2FA on) or OAuth. Plain account passwords often fail.
  • Submission port 587 with STARTTLS is the usual choice. Port 465 uses implicit TLS (tls_starttls off in msmtp).
  • Set chmod 600 ~/.msmtprc so only your user reads SMTP credentials.
  • Pair mutt with msmtp when scripts must send attachments through authenticated SMTP.

Prerequisites

  • A DigitalOcean Droplet running Ubuntu 22.04/24.04/26.04 or AlmaLinux/Rocky 8+.
  • sudo access to install packages.
  • For outbound internet mail: an SMTP account or a properly configured MTA.

You will learn about the following tools in this tutorial:

  • mail / mailx
  • mutt
  • mpack
  • sendmail (MTA interface)
  • msmtp (authenticated SMTP client)

1. Using the mail or mailx command

Linux mail is the classic client for short text messages. On Debian and Ubuntu, mail comes from the mailutils package and often depends on Postfix as the MTA. On RHEL, AlmaLinux, and Rocky Linux, install the mailx package.

Install mail on Debian and Ubuntu

  1. sudo apt update
  2. sudo apt install mailutils -y

Postfix may prompt you during install. Choose Internet Site unless you run a complex mail hub. Press Tab to move between dialog options, then Enter.

Postfix Configuration

Internet Site option mail command

Install mail on RHEL, AlmaLinux, and Rocky Linux

  1. sudo dnf install mailx -y

On older CentOS 7 systems, yum install mailx still works.

Test the mail command (interactive)

  1. mail -s "Test Email" email_address

Replace email_address with your address:

  1. mail -s "Test Email" james@example.com

Press Enter at the Cc: prompt if you do not need a copy. Type the body, press Enter, then press Ctrl+D to send.

Linux Mail Command Example

Linux send email example

Test the mail command (one line)

  1. echo "sample message" | mail -s "sample mail subject" email_address

Example:

  1. echo "Hello world" | mail -s "Test" james@example.com

Linux email with subject

Set the From address

Many providers require a valid sender. Use -r:

  1. echo "Hello" | mail -s "Test" -r "Server Alerts <alerts@example.com>" james@example.com

Send an attachment with mail

GNU mailutils uses -A for attachments:

  1. mail -s "subject" -A message.txt email_address

Example:

  1. mail -s "Important Notice" -A message.txt james@example.com

Linux Send email with attachment

Linux email with attachment

Send to multiple recipients

  1. mail -s "test header" email_address email_address2

2. Using the mailx command

mailx is the POSIX name for the same family of clients. On current Ubuntu, mail and mailx often point to the same binary after you install mailutils. Some distros ship a separate bsd-mailx package with different flags. Run mailx --version or man mailx on your host before you rely on attachment syntax.

Install mailx on Debian and Ubuntu (optional standalone)

  1. sudo apt install bsd-mailx

Install mailx on RHEL family

  1. sudo dnf install mailx -y

Test mailx with echo

  1. echo "message body" | mailx -s "subject" email_address

Example:

  1. echo "Make the most out of Linux!" | mailx -s "Welcome to Linux" james@example.com

3. Using the mutt command

mutt is a lightweight MUA. Use it when you need dependable MIME attachments or when you pipe a body from a script.

Install mutt

Debian/Ubuntu:

  1. sudo apt install mutt -y

RHEL family:

  1. sudo dnf install mutt -y

Send a blank body

  1. mutt -s "Test Email" email_address < /dev/null

Example:

  1. mutt -s "Greetings" james@example.com < /dev/null

Mutt Blank Email

Send with an attachment

  1. echo "Message body" | mutt -a "/path/to/file.to.attach" -s "subject of message" -- email_address

Example:

  1. echo "Hey team, see attached report." | mutt -a report.doc -s "Notice" -- james@example.com

The -- token ends option parsing so the address is not read as a flag.

Linux Mutt Send Email with File Attachment

Mutt Email File Attached

Point mutt at msmtp

After you configure msmtp (section 6), add to ~/.muttrc:

set sendmail="/usr/bin/msmtp"

Then mutt delivers through your SMTP account instead of the default Postfix queue.

4. Using the mpack command

mpack encodes a file into a MIME message. It still needs a sendmail-compatible program to deliver the message.

Install mpack

  1. sudo apt install mpack -y
  1. sudo dnf install mpack -y

Send with mpack

  1. mpack -s "Subject here" file email_address

Example:

  1. mpack -s "Sales Report" report.doc james@example.com

Mpack Command

Linux Mpack Send Email Attach File

5. Using sendmail

sendmail here means the sendmail-compatible MTA interface (/usr/sbin/sendmail), not a tutorial on running a full Sendmail server. Postfix on Ubuntu provides this binary.

Install sendmail (Sendmail MTA) on Debian/Ubuntu

Most Ubuntu users already have Postfix from mailutils. To install the Sendmail MTA package instead:

  1. sudo apt install sendmail -y

Install sendmail on RHEL family

  1. sudo dnf install sendmail -y

Send a file through the sendmail interface

Create report.txt:

Hello there!

Send:

  1. sendmail james@example.com < report.txt

Sendmail Output

Add a Subject header

Put headers at the top of the file, then a blank line, then the body:

Subject: Sendmail test email
From: alerts@example.com

Hello there!

6. Send email with SMTP authentication (msmtp)

Gmail, Yahoo, and Microsoft 365 reject unauthenticated relay from random VPS IPs. Use msmtp as a lightweight SMTP client. ssmtp was common years ago but is unmaintained and absent from current Debian/Ubuntu repositories. Prefer msmtp.

Install msmtp

Debian/Ubuntu (includes a sendmail wrapper when you install msmtp-mta):

  1. sudo apt install msmtp msmtp-mta -y

RHEL family (EPEL may be required on minimal images):

  1. sudo dnf install msmtp -y

Configure msmtp for Gmail (port 587)

Create or edit ~/.msmtprc:

defaults
auth           on
tls            on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile        ~/.msmtp.log

account        gmail
host           smtp.gmail.com
port           587
from           your_gmail_address@gmail.com
user           your_gmail_address@gmail.com
password       your_app_password_here
tls_starttls   on

account default : gmail

Replace the address and password. Google accounts with 2FA need an App Password from the Google Account security page. I have not verified every Workspace admin policy. Some orgs block SMTP entirely.

On RHEL, the CA bundle path might be /etc/pki/tls/certs/ca-bundle.crt. Run:

  1. ls /etc/ssl/certs/ca-certificates.crt /etc/pki/tls/certs/ca-bundle.crt 2>/dev/null

Lock down the config file:

  1. chmod 600 ~/.msmtprc

Send a test message with msmtp (port 587)

  1. msmtp -a gmail recipient@example.com <<EOF
  2. From: your_gmail_address@gmail.com
  3. To: recipient@example.com
  4. Subject: Test Email with msmtp
  5. This is the body of the email.
  6. EOF

Use mail or mutt to send email with msmtp

With msmtp-mta installed, mail and mutt often pick up /usr/bin/msmtp as the sendmail binary. You still pass -r for the visible sender when needed:

  1. echo "This is a test email sent via msmtp." | mail -s "msmtp Test" -r your_gmail_address@gmail.com recipient@example.com

Configure msmtp for Gmail on port 465 (implicit TLS)

Some networks block 587. Try this account block instead of the 587 block above:

account gmail465
host           smtp.gmail.com
port           465
from           your_gmail_address@gmail.com
user           your_gmail_address@gmail.com
password       your_app_password_here
tls            on
tls_starttls   off

account default : gmail465

7. Send email from Bash scripts

Disk usage alert

#!/bin/bash

disk_usage=$(df -h / | awk 'NR==2 {print $5}')

if [[ ${disk_usage%\%} -gt 90 ]]; then
  echo "Warning: Disk usage on / is above 90% ($disk_usage)" | \
    mail -s "Disk Space Alert" admin@example.com
fi

Schedule with cron only after you confirm mail delivery from the host.

Send a log file attachment

#!/bin/bash

echo "This is a sample log message." > mylog.txt

echo "Log attached." | mutt -s "Log File" -a mylog.txt -- admin@example.com < /dev/null

For complex MIME, you should prefer mutt over mail -A as it provides a more reliable attachment handling.

Tool Best for Pros Cons
mail / mailx Quick text, local MTA already working Simple one-liners, common on servers Weak attachment story, no built-in remote SMTP auth
msmtp Scripts that must use Gmail/365 SMTP Stores credentials in one config file Send-only, needs provider setup
mutt Attachments and scripted MIME Solid -a attachment support Needs a sendmail binary (msmtp or Postfix)
mpack MIME encode one file Small utility Depends on sendmail interface
sendmail System MTA plumbing Standard pipe interface Not a friendly MUA for humans

Postfix is the usual alternative to Sendmail as a full MTA on Linux. For scripted outbound mail to public providers, Postfix relay plus msmtp covers most homelab and VPS cases without running Sendmail proper.

FAQs

1. How do you send an email via the command line?

Pipe text into mail:

  1. echo "This is the body of the email." | mail -s "Email Subject" recipient@example.com

Install mailutils on Ubuntu first. Confirm delivery with mailq or your provider’s sent folder when using SMTP.

2. How do you send an email in the Linux mail command?

Same syntax. Interactive mode:

  1. mail -s "Subject" user@example.com

Type the body, then press Ctrl+D. One-liner:

  1. echo "Body text" | mail -s "Subject" user@example.com

3. How do you send email via SMTP from the Linux command line?

  1. Install msmtp.
  2. Write ~/.msmtprc with host, port, user, and password (or App Password).
  3. Run chmod 600 ~/.msmtprc.
  4. Send with msmtp -a account_name recipient@example.com and RFC822 headers.

Relaying through Postfix is another path. See How To Install and Configure Postfix on Ubuntu 22.04.

4. Can you send an email from the Linux terminal?

Yes. You need either a working local MTA for your domain or an SMTP client like msmtp for provider mailboxes. Terminal MUAs (mail, mutt) only compose and hand off the message.

5. How do you send SMTP email from the command line in Linux?

Use msmtp with STARTTLS on port 587, or implicit TLS on port 465. Test with a here-document that includes From, To, and Subject headers, then a blank line, then the body.

6. Is SMTP port 587 or 465?

Both are valid today. 587 expects STARTTLS after connect (tls_starttls on in msmtp). 465 wraps TLS from the first byte (tls_starttls off). Gmail documents both. Pick one and match your firewall rules.

7. What is the sendmail command?

sendmail is the traditional interface to the system MTA. You pipe a message file into /usr/sbin/sendmail. On Ubuntu with Postfix, the binary is provided by Postfix even though the command name is sendmail. Reading a message ends with Ctrl+D or a line with only a dot, depending on mode.

What’s next

Pick the path that matches your delivery target. Local root mail and cron reports often work with mail plus Postfix. External inboxes need msmtp or a Postfix relay. Attachments in automation usually mean mutt.

Continue with:

Run alerts and cron jobs on a DigitalOcean Droplet without managing hardware.

Still looking for an answer?

Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.