Mark Nelson

Dr. Dobb's Bloggers

Free SMTP

July 01, 2011

Integral email is still a great feature, almost 40 years after it first showed up in UNIX.

This summer I'm teaching a graduate class, Principles of UNIX, which is more or less a crash course in the Mother of All Operating Systems. One of our early topics is email on UNIX, in which I try to impart to the class just how transformative email was back in the day. For early Internet users (who were mostly UNIX users), this was an incredibly big deal.

Unfortunately, setting up email on a Linux or UNIX system is not quite as automatic as it once was. In our class we use mailx and sendmail as tools to send files from background processes or cron jobs — but mailx will typically not work out of the box. In this post, I'll discuss how to get it working on an Ubuntu 11 system.

Things Have changed

Back in the day if you wanted to send an email, you simply found a handy SMTP server that was accepting incoming connections. There were thousands, and they were undiscriminating, so this was no big deal.

The invention of spam ruined that.

Now any SMTP server you find is going to require you to go through a bit of a dance in order to authenticate and prove you are not a spammer. I started this post with the intention of showing you how to use your gmail account to access Google's SMTP servers. The process was fairly arduous, as it involved creating a certificate authority, your own certificates, and then setting up the mail server to use this authentication.

While working on this, my son Joey recommended that I just set up a free account on one of several email gateway providers, such as SendGrid or MailJet. Both services will let you access their servers and send up to 200 emails a day for free.

I took him up on it and found the process to be much simpler than using gmail, so I'll pass along the setup procedure here.

Getting an Account

Obviously, SendGrind is in business to get you to purchase a commercial account so you can send thousands of emails a day from your website. Accordingly, SendGrind doesn't go out of its way to advertise its free plan. If you go to the pricing page, you will find a little tiny link to the free plan hidden at the bottom.

Setting up an account is easy, but SendGrid insists that you have a website. For automatic verification, SendGrid will need to find your email address on the site. I opted for an alternate provisioning plan in which I created a page on my site with the phrase "Sendgrid".

Once you have an account, you have free access to the SendGrid SMTP servers for up to 200 outbound messages a day. So you are ready to configure your UNIX system to take advantage of it.

Ubuntu Configuration

Configuring Ubuntu 11 to send email is fairly painless. Using the Ubuntu Software Center, you can locate and install two packages: postfix and bsd-mailx. During the install of postfix, you will get dropped into a debconf window asking you some basic configuration questions.

I entered the following answers to the two questions I got hit up with:

General configuration: Internet

System mail name: dogma.net

That seemed to be all I needed for basic configuration.

Postfix Configuration

Configuring postfix to use SendGrid was just a matter of adding a few lines to /etc/postfix/main.cf, using your SendGrid user name and password. Note that the file probably has an existing relayhost line; this one should replace it:

Moving On to Better Things

Now that postfix is properly configured, I can really start taking advantage of the mail infrastructure on my system. The next obvious step is to create a .forward in my home directory, and give it my external gmail address. That external address will now be the recipient of output from cron jobs, or from at or batch. It's nice to have the mail set up as in integral part of the O/S, and if you can just make it through a little bit of setup, it's all yours.

With a limit of 200 messages a day you can still make extensive use of outbound email for system monitoring — whether it is via text to your phone or huge messages being sent to an account used for storing log files. Either way, integral email is still a great feature, almost 40 years after it first showed up in UNIX.

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.

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!