• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
NameHero® Blog

NameHero® Blog

Web Hosting Tips & Resources From NameHero

  • AI Agents
    • OpenClaw
    • Claude Code
    • n8n
    • Hermes Agent
    • Open WebUI
    • Docker
  • Hosting
    • Web Hosting
    • WordPress Hosting
    • WooCommerce Hosting
    • Enterprise Hosting
    • Email Hosting
    • HeroicGuard
    • GPU Hosting
    • Website Builder
  • VPS
    • Managed VPS
    • Unmanaged VPS
    • Flex VPS
  • Reseller
  • Gaming
  • Domains
  • Account
  • Blog Home
  • Categories
  • Authors

How to Use and Install the Linux “mail” Command

Bhagwad Park

Published on: March 27, 2024

Categories: Linux Command Line, VPS Hosting 1

The Linux “mail” command sends and receives e-mails from the command line. In this tutorial, I’ll show you how to install it with a basic configuration to send e-mails over the Internet and instruct you on how to use it to send e-mails, with and without attachments. A more advanced configuration is necessary if you want your sent e-mails to escape the spam folder. For that, you need to make changes to your DNS settings.

I’ve written an earlier tutorial on how to prevent your VPS e-mails from going to spam, and you should follow that article after you’ve set up the mail command as shown below.

Table of Contents
  • How to Install the Linux “mail” Command
    • No Configuration
    • Internet Site
    • Internet with Smarthost
    • Satellite System
    • Local Only

How to Install the Linux “mail” Command

The mail command on Ubuntu is part of the “mailutils” package. My default VPS server with NameHero wasn’t installed automatically as part of the VPS. But installing it is easy with the following command:

sudo apt install mailutils

Make sure you have your sudo password ready for this. If you don’t, here’s a tutorial on how to execute sudo commands on your VPS. Once the installation is in progress, the mailutils package will open up a text-based graphical interface allowing you to configure it.

Here’s a screenshot of the Postfix configuration screen:

Postfix Configuration Screen
Postfix Configuration Screen

Postfix is a well-known Mail Transfer Agent (MTA), that was designed as an alternative to the Sendmail MTA. Postfix is faster and more secure than Sendmail, and is easier to set up compared to its complex counterpart. As you will see, setting it up is absurdly easy.

The above screen shows you the options available with Postfix. Here they are one by one.

No Configuration

Choose this option if you just want to install Postfix and don’t want to configure it at the moment. If you skip this step, you won’t be able to immediately send e-mails, but you can always do it later.

Internet Site

This option is what we most likely want when we install mailutils. It allows us to send e-mails to e-mail addresses over the Internet. This is useful within scripts, for example, when you want to send a message to the administrator that a certain operation is complete.

Internet with Smarthost

This option is available when you want to hand over mail delivery services to a 3rd party that will assume responsibility for its delivery. These 3rd party e-mail services specialize in efficient e-mail delivery and are very useful if you don’t want to go through complex e-mail configurations on your own. Sometimes, your server or VPS might be blocked or rate-limited as spam prevention measures, and the smarthost is a great way to ensure that your e-mails are delivered as intended without being marked as spam.

One important point to note is that with this option, incoming mail is still received directly by the server via SMTP. The system delivers messages to local clients as well.

Satellite System

A satellite system is just like the “Internet with Smarthost” option, with the only difference being that the smarthost handles incoming mail as well. This setup is suitable for high-security servers, and in situations where network connectivity is limited since all e-mail activity occurs through the smarthost.

Local Only

In this setup, the system only delivers mail to local users who have an account on the system. It’s great for notifications and system alerts. This means that if you need to deliver to or receive mail from an external e-mail address, you won’t be able to do so.

For this tutorial, I’m going to explore the second option – the Internet site. This will let us send e-mails to 3rd party addresses.

When you see this screen on a Linux system, you might be puzzled that there’s no way to select one of the options. You can’t highlight any of them, and if you use the arrow or tab keys, the only thing you can select is the “OK” button. Don’t panic! This is normal. This screen is for information only – you’ll be making your actual choice on the next screen. So click the “OK” button by highlighting it and pressing “Enter”, and you’ll see the following screen below:

Internet Site
Internet Site

For this tutorial, I’m going to show you how to set up an “Internet Site” mailing system that will let you send e-mails over the Internet from the command line. So use the arrow keys to highlight the second option, then use the “Tab” button to select “OK”.

On the next screen, the system will ask you to enter a “Fully Qualified Domain Name” or an “FQDN”.

FQDN
FQDN

This is essentially the name of the server that you’re using when you send an e-mail. While nothing is stopping you from using any old name here, modern e-mail systems with spam detection capabilities won’t accept e-mails from sources that don’t have a verified entry in their DNS systems. But for now, you can enter what you want. As you can see, I’ve just entered “testing” here.

