How To Send An Email in Ubuntu Terminal

Managing a remote server often requires a way to receive automated notifications without a graphical interface. You might be running long-duration scripts and need a way to get notified upon completion, or you might want to receive unattended-upgrades reports directly in your inbox to maintain system health. Without a terminal-based email solution, you risk missing critical security alerts or hardware failures.

Ubuntu provides several powerful mechanisms to send mail directly from the command line, ranging from simple notification agents to full-scale transfer services. Whether you are automating a backup script or need to check file size in Linux and mail the results to a colleague, the terminal offers efficient tools to integrate communication into your workflow. This guide explains the best methods to get mail flowing from your shell.

Key Takeaways: Terminal Email Essentials

  • MUA (Mail User Agent) → The client software (like ssmtp or mail) that you use to compose and send the message.
  • MTA (Mail Transfer Agent) → The background service (like Postfix) that handles the routing and delivery of the email.
  • Postfix → The default MTA for Ubuntu, compatible with legacy sendmail commands.
  • TLS Encryption → Essential for security to ensure that your passwords and message content are not sent in plain-text over the network.
  • Sudo Privileges → Administrative rights are required to install and configure mail services to ensure Linux server security.

Method 1: Using sSMTP for Minimalistic Sending

For most users who only need to send alerts from scripts, sSMTP is the ideal solution. It is a minimalistic client that forwards mail from your terminal to an external mail server (like Gmail or an enterprise relay) without hosting a full mail server locally.

Installation: sudo apt install ssmtp

Usage Example: echo "Subject: Test Mail" | ssmtp [email protected]

This method is highly efficient because it does not run a persistent background daemon, saving system resources while still providing reliable delivery.

Method 2: Configuring Postfix (The Ubuntu Standard)

Postfix is the default supported Mail Transfer Agent in Ubuntu. It is robust, secure, and designed to handle professional-grade mail traffic. It is the best choice if you need a persistent service to handle mail for multiple users or complex routing.

Installation: sudo apt install postfix

Configuration Details: During installation, a configuration menu will appear. For basic terminal sending, select “Internet Site” and enter your Fully Qualified Domain Name (FQDN). You can trigger this menu manually later using: sudo dpkg-reconfigure postfix

Method 3: Verifying Connectivity with telnet

If your emails are not arriving, you can manually test your connection to the mail server to see if Port 25 (the standard SMTP port) is blocked or open.

See also: Mastering the Linux Command Line — Your Complete Free Training Guide

Command: telnet mail.example.com 25

Expected Output:

220 mail.example.com ESMTP Postfix
ehlo mail.example.com
250-STARTTLS
250 8BITMIME

If you see “Connected,” your network path is clear. If it hangs, your ISP or firewall settings may be blocking outbound mail traffic.


Step-by-Step Process: Setting Up Postfix for Local Sending

Follow these steps to configure the standard Ubuntu mail service for your local environment:

  1. Open your terminal and ensure your system is up to date with sudo apt update.
  2. Install the package by running sudo apt install postfix.
  3. Choose “Internet Site” when the configuration wizard appears.
  4. Enter your domain (e.g., mail.yourdomain.com) as the System Mail Name.
  5. Restart the service to apply changes: sudo systemctl restart postfix.
  6. Send a test mail using the mail command: echo "Body text" | mail -s "Terminal Test" [email protected]
  7. Check logs if the mail fails to arrive: sudo tail -f /var/log/mail.log.

Summary Tables

Mail ComponentRoleRecommended Tool
User Agent (MUA)Compose/Sendssmtp or mailutils
Transfer Agent (MTA)Routing/DeliveryPostfix
Delivery Agent (MDA)Inbox StorageDovecot
Alert ManagerAutomated AlertsPrometheus
Configuration ChoiceMeaningBest For…
Internet SiteSends/Receives via SMTPProduction Servers
Satellite SystemForwards all to another hostInternal Office Nodes
Local OnlyNo external deliveryDevelopment/Testing

FAQs

Why is my terminal mail going to spam? Most automated mail from unknown IP addresses is flagged as spam. Ensure you have a valid SPF record or use an authenticated relay service.

Do I always need ‘sudo’ to send mail? No. You need the sudo command only for installation and configuration. Once the service is set up, any user can send mail via the CLI.

Can I send file attachments from the terminal? Yes, tools like mutt or mail -A allow you to attach files. This is useful for sending system log extracts.


Related Posts

David Cao
David Cao

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

Articles: 639

Leave a Reply

Your email address will not be published. Required fields are marked *