How to Send Email in Linux from the Command Line

Learning to send emails directly from the Linux terminal is a powerful skill for developers and system administrators. This guide will walk you through a variety of command-line tools, starting with the classic mail command for simple text messages. You will then explore more robust utilities like mutt for handling file attachments reliably and msmtp for securely authenticating with external SMTP servers like Gmail. By the end, you will be able to integrate these commands into Bash scripts to fully automate email alerts, reports, and other notifications.
We will learn about following commands to send emails in Linux.
- mailx
- mutt
- mpack
- sendmail
Linux mail command is quite popular and is commonly used to send emails from the command line. Mail is installed as part of mailutils on Debian/Ubuntu systems and the mailx package on Red Hat/CentOS systems. The two commands process messages on the command line. To install mailutils in Debian and Ubuntu Systems, run:
- sudo apt install mailutils -y
For CentOS and Red Hat distributions, run:
- yum install mailx
When you run the command, the following window will pop up. The mailutils package depends on a Mail Transfer Agent (MTA) like Postfix to handle the actual email delivery. The installation process will prompt you to configure it. Press the TAB button and hit on ‘OK’.
In the next Window, scroll and hit ‘Internet Site’.
The system will thereafter finish up with the installation process.
Testing Mail command
If the mail command is successfully installed, test the application by using the following format and press enter:
- mail –s “Test Email” email_address
Replace email_address with your email address. For example,
- mail –s “Test Email” james@example.com
After pressing “Enter”, you’ll be prompted for a Carbon Copy (Cc:) address. If you wish not to include a copied address, proceed and hit ENTER. Next, type the message or the body of the email and hit ENTER. Finally, Press Ctrl + D simultaneously to send the email.
Output
Alternatively, you can use the echo command to pipe the message you want to send to the mail command as shown below.
- echo “sample message” | mail -s “sample mail subject” email_address
For example,
- echo “Hello world” | mail -s “Test” james@example.com
Output
Let’s assume you have a file that you want to attach. Let’s call the file message.txt How do you go about it? Use the command below.
- mail -s “subject” -A message.txt email_address
The -A flag defines attachment of the file. For example;
- mail -s “Important Notice” -A message.txt james@example.com
Output
To send an email to many recipients run:
- mail –s “test header” email_address email_address2
Mailx is the newer version of mail command and was formerly referred to as nail in other implementations. Mailx has been around since 1986 and was incorporated into POSIX in the year 1992. On Debian-based systems, mailx is available as a standalone package. Users, system administrators, and developers can use this mail utility. The implementation of mailx also takes the same form as the mail command line syntax. To install mailx in Debian/Ubuntu Systems run:
- sudo apt install mailx
To install mailx in RedHat & CentOS run:
- yum install mailx
Testing Mailx command
You may use the echo command to direct the output to the mail command without being prompted for CC and the message body as shown here:
- echo “message body” | mail -s “subject” email_address
For example,
- echo “Make the most out of Linux!” | mail -s “Welcome to Linux” james@example.com
Mutt is a lightweight Linux command line email client. While the mail command can send basic attachments, mutt provides more reliable and powerful features for handling attachments, especially with MIME types. Mutt also reads emails from POP/IMAP servers and connecting local users via the terminal. To install mutt in Debian / Ubuntu Systems run:
- sudo apt install mutt
To install mutt in Redhat / CentOS Systems run:
- sudo yum install mutt
Testing Mutt command
You can send a blank message using mutt with the < /dev/null right after the email address.
- mutt -s “Test Email” email_address < /dev/null
For example,
- mutt -s “Greetings” james@jaykiarie.com < /dev/null
Output
Mutt command can also be used to attach a file as follows.
- echo “Message body” | mutt -a “/path/to/file.to.attach” -s “subject of message” — email_address
For example,
- echo “Hey guys! How’s it going ?” | mutt -a report.doc -s “Notice !” — james@jaykiarie.com
The — separator is used to signify the end of options, ensuring that the email address is not accidentally interpreted as a command-line flag.
Output
The mpack command is used to encode the file into MIME messages and sends them to one or several recipients, or it can even be used to post to different newsgroups. To install mpack in Debian/Ubuntu Systems run:
- sudo apt install mpack
To install mpack in Red Hat/CentOS Systems run:
- sudo yum install mpack
Testing mpack command
Using mpack to send email or attachment via command line is as simple as:
- mpack -s “Subject here” -a file email_address
For example,
- mpack -s “Sales Report 2019” -a report.doc james@jaykiarie.com
Output
This command is another popular SMTP server used in many distributions. To install sendmail in Debian/Ubuntu Systems run:
- sudo apt install sendmail
To install sendmail in Red Hat/CentOS Systems run:
- sudo yum install sendmail
Testing sendmail command
You can use the following instructions to send email using the sendmail command:
- sendmail email_address < file
For example, I have created a file report.doc with the following text:
Hello there !
The command for sending the message will be:
- sendmail james@example.com < report.doc
Output
To specify the subject, add it to the file.
Subject: Sendmail test email Hello there!
Sending emails to external domains like Gmail or Yahoo requires SMTP authentication. The basic mail command often lacks direct support for this. You’ll need to use a tool that can handle SMTP authentication, such as msmtp or configure a full MTA like Postfix to relay through Gmail’s SMTP server.
For this example, we’ll be using msmtp. msmtp is a lightweight SMTP client specifically designed for sending emails with authentication.
-
Install msmtp using one of the following commands:
- Debian/Ubuntu: sudo apt install msmtp msmtp-mta
- RedHat/CentOS: sudo yum install msmtp
-
Next, create or edit the ~/.msmtprc file with your Gmail credentials:
account gmail host smtp.gmail.com port 587 from your_gmail_address@gmail.com auth on user your_gmail_address@gmail.com password your_gmail_password tls on tls_starttls on tls_trust_file /etc/ssl/certs/ca-certificates.crt # Path may vary logfile ~/.msmtp.log account default : gmail
Replace your_gmail_address@gmail.com with your Gmail account and your_gmail_password with your App password. You can obtain this by going into the Security page of your Google Account.
Also, ensure the tls_trust_file path is correct for your system.
-
Set strict file permissions so only you can read and write to it. This is vital as it contains your password.
- chmod 600 ~/.msmtprc
-
You can now use msmtp as a drop-in replacement for sendmail or pipe to it:
- echo “This is a test email sent via msmtp.” | mail -s “msmtp Test” -a attachment.txt recipient@example.com -r your_gmail_address@gmail.com
Or:
- msmtp recipient@example.com <<EOF
- From: your_gmail_address@gmail.com
- Subject: Test Email with msmtp
- This is the body of the email.
- EOF
Sending emails directly from a Bash script is a useful tool for automating notifications, reports, and alerts. This can be achieved using various command-line utilities, each with its own syntax and capabilities. Here are some simple examples to get you started:
-
You can write a simple Bash script to send an email notification based on certain condition. This script sends an email whenever the disk usage is over 90%:
#!/bin/bash # Check disk space disk_usage=$(df -h / | awk ‘NR==2 {print $5}’) # Send email if disk usage is above 90% 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
This script can be scheduled to run at regular intervals to check whether the disk usage exceeds the limit.
-
You can also use a Bash script to send a file as an attachment:
#!/bin/bash # Create a log file echo “This is a sample log message.” > mylog.txt # Send the log file as an attachment mail -s “Log File” -a mylog.txt admin@example.com < /dev/null
The script generates a file named mylog.txt and attaches it to the email. When using mail with attachments in scripts, consider using mutt for more reliable attachment handling, especially for complex file types.
For basic emailing on traditional Linux systems, mail and mailx are the simplest tools. They are universally available but are not designed for modern needs. They lack built-in support for authenticating with external SMTP servers and cannot reliably handle attachments, as they depend on a pre-configured local sendmail compatible server to send messages.
For modern scripting, msmtp is the recommended tool for sending emails. It is a dedicated SMTP client designed to securely connect and authenticate with any external email service. You can configure your server settings and credentials once in a secure file, allowing your scripts to send emails reliably without exposing passwords. It is the ideal backend for any automated notification or alert.
When your scripts need to send attachments, the best solution is to use mutt in combination with msmtp. mutt excels at creating complex emails with proper MIME encoding for attachments. You use mutt’s simple command-line options to compose the email and attach files, and it then hands the final message off to msmtp for secure sending.
Finally, the other tools serve niche roles. sendmail is not a user tool but a full, complex email server engine that runs in the background. mpack is a simple utility just for encoding attachments, but its functionality is largely superseded by the more powerful and integrated capabilities of mutt.
Here’s a simple breakdown to help you decide when to use which:
Tool | Use Case | Pros | Cons |
---|---|---|---|
mail / mailx | Sending simple, text-only emails on a server with a pre-configured local mail system (like sendmail). | • Universally available on all Linux/UNIX systems. | • No built-in SMTP authentication (can’t connect to Gmail). |
• Extremely simple syntax for basic emails. | • No reliable, easy way to handle attachments. | ||
• Depends entirely on a local mail server. | |||
msmtp | Securely sending emails from scripts via any external SMTP server (e.g., Gmail, Office 365) that requires authentication. | • Purpose-built for authenticated SMTP. | • Requires a one-time setup of a configuration file. |
• Securely handles credentials in a config file (no passwords in scripts). | • It only sends email; it cannot read or manage mailboxes. | ||
• Flexible and reliable for automation. | |||
mutt | • As an interactive terminal client for reading/writing email. | • Excellent, reliable support for MIME attachments. | • Can be complex to configure. |
• For scripting emails with attachments. | • Highly configurable and powerful for interactive use. | • It’s a “composer,” not a “sender,” so it needs a separate tool like msmtp or sendmail to send the email. | |
• Can be paired with msmtp for modern sending. | |||
mpack | A single-purpose utility to encode a file into a MIME attachment and create a basic email structure. | • Simple, lightweight, and does one thing well: encoding files for email. | • Depends on a local sendmail command to send the email. |
• No SMTP authentication capabilities. | |||
• Functionality is mostly redundant if you use mutt. | |||
sendmail | Running as a system-wide Mail Transfer Agent (MTA); acting as a full email server to route and deliver all mail. | • The original, powerful, and feature-rich MTA. | • Not a user tool for sending single emails. |
• Defines many of the standards used today. | • Configuration is notoriously complex. | ||
• Mostly superseded by modern, easier MTAs. |
1. What is the easiest way to send an email from the Linux terminal?
The easiest method is using the mail command, part of the mailutils package (or mailx on some systems). It’s designed for quick, simple emails.
Run sudo apt-get install mailutils to install the package.
To send an email, use this simple syntax:
- echo “This is the body of the email.” | mail -s “Email Subject” recipient@example.com
2. Can I send emails from a script?
You can easily incorporate email commands into a shell script to automate notifications. This is useful for alerts about script completion, cron job status, or system errors.
Here is an example of a simple bash script that sends an email:
#!/bin/bash # Define email variables RECIPIENT=”admin@example.com” SUBJECT=”System Backup Report” BODY=”The system backup completed successfully on $(date).” # Send the email echo “$BODY” | mail -s “$SUBJECT” “$RECIPIENT” echo “Report email sent to $RECIPIENT.”
3. How do I send an email with an attachment in Linux?
While the basic mail command isn’t ideal for attachments, mutt is a much better alternative that handles them with ease.
To install mutt, run the command:
- sudo apt-get install mutt
To send an email with an attachment, use the -a flag:
- echo “Please find the report attached.” | mutt -s “Report Attached” -a /path/to/file.zip — recipient@example.com
While command-line email clients are simpler and less computationally intensive, using the right tool for the task is crucial. Basic commands like mail are excellent for local system alerts but cannot connect to external services like Gmail or Yahoo that require authentication. This limitation is overcome by using a modern client like msmtp, which is designed to securely handle SMTP authentication for sending email to any domain. For sending attachments reliably, pairing mutt with msmtp provides a powerful and scriptable solution.
Although GUI clients like Thunderbird are ideal for daily email management, the command-line approach remains unmatched for automating notifications and integrating email into system administration workflows, helping you avoid problems with undelivered mail in your scripts.
Now that you can send emails from the command line, you might want to dive deeper into mail server configuration or application-level email automation. Check out these guides to continue learning: