Skip to content
Posts en inglés. Usá el traductor del navegador para leerlos en tu idioma.

How to Send Emails from the Linux Command Line

Yammbo
· 2 min read
linux email command line email mailutils tutorial mutt attachments msmtp configuration
How to Send Emails from the Linux Command Line

Sending emails directly from the Linux command line is a fundamental skill for system administrators, developers, and anyone managing server-side operations. Whether you need to deliver automated alerts from cron jobs, forward script output to an inbox, or send notifications from a custom application, command-line email tools provide a powerful and flexible solution. This tutorial guides you through setting up and using three essential tools: mail (or mailx) for basic text emails, mutt for handling attachments reliably, and msmtp for connecting to external SMTP services that require authentication.

Setting Up Your Email Environment

Before you can send emails, you need to install the necessary packages on your Linux system. The specific commands vary slightly between Debian/Ubuntu-based distributions and RHEL-family systems like AlmaLinux or Rocky Linux.

For Debian/Ubuntu Systems

On Debian and Ubuntu, the primary tool for basic command-line email is provided by the mailutils package. This package often installs or depends on a Mail Transfer Agent (MTA) like Postfix to handle the actual sending of emails.

  1. Update your package list:

    sudo apt update
  2. Install mailutils:

    sudo apt install mailutils -y

    During the installation, if Postfix is installed, you might be prompted to configure it. For most simple use cases where you're sending outbound emails from a server, select 'Internet Site'. You can navigate the dialogs using the Tab key and confirm with Enter.

For RHEL-Family Systems (AlmaLinux, Rocky Linux)

On RHEL-based distributions, the equivalent package is typically mailx. These systems often use Sendmail or Postfix as their default MTA, which mailx can interface with.

  1. Install mailx:

    sudo dnf install mailx -y

Sending Basic Text Emails with mail / mailx

The mail (or mailx) command is perfect for sending quick, plain-text messages. It's simple to use and ideal for automated scripts where you just need to deliver a short notification.

Interactive Mode

You can use mail in an interactive mode, where you type the subject and body directly into the terminal.

  1. Start the mail command, specifying the subject and recipient:

    mail -s