Mark Nelson

Dr. Dobb's Bloggers

SMTP Configuration

July 22, 2011

I'm going to share a few tips to help in the process of setting up an SMTP server so postfix can send mail from your Linux system.

In my last post I showed a particularly easy way to set up an SMTP server so postfix could send mail from your Linux system. In this post I'm going to add a few tips that might help you get through some rough spots in the whole process.

Results From the Field

After writing a HOWTO page, there is no better test of its quality than to try it out with a group of students performing the task for the first time. That's just what I did with my SMTP configuration post — I created a homework problem that asked my students to set up outbound email on their personal Linux systems.

Their resulting glitches highlighted a few difficulties in the process, most of which can be handled pretty easily.

Debian Package Configuration

When you install postfix on Ubuntu, you will normally get kicked into a simple package configuration dialog after the package has been downloaded and installed. This final step performs some very basic setup on postfix, getting into a basically functional state.

If this configuration doesn't run, postfix seems to be configured to only handle local mail — messages from one user to another. In some cases this may be just what you want, but normally it is not all that useful.

For unknown reasons, this configuration pass doesn't always kick off properly. I've seen it fail to run, and some students have as well. Fortunately, you can execute the same process by hand from the command line:

sudo dpkg-reconfigure postfix

Running through this, I take the default settings for everything except Root and postmaster mail recipient, which I change to student, the name of my account on this system. After this is complete, following the instructions from my previous post will give you a proper Internet email setup.

Using Mailjet Instead of SendGrid

After some glitches getting accounts on SendGrid, I had a couple of students decide to use Mailjet as an STMP provider. Registering with Mailjet is easier, as it is an automated process. However, using Mailjet as your SMTP provider comes with a couple of interesting twists.

First, instead of using your Mailjet account credentials to authenticate with its SMTP server, Mailjet gives you a set of strong credentials specifically for SMTP authentication. You have to copy these from your Account Info page. No problem there, as long as you RTFM.

Slightly more problematic is that Mailjet insists that the return address you use in your outbound emails be an authenticated address. In other words, you have to be able to receive an email sent to that address and click on an authentication link.

When using postfix's default settings, email sent by my students was going out with a return address of student@ubuntu, and there is obviously no way to authenticate that address. So we have to rewrite that address to something usable.

After taking that additional requirement into account, my changes to /etc/postfix/main.cf for Mailjet are shown below. The file had a default relayhost entry, which is replaced, and the remaining lines are all new. The username and password have to be replaced with the ones from your Mailjet account page:

The last line I added to the configuration tells postfix to look in a file called /etc/postfix/generic for a list of substitutions to make to email addresses found in headers being processed. I create a single line in this file:

student@ubuntu yourmail@yourprovider.tld

This entry in the map file changes the local student account to an authenticated email address. After making this change, emails sent from the student account will have their headers rewritten to give a sender identity of your personal email address, which means Mailjet will happily forward your messages to their final destination.

The headers on my received email show that things worked more or less as expected:

One nice thing about using the generic substitution file is that now you can accommodate different sender email addresses for every account on your system. Since you have to enter account info by hand, this doesn't scale to a large enterprise, but it works just fine for your personal system.

There's no doubt that the complexities of managing mail on a Linux system is still a bit daunting, but at least the process of simply getting outbound mail working is manageable. Give it a shot!

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!