In the final stage of the setup, the program will ask you to restart the services and update the older packages. Using the spacebar, toggle on or off, the ones that you want to restart. If you don’t see a need not do so, just restart all of them.

Once you’ve gone through the configuration screen, the mailutils package will continue with its installation.

Sending Mail from the Command Line

Once the installation is complete and you’ve restarted all the services, it’s time to send a test e-mail from the command line. Doing so is easy. Just type something like this:

echo "This is the body" | mail -s "Subject" [email protected]

Replace “[email protected]” with an e-mail that you control and see how it works. If everything goes smoothly, you should receive an email like this:

Received E-mail
Received E-mail

As I mentioned earlier, this will likely go to spam, unless you’ve followed my earlier tutorial about ensuring that your VPS e-mails don’t go to spam. But that setup can be complicated, and different e-mail providers have different standards. If the only person who’s going to see these e-mails is yourself, then I suggest you just create a filter that delivers these specific e-mails to your inbox, by specifying the “from” address. It’s more convenient. If, on the other hand, you want to send legitimate e-mails to other people on a different server, from the command line, then you have no option but to make the changes to your DNS entries.

Sending an E-mail with an Attachment

Sending an attachment with the mail command is easy. The trick is to find out which version of the “mail” command you have and use the appropriate method. For example, the one I’m using requires the “-A” flat to signify an attachment. So the command is:

echo "This is the body" | mail -s "Subject" -A testbackup.txt [email protected]

For other mail programs, you have to use the small “a” instead of the capital “A”. The best way to find out what the appropriate syntax is by consulting the mail command’s “man” page like this:

man mail

I get the following response:

Attach a File
Attach a File

You can see that it tells me to use either “-A” or “–attach” for attachments. So follow whatever instructions your man page tells you to!

Conclusion

The Linux mail command is quite easy to use from the command line. Even installing it is easy. The tricky part is ensuring that your e-mail doesn’t go to the spam folder. Some e-mail providers don’t even allow the mails to go to spam, and just delete them outright. If you can get away with it, just set up an e-mail filter to send them straight to your inbox. Otherwise, you’ll have the hassle of changing your DNS entries.

Bhagwad Park Profile Picture
Bhagwad Park

I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!

Related Posts

Curl vs Wget: Choosing the Right Tool for the Job

Linux has two popular tools for getting data from third party websites. Here's the difference between curl and wget.

Using the “whois” Command – All you Need to Know

If you want to know about who owns a domain, or when it expires, then you need to use the "whois" command. Here's all you need to know.

Bash Debug Mode: set -x and the PS4 Variable

When coding a program, you usually have a full IDE set up with autocomplete and stack tracing, allowing you to pause execution at any point and see what’s going on under the hood. These tools make coding more productive, and they’ve been in use for a long time. But what if you’re just quickly coding […]

Docker Build Cache – How it Works and Invalidating it

Docker heavily optimizes its build process with caching to save time. But it's important to know how to invalidate it.

Reader Interactions

Comments

  1. scott collins says

    April 28, 2025 at 11:53 am

    very clear .. thank you

    Reply

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow & Subscribe

Exclusive promos, content and more!


Most Popular Posts

NameHero’s Recommended WordPress Plugin and Theme Setup

WordPress Hosting vs. Web Hosting – What’s The Difference?

How To Increase The InnoDB Buffer Pool Size

How To Fix A Stuck All-in-One WP Migration Import

How To Add A Subdomain In Cloudflare

Top Categories

  • WordPress
  • WordPress Tutorials
  • OpenClaw Hosting
  • Enterprise Hosting
  • WooCommerce
  • Web Hosting
  • Resellers
  • Website Security
  • Website Development
  • Website Performance
  • VPS Hosting
  • SEO Tips
  • Announcements
  • Domain Registration
NameHero

NameHero® proudly provides web hosting to over 40,000 customers with 99.9% uptime to over 750,000 websites.

  • Master Card
  • Visa
  • American Express
  • Discover
  • Paypal
Products
  • Web Hosting
  • Managed VPS Hosting
  • Unmanaged VPS Hosting
  • Flex VPS Hosting
  • WordPress Hosting
  • WooCommerce Hosting
  • Reseller Hosting
  • Enterprise Hosting
  • GPU Hosting
  • Email Hosting
  • HeroicGuard
  • Domains
  • Website Builder
  • AI Agent Hosting
Help & Support
  • NameHero Blog
  • NameHero Gaming Blog
  • Support
  • Help Center
  • Migrations
  • Affiliates
  • Gaming Affiliates
  • Call 1-855-984-6263
Company
  • About Us
  • Contact Sales
  • Reviews
  • Uptime
  • We're Hiring

Copyright © 2026 Name Hero, LLC. All rights reserved.
NameHero® is a registered trademark.

  • Privacy Policy
  • Terms of Use
  • Acceptable Use Policy
  • Payment Policy
  • DMCA