Some time ago, I spent many hours setting up my home Linux box (Ubuntu) to send mail via my ISP (Comcast at the time). I only needed outbound mail (so that I could get my server logs with the rest of my email), but had to endure the seemingly endless configuration nightmare that was sendmail. I only ever got it partially working - cron’s emails to root did work, as did sending email to fully qualified addresses, but I was never able to get both qualified and unqualified addresses to work. Eventually, I just gave up.
About a year ago or so, my server went four-paws-up hardware-wise, and instead of replacing the motherboard like I usually would, I just bought a whole new box. When it arrived, I installed Ubuntu, and then added the drive from the old box that had the partition with the /home directory. I didn’t install sendmail this time - figured I get around to it eventually.
Well, today, I decided to rectify the situation, and for yucks, googled something about linux and gmail. In the results, I found ssmtp.
It was pretty simple to set up, but I still wound up having to poke around quite a bit to figure out how to get everything just the way I wanted it.
So, here’s what I did:
- Install ssmtp:
sudo apt-get install ssmtp
- Edit /etc/ssmtp/ssmtp.conf to look like:
##Config file for sSMTP sendmail ## The person who gets all mail for userids < 1000 # Make this empty to disable rewriting. email@example.com
# The place where the mail goes. The actual machine name is required no # MX records are consulted. Commonly mailhosts are named mail.domain.com mailhub=smtp.gmail.com:587
# Where will the mail seem to come from? rewriteDomain=
# The full hostname hostname=smtp.gmail.com:587
UseSTARTTLS=YES AuthUserfirstname.lastname@example.org AuthPass=yourpassword
# Are users allowed to set their own From: address? # YES - Allow the user to specify their own From: address # NO - Use the system generated From: address FromLineOverride=YES
Obviously, you should replace “yourname” with your actual gmail username in the lines above.
- Edit /etc/ssmtp/revaliases to look like:
# sSMTP aliases # # Format: local_account:outgoing_address:mailhub # # Example: root:email@example.com:mailhub.your.domain[:port] # where [:port] is an optional port number that defaults to 25. root:firstname.lastname@example.org:smtp.gmail.com:587 yourlocaluser:email@example.com:smtp.gmail.com:587
Again, replace ‘yourname’ and ‘yourlocaluser’ with appropriate values.
So far, this is information that you can find in any number of places on the net. But…this works only for mail to ‘root’ and to fully qualified addresses. What about mail to non-root local users? While this probably not really necessary, I wanted to get it to work this time.
At first, I tried adding aliases to /etc/aliases, but as the documentation clearly states (if you bother to read it), ssmtp does not use /etc/aliases.
The solution turns out to be letting ‘mail’ handle the alias - which is done by configuring aliases in /etc/mail.rc
set ask askcc append dot save crt ignore Received Message-Id Resent-Message-Id Status Mail-From Return-Path Via Delivered-To alias root root<firstname.lastname@example.org> alias yourlocaluser yourlocaluser<email@example.com>
And that’s it…mail to both root and to my local username (both as unqualified addresses) now get delivered to my gmail account.
In the examples above, you might have noticed gmail addresses with a plus-sign in them. This is a neat trick that you can do with gmail - the part after the plus sign can be anything, and is ignored by gmail for delivery purposes. You can then set up a filter on the full to: address, with the plus sign and extra characters, to set a label, and optionally other things, like starring, archiving, and marking as ‘read’.