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.
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.
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: 51A5E5E1DA6: uid=1000 from=<mark> Jun 26 17:02:08 ubuntu postfix/cleanup: 51A5E5E1DA6: message-id=<[email protected]> Jun 26 17:02:08 ubuntu postfix/qmgr: 51A5E5E1DA6: from=<[email protected]>, size=273, nrcpt=1 (queue active) Jun 26 17:02:08 ubuntu postfix/smtp: 51A5E5E1DA6: to=<[email protected]>, relay=smtp.sendgrid.net[220.127.116.11]: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: 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
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.