Support Online
Skip to main content

How to Send Email from Linux Command Line?

Sending email directly from a Linux terminal is a powerful skill for developers and system administrators.
In this guide, you'll learn how to send email, attach files, and authenticate with external SMTP servers like Gmail using the mail, mutt, mailx, and msmtp tools.
You will also be able to fully automate system alerts by integrating these commands into Bash scripts. 🚀

🧠 Technical Summary

CategoryDescription
Main Technical TopicLinux Command Line Email Tools (mail, mutt, msmtp)
Solved ProblemNeed to send email via terminal or script (text, attachment, with SMTP authentication).
User Steps1️⃣ Install mailutils/mailx. 2️⃣ send basic email via mail. 3️⃣ Send email with attachment via mutt. 4️⃣ Authenticate via Gmail with msmtp. 5️⃣ Integrate these commands into Bash automation.

💌 1. Classic Method: mail and mailx Commands

The mail (or mailx) command is the most classic way to send email from the terminal.
However, for it to work, you must have a Mail Transfer Agent (MTA) — for example, Postfix — installed on your system.

Step 1: Installation

sudo apt install mailutils -y # Debian / Ubuntu
sudo yum install mailx -y # CentOS / Red Hat

Step 2: Send a Simple Text Email


echo "Sunucu durumu: Kritik seviyede disk kullanımı var." | mail -s "Disk Uyarısı" admin@ornek.com

This command quickly sends an email titled “Disk Alert”.

Step 3: Interactive Submission


mail -s "Test E-postası" alici@ornek.com

After the command runs, the CC address and message body are entered. To send, press the Ctrl + D combination.


📎 2. Reliable Attachment Management: mutt Command

mutt is a powerful terminal email client with MIME support. It is much more stable than the mail command when working with file attachments.

Step 1: Installation


sudo apt install mutt -y

Step 2: Send Email with Attachment


echo "Rapor ektedir." | mutt -a /yedekler/rapor.pdf -s "Haftalık Rapor" -- yonetici@ornek.com

The -- expression is used to prevent the email address from being detected as a parameter.


🔐 3. External SMTP Authentication: msmtp

msmtp is designed to connect with authentication to external SMTP servers such as Gmail or Outlook. It is the safest and most flexible method in modern automation scenarios.

Step 1: Installation


sudo apt install msmtp msmtp-mta -y

Step 2: Creating a Configuration File

Create .msmtprc file in user directory:


nano ~/.msmtprc

And enter the following content (Gmail example):


account gmail
host smtp.gmail.com
port 587
from hesap_adiniz@gmail.com
auth on
user hesap_adiniz@gmail.com
password UYGULAMA_SIFRENIZ
tls on
tls_starttls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt

account default : gmail

⚠️ Warning: In the "password" section, enter the App Password information you received from your Google account, not your normal password.

Step 3: Security Permissions


chmod 600 ~/.msmtprc

This command ensures that only you can access the configuration file.

Step 4: Sending via msmtp


echo "msmtp üzerinden gönderildi." | mail -s "SMTP Testi" alici@ornek.com -r hesap_adiniz@gmail.com

With this method, authenticated e-mail is sent via reliable SMTP such as Gmail.


⚙️ 4. Automation in Bash Scripts

🧩 Example 1: Disk Space Warning


#!/bin/bash
disk_kullanimi=$(df -h / | awk 'NR==2 {print $5}')
if [[ ${disk_kullanimi%\%} -gt 90 ]]; then
echo "UYARI: Disk kullanımı %90'ın üzerinde! ($disk_kullanimi)" | mail -s "Disk Alanı Alarmı" admin@ornek.com
fi

This script automatically sends an e-mail when the disk fullness exceeds 90%.

🧩 Example 2: Log File Sending


#!/bin/bash
echo "Betik çalışması tamamlandı: $(date)" > sistem_log.txt
echo "Güncel log dosyası ektedir." | mutt -s "Sistem Log Raporu" -a sistem_log.txt -- admin@ornek.com < /dev/null

This script automatically sends the generated system_log.txt file via email with an attachment.


🧠 Frequently Asked Questions (FAQ)

1. Which command should I use and when?

mail/mailx: For simple text emails.

mutt: To send email with attached files.

msmtp: For secure authentication via external SMTP.

2. Why do my emails end up in the spam folder?

Because it is sent from an unauthenticated server. ✅ Solution: Send over reliable SMTP using msmtp or Postfix relay.

3. How can I send emails with my Gmail account?

Create “App Password” from your Google account and use it in the .msmtprc file.

4. What do sendmail and Postfix do?

These are MTA (Mail Transfer Agent) software. They transmit e-mail behind tools such as mail, mutt or msmtp.


💡 Summary

VehicleUsage AreaProsCons
mail / mailxSimple emailsQuick, easyNo SMTP support
muttEmails attachedMIME support, flexibleConfiguration difficult
msmtpAuthenticated SMTPSafe, modernShipping only
sendmailServer level mailStrongComplex configuration

🎯 Result

Sending email from the command line is a skill that is both practical and automation-friendly for system administrators.

For simple alerts → mail

For attached submissions → mutt

For secure SMTP → msmtp

💼 Tip: By testing these processes on the servers on GenixNode, you can set up your own email automation system in minutes.