How to Send Email in Linux from the Command Line

Jun 02, 2026 07:00 AM - 5 days ago 4616

Introduction

You nonstop email from a Linux ammunition erstwhile you want cron alerts, backup notices, or script output successful an inbox. The message bid handles plain matter connected a server with a section Mail Transfer Agent (MTA). mutt adds reliable attachments. msmtp connects to Gmail, Microsoft 365, aliases immoderate SMTP big that needs a username and password.

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

The pursuing commands were checked connected Ubuntu 22.04 LTS, 24.04 LTS and 26.04 LTS. RHEL-family systems usage dnf alternatively of apt.

Key takeaways

  • Install mailutils connected Debian/Ubuntu (sudo apt instal mailutils -y) or mailx connected RHEL (sudo dnf instal mailx).
  • Send a one-liner pinch echo "body" | message -s "subject" [email protected]. Use ASCII hyphens successful flags (-s), not typical dash characters.
  • mail -A file attaches a record connected GNU mailutils. mutt -a file is more reliable for MIME types successful scripts.
  • External SMTP (Gmail, etc.) needs msmtp aliases a Postfix relay. ssmtp is obsolete and removed from existent Debian/Ubuntu repos.
  • Gmail and Google Workspace request an App Password (with 2FA on) aliases OAuth. Plain relationship passwords often fail.
  • Submission larboard 587 pinch STARTTLS is the accustomed choice. Port 465 uses implicit TLS (tls_starttls disconnected successful msmtp).
  • Set chmod 600 ~/.msmtprc truthful only your personification sounds SMTP credentials.
  • Pair mutt pinch msmtp erstwhile scripts must nonstop attachments through authenticated SMTP.

Prerequisites

  • A DigitalOcean Droplet moving Ubuntu 22.04/24.04/26.04 aliases AlmaLinux/Rocky 8+.
  • sudo entree to instal packages.
  • For outbound net mail: an SMTP relationship aliases a decently configured MTA.

You will study astir the pursuing devices successful this tutorial:

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

1. Using the message aliases mailx command

Linux message is the classical customer for short matter messages. On Debian and Ubuntu, message comes from the mailutils package and often depends connected Postfix arsenic the MTA. On RHEL, AlmaLinux, and Rocky Linux, instal the mailx package.

Install message connected Debian and Ubuntu

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

Postfix whitethorn punctual you during install. Choose Internet Site unless you run a analyzable message hub. Press Tab to move betwixt dialog options, past Enter.

Postfix Configuration

Internet Site action message command

Install message connected RHEL, AlmaLinux, and Rocky Linux

  1. sudo dnf install mailx -y

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

Test the message bid (interactive)

  1. mail -s "Test Email" email_address

Replace email_address pinch your address:

  1. mail -s "Test Email" [email protected]

Press Enter astatine the Cc: punctual if you do not request a copy. Type the body, press Enter, past property Ctrl+D to send.

Linux Mail Command Example

Linux nonstop email example

Test the message bid (one line)

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

Example:

  1. echo "Hello world" | message -s "Test" [email protected]

Linux email pinch subject

Set the From address

Many providers require a valid sender. Use -r:

  1. echo "Hello" | message -s "Test" -r "Server Alerts <[email protected]>" [email protected]

Send an attachment pinch 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 [email protected]

Linux Send email pinch attachment

Linux email pinch attachment

Send to aggregate recipients

  1. mail -s "test header" email_address email_address2

2. Using the mailx command

mailx is the POSIX sanction for the aforesaid family of clients. On existent Ubuntu, mail and mailx often constituent to the aforesaid binary aft you instal mailutils. Some distros vessel a abstracted bsd-mailx package pinch different flags. Run mailx --version aliases man mailx connected your big earlier you trust connected attachment syntax.

Install mailx connected Debian and Ubuntu (optional standalone)

  1. sudo apt install bsd-mailx

Install mailx connected RHEL family

  1. sudo dnf install mailx -y

Test mailx pinch echo

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

Example:

  1. echo "Make the astir retired of Linux!" | mailx -s "Welcome to Linux" [email protected]

3. Using the mutt command

mutt is simply a lightweight MUA. Use it erstwhile you request dependable MIME attachments or when you tube a assemblage 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" [email protected] < /dev/null

Mutt Blank Email

Send pinch an attachment

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

Example:

  1. echo "Hey team, spot attached report." | mutt -a report.doc -s "Notice" -- [email protected]

The -- token ends action parsing truthful the reside is not publication arsenic a flag.

Linux Mutt Send Email pinch File Attachment

Mutt Email File Attached

Point mutt astatine msmtp

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

set sendmail="/usr/bin/msmtp"

Then mutt delivers done your SMTP relationship alternatively of the default Postfix queue.

4. Using the mpack command

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

Install mpack

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

Send pinch mpack

  1. mpack -s "Subject here" file email_address

Example:

  1. mpack -s "Sales Report" report.doc [email protected]

Mpack Command

Linux Mpack Send Email Attach File

5. Using sendmail

sendmail present intends the sendmail-compatible MTA interface (/usr/sbin/sendmail), not a tutorial connected moving a afloat Sendmail server. Postfix connected Ubuntu provides this binary.

Install sendmail (Sendmail MTA) connected Debian/Ubuntu

Most Ubuntu users already person Postfix from mailutils. To instal the Sendmail MTA package instead:

  1. sudo apt install sendmail -y

Install sendmail connected RHEL family

  1. sudo dnf install sendmail -y

Send a record done the sendmail interface

Create report.txt:

Hello there!

Send:

  1. sendmail [email protected] < report.txt

Sendmail Output

Add a Subject header

Put headers astatine the apical of the file, past a blank line, past the body:

Subject: Sendmail test email From: [email protected] Hello there!

6. Send email pinch SMTP authentication (msmtp)

Gmail, Yahoo, and Microsoft 365 cull unauthenticated relay from random VPS IPs. Use msmtp arsenic a lightweight SMTP client. ssmtp was communal years agone but is unmaintained and absent from existent Debian/Ubuntu repositories. Prefer msmtp.

Install msmtp

Debian/Ubuntu (includes a sendmail wrapper erstwhile you instal msmtp-mta):

  1. sudo apt install msmtp msmtp-mta -y

RHEL family (EPEL whitethorn beryllium required connected minimal images):

  1. sudo dnf install msmtp -y

Configure msmtp for Gmail (port 587)

Create aliases 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 [email protected] user [email protected] password your_app_password_here tls_starttls on account default : gmail

Replace the reside and password. Google accounts pinch 2FA request an App Password from the Google Account information page. I person not verified every Workspace admin policy. Some orgs artifact SMTP entirely.

On RHEL, the CA bundle way mightiness beryllium /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 trial connection pinch msmtp (port 587)

  1. msmtp -a gmail [email protected] <<EOF
  2. From: [email protected]
  3. To: [email protected]
  4. Subject: Test Email pinch msmtp
  5. This is the assemblage of the email.
  6. EOF

Use message aliases mutt to nonstop email pinch msmtp

With msmtp-mta installed, message and mutt often prime up /usr/bin/msmtp as the sendmail binary. You still walk -r for the visible sender erstwhile needed:

  1. echo "This is simply a trial email sent via msmtp." | message -s "msmtp Test" -r [email protected] [email protected]

Configure msmtp for Gmail connected larboard 465 (implicit TLS)

Some networks artifact 587. Try this relationship artifact alternatively of the 587 artifact above:

account gmail465 host smtp.gmail.com port 465 from [email protected] user [email protected] 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 connected / is supra 90% ($disk_usage)" | \ message -s "Disk Space Alert" [email protected] fi

Schedule pinch cron only aft you corroborate message transportation from the host.

Send a log record attachment

#!/bin/bash echo "This is simply a sample log message." > mylog.txt echo "Log attached." | mutt -s "Log File" -a mylog.txt -- [email protected] < /dev/null

For analyzable MIME, you should for illustration mutt complete message -A arsenic it provides a much reliable attachment handling.

Tool Best for Pros Cons
mail / mailx Quick text, section MTA already working Simple one-liners, communal connected servers Weak attachment story, nary built-in distant SMTP auth
msmtp Scripts that must usage Gmail/365 SMTP Stores credentials successful 1 config file Send-only, needs supplier setup
mutt Attachments and scripted MIME Solid -a attachment support Needs a sendmail binary (msmtp aliases Postfix)
mpack MIME encode 1 file Small utility Depends connected sendmail interface
sendmail System MTA plumbing Standard tube interface Not a friends MUA for humans

Postfix is the accustomed alternative to Sendmail arsenic a afloat MTA connected Linux. For scripted outbound message to nationalist providers, Postfix relay positive msmtp covers most homelab and VPS cases without moving Sendmail proper.

FAQs

1. How do you nonstop an email via the bid line?

Pipe matter into mail:

  1. echo "This is the assemblage of the email." | message -s "Email Subject" [email protected]

Install mailutils connected Ubuntu first. Confirm transportation pinch mailq aliases your provider’s sent files erstwhile utilizing SMTP.

2. How do you nonstop an email successful the Linux message command?

Same syntax. Interactive mode:

  1. mail -s "Subject" [email protected]

Type the body, past property Ctrl+D. One-liner:

  1. echo "Body text" | message -s "Subject" [email protected]

3. How do you nonstop email via SMTP from the Linux bid line?

  1. Install msmtp.
  2. Write ~/.msmtprc pinch host, port, user, and password (or App Password).
  3. Run chmod 600 ~/.msmtprc.
  4. Send pinch msmtp -a account_name [email protected] and RFC822 headers.

Relaying done Postfix is different path. See How To Install and Configure Postfix connected Ubuntu 22.04.

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

Yes. You request either a moving section MTA for your domain aliases an SMTP customer like msmtp for supplier mailboxes. Terminal MUAs (mail, mutt) only constitute and hand disconnected the message.

5. How do you nonstop SMTP email from the bid statement successful Linux?

Use msmtp pinch STARTTLS connected larboard 587, aliases implicit TLS connected larboard 465. Test with a here-document that includes From, To, and Subject headers, past a blank line, past the body.

6. Is SMTP larboard 587 aliases 465?

Both are valid today. 587 expects STARTTLS aft link (tls_starttls on in msmtp). 465 wraps TLS from the first byte (tls_starttls off). Gmail documents both. Pick 1 and lucifer your firewall rules.

7. What is the sendmail command?

sendmail is the accepted interface to the strategy MTA. You tube a connection file into /usr/sbin/sendmail. On Ubuntu pinch Postfix, the binary is provided by Postfix moreover though the bid sanction is sendmail. Reading a connection ends with Ctrl+D aliases a statement pinch only a dot, depending connected mode.

What’s next

Pick the way that matches your transportation target. Local guidelines message and cron reports often activity pinch message positive Postfix. External inboxes request msmtp aliases a Postfix relay. Attachments successful automation usually mean mutt.

Continue with:

  • How To Install and Configure Postfix connected Ubuntu 22.04
  • How To Use Google’s SMTP Server
  • JavaMail Example - Send Mail successful Java utilizing SMTP

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

Creative CommonsThis activity is licensed nether a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.

More