Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Mark Nelson

Dr. Dobb's Bloggers


July 01, 2011

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:

smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = static:username:password
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = may
relayhost = [smtp.sendgrid.net]:587

After making the changes, restart postfix so it reads the new config options. I also watch the mail log file so I can see if there are any problems on first use:

sudo /etc/init.d/postfix restart
sudo tail -f /var/log/mail.log

A test message sent to my cell phone arrived as a text message in just one or two seconds, with the following log messages:

Jun 26 17:02:08 ubuntu postfix/pickup[21145]: 51A5E5E1DA6: uid=1000 from=<mark>
Jun 26 17:02:08 ubuntu postfix/cleanup[21336]: 51A5E5E1DA6: message-id=<[email protected]>
Jun 26 17:02:08 ubuntu postfix/qmgr[21146]: 51A5E5E1DA6: from=<[email protected]>, size=273, nrcpt=1 (queue active)
Jun 26 17:02:08 ubuntu postfix/smtp[21338]: 51A5E5E1DA6: to=<[email protected]>, relay=smtp.sendgrid.net[]:587, delay=0.33, delays=0.04/0.02/0.23/0.04, dsn=2.0.0, status=sent (250 Delivery in progress)
Jun 26 17:02:08 ubuntu postfix/qmgr[21146]: 51A5E5E1DA6: removed

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.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

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.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.