Connect with us

Website Tutorials

How to Use and Install the Linux “mail” Command

Published

on

How to Use and Install the Linux “mail” Command

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.

How to Install the Linux “mail” Command

The mail command on Ubuntu is part of the “mailutils” package. My default VPS server with SirsteveHQ 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. 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:

Advertisement
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.

Advertisement

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”.

Advertisement
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" recipient@example.com

Replace “recipient@example.com” 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:

Advertisement
echo "This is the body" | mail -s "Subject" -A testbackup.txt bhagwadpark@outlook.com

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.

Stephen Oduntan is the founder and CEO of SirsteveHQ, one of the fastest growing independent web hosts in Nigeria. Stephen has been working online since 2010 and has over a decade experience in Internet Entrepreneurship.

Continue Reading
Advertisement
Comments

Trending

Copyright © 2024 SirsteveHQ. All Rights Reserved.