Learning Center
Mail

How to Send Email in Linux from the Command Line with Sendmail and Mailx

18 Mar 2025
Awais Khan
Awais Khan

For those managing servers or working on automation tasks, knowing how to send emails from the Linux terminal is essential. It offers complete control over email functions and eliminates the need for complex mail programs. This is useful in scenarios where speed and simplicity matter most.

Common tools such as sendmail and mailx are frequently used for sending messages, checking SMTP settings, automating alerts, and integrating with scripts. They are straightforward yet effective, making them perfect for tasks like informing teams about server updates, automating reports, or testing email setups.

This guide is designed for users looking to manage their email directly from the terminal. It covers the installation of essential tools and delves into more advanced tasks, such as sending attachments and configuring email tools.

Why Choose Command-Line Email Tools?
Copy link

Two commonly used tools, sendmail and mailx, are reliable options for mail transmission in Linux. They come with a certain set of benefits:

  • Efficiency: Traditional email software can be slow and resource-intensive. These tools enable quick and lightweight email sending directly from the terminal.
  • Automation: They integrate smoothly with shell scripts, cron processes, and system monitoring tools. Automating mail alerts and notifications for repeated actions is possible via these Linux mail tools.
  • Troubleshooting SMTP Problems: Debugging SMTP setups becomes more manageable. These commands provide visibility into message delivery, ensuring mail logs and errors are easier to inspect.
  • Flexibility: Whether it’s sending alerts or generating automated reports, command-line tools like sendmail and mailx offer versatility across a range of tasks.

Prerequisites 
Copy link

Before utilizing these Linux mail command line tools, ensure you have terminal access. Root privileges may be required in some cases, especially for configuring each mail command on Linux discussed in this guide.

Setting Up a SMTP Server
Copy link

SMTP servers are essential for sending emails. These servers fall into two categories: External and Local SMTP servers.

It refers to a mail server hosted by a third-party provider. These servers are utilized to deliver emails over the internet to recipients who are not part of your local network. They are built to manage global mail delivery while ensuring proper authentication, encryption, and spam prevention.

Examples 

  • Gmail 

Address: smtp.gmail.com

Port: 587 (with TLS) or 465 (with SSL)

  • Outlook 

Address: smtp.office365.com

Port: 587

These servers need appropriate authentication methods (such as a username, password, or app-specific passwords) and encryption (like TLS or SSL) to ensure secure communication.

Note: We’ve already provided a guide for setting up external SMTP servers. The command to send emails through Postfix remains the same as mentioned in this article. Simply configure the SMTP settings using our guide, and replace the email address with Gmail or any other preferred provider for proper email delivery.

This server functions solely within a private network or system. It is perfect for:

  • Sending emails between users on the same network or domain (e.g., tom@office.local to jerry@office.local).
  • Local testing and development tasks.
  • Internal communication within an organization.
  • Does not need internet access to operate, as they manage mail delivery internally.

Setting Up a Local SMTP Server
Copy link

Here are the procedures to set up a local SMTP server using Postfix:

  1. Install Postfix via:

sudo apt install postfix
  1. Modify the Postfix configuration file:

sudo nano /etc/postfix/main.cf
  1. Update or confirm these key settings:

myhostname = mail.office.local
mydomain = office.local
myorigin = $mydomain
inet_interfaces = loopback-only
local_recipient_maps = proxy:unix:passwd.byname
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
  1. Save and exit the file after doing changes, then restart Postfix:

sudo systemctl restart postfix
  1. To create email addresses like linux@office.local and hostman@office.local, set up user accounts on the server:

sudo adduser linux
sudo adduser hostman

Overview of sendmail
Copy link

sendmail is a prominent mail transfer agent (MTA) in Linux. It works flawlessly with SMTP servers for mail delivery and allows emails to be sent and routed from local systems or scripts. 

Installing sendmail 
Copy link

Before sending emails, you must install the Linux sendmail tool. Execute the commands below based on your distribution:

For Debian/Ubuntu

sudo apt install sendmail

For CentOS/Red Hat

sudo yum install sendmail

Starting and Enabling Service
Copy link

Once installed, make sure sendmail is running and configured to start at boot:

sudo systemctl start sendmail
sudo systemctl enable sendmail

Testing the Configuration
Copy link

Check the sendmail is set up correctly by executing:

echo "Testing sendmail setup" | sendmail -v your-email@example.com

Image2

Verify email by executing the mail command:

mail

Image4

Note: Install mailutils package in case the mail command is not working.

sudo apt install mailutils

Or utilize the cat command:

cat /var/mail/user

Image3

Editing the Configuration File

To customize settings for sendmail, modify the configuration file located at /etc/mail/sendmail.mc:

sudo nano /etc/mail/sendmail.mc

Image1

Make the required changes to fit your server. For example, if you want to define the domain name for your server, you can add or modify the following line:

define(`confDOMAIN_NAME', `your_domain.com')dnl

Here, replace your_domain with your actual domain name.

Then rebuild the configuration file:

sudo m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf

If a "permission denied" error occurs, use:

sudo sh -c "m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf"

Finally, restart the service:

sudo systemctl restart sendmail

Sending Email Via sendmail
Copy link

With sendmail, you can easily deliver emails, customize subjects, and even add attachments using external tools. Let’s go over the process to send emails:

Basic Example
Copy link

To send an email with sendmail, use the below-given instructions:

  1. First, create a file to hold the message:

nano email.txt
  1. Add any content to the file, for example:

Subject: Test Email from Hostman
This is a test email sent using sendmail on Linux.
  1. Deliver the file's contents:

sendmail recipient@example.com < email.txt

The contents of email.txt will be sent to the designated recipient.

Image6

  1. For verification, apply:

mail

Image5

Adding Attachments 
Copy link

sendmail by itself doesn’t support attachments. You’ll need to utilize uuencode or similar tools to include files. First, install sharutils for uuencode:

sudo apt install sharutils

Here’s how to attach a file:

( echo "Subject: Email with attachment"; uuencode file.txt file.txt ) | sendmail recipient@example.com

In the above sendmail example we send an email with file.txt attached.

Image8

To verify, apply the Linux command mail:

mail

Image7

Overview of mailx 
Copy link

The mailx Linux command is a simple and effective terminal application for managing emails. It is included in the mailutils package found in most Linux distributions.

Installing mailx 
Copy link

Install mailutils package on your system to utilize the mailx command on Linux:

For Debian/Ubuntu systems

sudo apt install mailutils

For Red Hat-based systems

sudo yum install mailx

Sending Email with mailx
Copy link

This is a simple example demonstrating the use of mailx.

  1. Include a subject line and message in your email:

echo "This is the body of the email" | mailx -s "Test Email from Mailx" recipient@example.com

Image11

Utilize the Linux mail command for verification:

Image9

Example with Attachments
Copy link

Use the -A flag with the mailx command to send emails from Linux with attachments:

echo "Please find the attached document" | mailx -s "Email with Attachment" -A email.txt recipient@example.com

This sends email.txt as an attachment to the recipient.

Image10

Conclusion
Copy link

Sending email from the Linux command line is an effective method for automating communication tasks, troubleshooting servers, or testing configurations. Using tools such as sendmail and mailx, you can manage everything from simple messages to more complex setups with attachments. This guide has provided detailed instructions to help you begin without difficulty. Utilize these Linux email commands to improve your workflow. If you face any issues, feel free to refer back to this tutorial.

And if you’re looking for a reliable, high-performance, and budget-friendly solution for your workflows, Hostman has you covered with Linux VPS Hosting options, including Debian VPS, Ubuntu VPS, and VPS CentOS.