Hack and / - Make a Local Mutt Mail Server

 in
Mail server configuration doesn't have to require pages of text files, five manuals and a team of system administrators. If you just need to relay mail from your local mutt client, it'll take a few minutes and a few short lines in a Postfix config.

I talk about mutt a lot in this column, if you hadn't noticed. For me, in this day and age of large graphical mail programs and Web-based mail applications, you still can't beat the speed, power and customization of mutt. Let's also not forget the vi-style keybindings—I love those.

One thing you might notice the first time you use mutt, however, is that it is strictly a MUA (Mail User Agent) and not an MTA (Mail Transfer Agent). This means mutt is concerned only with acting as an e-mail client and doesn't actually contain any code to communicate with remote mail servers. That job is done by an MTA. Although many mail clients also include code so they can relay mail through an MTA, mutt opts to use the system's own local mail server. Traditionally, this hasn't been an issue on Linux, as most Linux servers have had some mail server installed and set up. These days, however, you might not have a fully configured mail server on your desktop install. That's okay though, because in this column, you'll see how simple it is to set up your own local mail server, thanks to Postfix.

Even if you don't use mutt, there are many advantages to having your own local mail server, if only to relay mail for you. For one, it can handle spooling all of your e-mail and will retry delivery automatically if it fails for some reason or another (such as if your wireless connection drops or you close your laptop) without having to leave your mail program open. For another, once you have your mail server set up how you want it, any other mail client on your computer can take advantage of it: simply point your client to localhost.

The Mail Server Holy War

A number of different mail servers are available for Linux these days, each with its own set of advantages and disadvantages. Many holy wars have been fought over Sendmail vs. Postfix vs. Exim vs. using Telnet to connect directly to port 25 on a mail server and type in raw SMTP commands. I've tried them all over the years (yes, even Telnet), and for me, Postfix has the best balance between stable performance, security and most important, simple configuration files. So for this column, I discuss the specific steps for setting up Postfix as a mail relay.

The first step is to install the Postfix server itself. On most distributions, you'll find this package is split up into a main Postfix package plus a few extra packages that provide specific features, such as MySQL or LDAP integration. Because we are just setting up a basic mail relay here, all we really need is the main Postfix package. Now, if you install this package on a Debian-based system, you will be prompted by the post-install script that acts as a wizard to set up Postfix for you. If you want, you simply can walk through the wizard and pick “Internet Site” to send e-mail out directly to the rest of the Internet or choose “Internet with smarthost” to relay all of your mail through a second mail server (perhaps provided by your ISP) first. Either way, you will be asked a few simple questions, and at the end, you'll have a basic Postfix configuration ready to use.

On other systems (or if you choose “No configuration” on a Debian-based system), you might end up with an empty or very heavily commented Postfix configuration file at /etc/postfix/main.cf. What you'll find is that for a basic mail server, you really need only a few lines in your config. Postfix picks pretty sane and secure defaults, so if you want it to deliver mail on your behalf, you need only a few lines:

mynetworks = 127.0.0.0/8
inet_interfaces = loopback-only

Yes, that's basically it. Now, simply restart Postfix with /etc/init.d/postfix restart, and your mail server will be up and running. With the sane defaults in Postfix, you just need to hard-code those two settings to ensure that Postfix accepts mail only on localhost. The inet_interfaces line tells Postfix to listen only on the localhost address for e-mail so no clients can connect to your server from the outside. The mynetworks line adds to that security and tells Postfix to allow only mail from localhost to be relayed through the server.

The Pesky Port 25 Problem

It used to be that the above was all you needed for a functioning mail server on the Internet. With the rise of spam measures and countermeasures, however, these days, fewer and fewer ISPs are willing to allow port 25 traffic from clients through to the outside world. Even if they do, many mail servers on the Net won't accept traffic from hosts inside ISP networks. If you find yourself on such a network, you likely will need to add a relay host to your main.cf. The relay host is a mail server usually provided by your ISP through which your mail server can send e-mail. If you were setting up a client like Thunderbird, for instance, this would be the SMTP server you would configure for it.

To set up a generic relay host in Postfix, just add:

relayhost = mail.somedomain.net

______________________

Kyle Rankin is a director of engineering operations in the San Francisco Bay Area, the author of a number of books including DevOps Troubleshooting and The Official Ubuntu Server Book, and is a columnist for Linux Journal.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

inspired me

katto's picture

I enjoyed this so much it inspired me to finally set up my mail.
3 days later I have a working system!
Mostly, this was me being dense, but I do have a couple of points to add to the above: in /etc/postfix/main.cf:

1) the relayhost DNS must be surrounded with [] if it is an alias
2) the smtp_generic_maps property must be used to specify the file containing the mapping of the local email address to that of the relayhost account.

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix