For those of us running a home server, there are plenty of reasons you might want to get some automated emailing setup. From system failure alerts to notifying you when some key event in your script has been triggered, there are dozens of reasons why having the ability to fire off emails will come in handy. Unfortunately, doing so requires a bit of setup beyond just installing something. Let's talk about it!

Photo by Stephen Phillips - Hostreviews.co.uk / Unsplash

Accessing an SMTP Service Provider

The first thing needed to send emails is an SMTP server. There are lots of ways you can go about this - you could use one of the many paid services like Mailgun or Amazon SES. You could use your friendly neighborhood Gmail, although there are additional hurdles to authenticate. You could even setup your own! For me, I was already using No-IP as my domain registrar and their managed DNS service, which includes access to their SMTP service.

No matter how you go about it, you'll eventually want to get your hands on some key pieces of information: the SMTP address and port, and the credentials to authenticate. For No-IP's service, the domain is mail.noip.com and I'm using port 587.

Setup Your SPF Record

The Sender Policy Framework (SPF) is a way to authenticate emails to protect against spoofing. The gist is that you provide a way for mail recipients to validate the the email they received came from a trusted source (i.e. you) and not someone pretending to be you. The exact way you'll setup the SPF record depends on where your domain is registered. For me, this is at No-IP and I was able to go to My Services > DNS Records > Modify > SPF to add a record.

The record itself has a pretty extensive specification which you can read more about here. Luckily, there are a number of wizards available online that will walk you through the steps to set yours up. Barring complicated configurations, you'll probably end up with something simple. For reference, here's mine which allows for any emails sent directly from my domain or from no-ip.com:

 v=spf1 a mx ptr include:no-ip.com -all 

Sending the Email!

With that initial setup out of the way, we can get down to business on the actual sending of emails. There are tons of ways to do this, but the quickest (and simplest) for me was using sendemail which can be installed from Ubuntu trusty via sudo apt-get install sendemail. After that, sending an email super simple!

sendemail \
    -f "youremail@yourdomain.com"
    -xu "username"
    -xp "password"
    -s "smtp.server.com:000"
    -o tls=yes
    -t "recepient@otherdomain.com"
    -u "Email subject goes here."
    -m "And the body goes here!"
    -a /path/to/optional/attachment.zip

In this example invocation, we're sending an email from "youremail@yourdomain.com" to "recepient@otherdomain.com" with a subject, message and attachment. You'd also need to fill in the appropriate details for the SMTP server, port and authentication.

Hopefully you'll use TLS to securely transport the email, but feel free to leave that off if it makes testing easier!

Closing Thoughts and Troubleshooting

After these relatively simple steps, you can start to automate emails from your home server to alert or notify when needed. The trickiest part of this for me was troubleshooting authentication issues and troubleshooting SPF record issues.

For the former, my first advice is to triple-check the SMTP server, address, port, authentication information and TLS settings. Some SMTP servers might require TLS, so try toggling it. You might also try contacting your SMTP service provider to make sure you're using the correct information.

If a mail server (such as Gmail or Outlook) is marking your messages as spam or suspicious, it's likely due to a misconfigured SPF record. The easiest way to diagnose this is to look at the raw email headers. In Gmail, this can be done as documented here. You'll be able to see where the email originated from, and then you can trace that back to understand better why it was rejected and hopefully fix the record.

Good luck, and have fun!