If you’re looking to set up a Linux box all by yourself, you’ll want to configure an e-mail server sooner or later. This seems like it should be a simple job, but it’s not. The e-mail architecture can be bewilderingly complex, with dozens upon dozens of options and things that can go wrong. No surprise then, that we have dedicated mail server administrators whose sole job it is to know the ins and outs of e-mail infrastructure.

However, there are times when we don’t care about the bells and whistles and just want to do one thing – send and receive e-mail. Sometimes we don’t care about security, features like passwords, virtual users, mailboxes, SMTP, POP etc. We want the ability to go to a terminal, type in a single command and send an e-mail. One of the ways to eventually build up to a complete mail solution is to start small. Start with the basics, test it, and then move ahead. As we progress, we can build up features like spam filtering etc.

But in this tutorial, we’re going to do just one thing – send an email without too much configuration and effort. Most tutorials for mail that you’ll find on the Internet involve installing lots of software, opening files and configuring a large number of settings, along with setting up SSL certificates etc. But here, we’re going to bypass all that and just rely on the basics.

Sending Mail is Useful for Server Alerts

In case you’re wondering what use we can have for such a simple e-mail system, the first use-case that comes to mind is server alerts. You have a lot of events on a server that you might want to be notified about. An important cron job for example. Or perhaps you want a certain log file to be sent to your e-mail ID at periodic intervals. Or a warning about when your disk space is getting low. Any of these can be configured as an e-mail message to the administrator. And here’s how to get started.

Step 1: Install postfix and mailx

The two basic packages we’re going to need are postfix and mailx. I’m not going into what they’re supposed to do. That can be quite a complicated mess in itself with lots of terminology that needs careful explaining. Just enter the following command into your CentOS terminal to install them:

yum install postfix mailx

Change up this command depending on your particular flavor of Linux. Use “apt-get” for Ubuntu instead of yum etc etc. Here are the two packages that are installed with the above command:

Once these two are installed, we’re ready to send e-mail! But first, we must start the postfix service like this:

systemctl start postfix

Sending E-mail from the Command Line in Linux

Now that we have the “mailx” package, we can start sending mail by typing in the following:

mail address@domain.com

This will take you to a blank line that says “Subject:”. Enter the subject and hit enter. On the next line, start typing in the body of the e-mail. And when done, go to a newline and press dot (.) . The whole thing will look like this:

Now go to the address you sent the e-mail to and check the spam folder. It’ll be there as shown here:

And the body inside is as we wrote:

As to why it got sent to the spam folder, that’s a different question altogether and not in the scope of this article.

Sending E-mail using a Single Command

The above technique requires user input. However, we can also write the entire e-mail using a single command like this:

mail -s "Inline e-mail" address@domain.com <<< 'Body of the e-mail goes here'

This is useful when you want to send e-mail using a script. You can see that I receive the email above in my inbox:

Configure a Linux server to send e-mail

You can even append a log file instead of a message by typing a single arrow (<) followed by the name of the file. This is perfect for warnings and error messages.

And that’s about it! This is probably the easiest way to get started with configuring your Linux server to send e-mail messages. It’s very much the tip of the iceberg – setting up a full fledged e-mail server is one of the more complicated operations on a Linux box. But if you just want to quickly send stuff, then this is all you need!

Is your website slow?

Enter its URL below to find out now:

About the Author

Bhagwad Park

Comments