Sendmail on Linux - the Easy Way
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 (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 mail, you simply found a handy dedicated server that was accepting incoming SMTP 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 off 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, SendGrid is in business to get you to purchase a commercial account so you can send thousands of emails a day from your web site. Accordingly, the don’t go out of their way to advertise their free plan. If you go to their pricing page, you will find a link to the free plan.
Setting up an account is easy, but SendGrid insists that you have a web site. For automatic verification they 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
To configure 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 you should restart postfix so it reads the new config options. I also start watching 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=<20110627000208.51A5E5E1DA6@ubuntu> Jun 26 17:02:08 ubuntu postfix/qmgr[21146]: 51A5E5E1DA6: from=<mark@dogma.net>, size=273, nrcpt=1 (queue active) Jun 26 17:02:08 ubuntu postfix/smtp[21338]: 51A5E5E1DA6: to=<xxxxxxxx@txt.att.net>, relay=smtp.sendgrid.net[174.36.32.204]: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 forty years after it first showed up in UNIX.