Support Online
Skip to main content

Guide to Sending Email from Linux Terminal

Sending email from the Linux command line is a critical skill for system administrators and developers.
In this guide, you'll learn all the methods, from simple text messages to automatic alerts with SMTP authentication.

What Will You Learn in This Guide?

  • Sending fast e-mail with the mail command
  • Creating emails with file attachments using mutt
  • Configuring secure SMTP via services such as Gmail
  • Automating disk, error and report notifications with Bash scripts

Technical Summary

This guide covers the processes of sending e-mail via terminal on Linux (Ubuntu / CentOS) systems.
The goal is to automate system notifications and reports.

Topics covered:

  • installation of mailutils and mutt
  • SMTP authentication with msmtp
  • Bash script integration

1. Basic Mail Command Usage

mail is the most basic and fastest email tool on Linux.
Typically used for local system alerts.

Installation

sudo apt install mailutils -y
  • This command sets up the basic email infrastructure.

2. Simple Email Sending


echo "Sistem yedeklemesi tamamlandı." | mail -s "Yedekleme Durumu" kisi@ornek.com
  • This method sends non-interactive e-mails.

2. Sending Email with File Attachments with Mutt

Mutt is much more reliable for sending files or reports. Handles MIME types without any problems.

  • Installation

sudo apt install mutt -y
  • This command installs the mutt tool.

Submission with File Attachment


echo "Güncel rapor ektedir." | mutt -a "rapor.pdf" -s "Haftalık Rapor" -- alici@ornek.com

The -- sign indicates the end of options.


3. External SMTP (Gmail etc.) configuration

Default mail tools do not work directly with services like Gmail. In this case msmtp should be used.


  1. Create msmtp Configuration ~/.msmtprc file:

account gmail
host smtp.gmail.com
port 587
auth on
user kullanici@gmail.com
password uygulama_sifresi
from kullanici@gmail.com
tls on

account default : gmail
  • This structure defines Gmail SMTP settings.

Security Setting


chmod 600 ~/.msmtprc
  • This command makes the configuration file private only for you.

4. Automation with Bash Scripts

  • The following script sends a warning e-mail when disk fullness exceeds 90%:

#!/bin/bash

doluluk=$(df -h / | awk 'NR==2 {print $5}' | sed 's/%//')

if [ "$doluluk" -gt 90 ]; then
echo "Sunucu doluluk oranı %$doluluk" | mail -s "Disk Uyarısı" admin@ornek.com
fi
  • This script can be run periodically via cron.

VehicleBest UsesAdvantage
mailSimple system alertsQuick and easy
muttEmails with file attachmentsStrong MIME support
msmtpGmail / external SMTPSecure authentication

Frequently Asked Questions (FAQ)

1. Why do my e-mails go to spam? E-mails sent from the local server have a low trust score. It is recommended that you use Gmail, SendGrid or corporate SMTP.

2. Is it safe to write the SMTP password into the script? No. Passwords should be kept in the .msmtprc file and chmod 600 should be applied.

3. I get the error “mail command not found” Mailutils or mailx is not installed in the system. You need to run the installation command.

4. Can I send the same email to more than one person? Yes. Just write the addresses side by side with a space.


Result

Sending email via command line professionalizes server management. Automation installed with the right tool allows you to detect errors immediately.

You can immediately test these methods on your high-performance VPS on the GenixNode infrastructure 🚀