Cat Projects Writings

How I Set Up Email for My Website, and Why You Shouldn't.

The short answer: unless you're a company, running an email server is more painful and less effective than paying Fastmail $5 a month. If you still want to, the plan is simple! 1. Get a server, and put an SMTP and IMAP server on it (or save yourself the pain and use docker-mailserver). Make sure your firewall or that of your service provider isn't catching mail ports (if you use a VPS, you probably need to have support unblock port 25). Set up TLS certs. Set up DKIM, SPF, DMARC, MX, and reverse DNS records. While DMARC records aren't technically a necessity, they help other mail servers see your mail as legitimate. Use a mail deliverability tester to fix the 30 other things you forgot or misconfigured, and, finally, accept that you will probably still hit spam boxes half the time because you bought a .xyz domain.

The Mail Server

So, to start off, you need a server hosting IMAP and SMTP. While this can be done with a slew of services (Postfix, Dovecot, Fail2ban, etc.) I prefer using docker-mailserver. It's much more hassle free, and more secure given it has more sane configurations. It's a docker container that bundles together all the services you need to handle inbound and outbound mail, spam prevention, security, and management. Make your life easier.

Set up a user account, postmaster account, and all the other things that go with it. The documentation for docker-mailserver is lovely and I highly suggest using it.

It's a good time to make sure that your firewall has unblocked ports 25, 587, 465, 993, and 995. If you use a VPS provider, remember to unblock them there too (if your VPS provider is anything like mine, this will require a call with an AI support representative to put you through to a person to unblock SMTP on their end. Matt was very sweet.)

Now it's time for my favorite part: TLS CERTIFICATES! This is relatively painless if you use certbot with Let'sEncrypt. While docker-mailserver has built-in certificate handling, I found it incredibly difficult to work with and wound up generating my certs on the host machine and importing them to the container. You need to sign for mail.yourdomain.tld, not just domain.tld.

Here's my docker-compose file:

services:
 mailserver:
 image: ghcr.io/docker-mailserver/docker-mailserver:latest
 container_name: mailserver
 hostname: mail.kasio.xyz # Change to your mail FQDN (DNS MX record must point here)
 environment:
 LOG_LEVEL: info
 POSTMASTER_ADDRESS: postmaster@kasio.xyz
 ENABLE_UPDATE_CHECK: 1
 SSL_TYPE: letsencrypt # Options: letsencrypt / custom / manual / self-signed / empty
 TLS_LEVEL: modern
 ENABLE_OPENDKIM: 0
 ENABLE_RSPAMD: 1
 SPOOF_PROTECTION: 1
 ports:
 - "25:25" # SMTP (STARTTLS; no auth)
 - "143:143" # IMAP (STARTTLS)
 - "465:465" # SMTPS (implicit TLS)
 - "587:587" # SMTP (STARTTLS + auth)
 - "993:993" # IMAPS (implicit TLS)
 volumes:
 - ./docker-data/dms/mail-data/:/var/mail/
 - ./docker-data/dms/mail-state/:/var/mail-state/
 - ./docker-data/dms/mail-logs/:/var/log/mail/
 - ./docker-data/dms/config/:/tmp/docker-mailserver/
 - /etc/localtime:/etc/localtime:ro
 - /etc/letsencrypt:/etc/letsencrypt:ro
 # For SSL_TYPE=custom/manual, mount your certs:
 # - ./certs/:/tmp/dms/custom-certs/:ro
 restart: always
 stop_grace_period: 1m

DNS

Assuming everything has gone well, it's time to start fussing with DNS records (and there are so many of them.) While this encompasses DMARC records, we will talk about those after we have working outbound mail.

At this point, all I'm worried about is setting up an MX record so that mail is correctly routed. This needs to specifically be a subdomain (mail.domain.tld). Note that if you don't already have an A and AAAA record, the subdomain means nothing and mail sending will fail because you don't have valid DNS records. The MX record points back to an A name, so it's a necessity.

If you're using Cloudflare like I am, be SURE to turn off proxying for your mail records. If you don't, email will get lost forever in a whirling vortex of proxy addresses and will never go anywhere.

As with anything else DNS, it takes a while for records to propagate. Be patient.

Server Hosting

I use a VPS for my site, so part of the setup involved opening the firewall on that end, as well as configuring reverse DNS. Ionos doesn't make using their firewall easy, every time you add a new port it reloads the firewall, giving you a good 20-30 second pause in your work. But whatever. Much like on the server itself, there are a good number of ports you need to unblock. Much like USB, we've invented a system that needs compatibility with like 5 different other systems to work as expected.

While you're here, point your IP's reverse DNS to your mail subdomain. It's one of the hundred things you have to do to get a message past spam boxes. This will take more setup with your VPS provider later.

DMARC, DKIM, SPF, PTR, oh my!

So at this point you should have a server that can send and receive mail. Only when you try to send mail you'll quickly realize it was never going to be that simple. You see, spam protection is intense and requires a number of measures from both parties before mail will be seen as legitimate. This is where DMARC, DKIM, SPF, and that RDNS we set up earlier come into play. There are a number of email helpers and mail deliverability tools that will make your life a bit simpler (but not easy!!!)

So what are all these other records actually accomplishing!? Basically, they're all just different ways to prove that an email came from who it says it did, and that you also don't like spam.

DMARC is an extension of SPF and DKIM, two other security measures which you also need to set up because this can't be easy. Basically, DMARC prevents impersonation by giving receivers instructions on how to authenticate the message they're getting. If you use Cloudflare, they'll make this step easy. If you don't, there are websites that'll generate records for you.

Because DMARC only works with the other two, we gotta set up the other two. Start with SPF since it's easier. SPF is, basically, a TXT record that receivers can look at to make sure that the mail they're receiving is coming from the server it says it is. Again, use a generator. All it really needs is your server's public IP and FQDN.

So. Now. DKIM. We love DKIM in this household. It's very easy. Finding the right command to execute from inside the container and then finding the directory it sent my keys to then actually getting the keys then figuring out how to write the TXT record was very very fun. Basically, DKIM is looking to see if incoming mail was signed by the correct public key. So we need to affix our public key as yet another TXT record. Again, please use a generator. They make life infinitely easier. Lookup how to format this correctly for your registrar because I spent, legitimately, a good two hours troubleshooting slight syntax errors with my record.

Finally, we need to set up PTR records. This is just a reverse DNS entry, so that mail can resolve your IP to the domain it expects. For my setup, this meant creating a PTR record with cloudflare that accomplished nothing before I thought to just change the reverse DNS entry with my server host. This is because PTR records are set by whoever owns the IP, so Cloudflare was never going to work.

Why My Mail Still Goes to Spam Boxes

So. You've got a mail server. You've got your certs and your records. You've got your security. Why is your mail still hitting spam boxes? There's a few reasons.

One, you probably didn't get all those records right on the first try. Use a mail deliverability checker to see what's wrong.

Two, you've got a new and unknown domain name suddenly sending mail. From the perspective of the big mail providers this is very suspicious. There's a process called mail warming, basically the process of slowly ramping up the amount of mail you're sending in order to prove to the big guys that you're legitimate. There are companies offering this service for money, and I cannot recommend against them enough. The much better way is to have friends and family indulge your weird new interest and write mail back and forth. Slowly, their mail provider will warm up to you. Slowly.

Three, the game was rigged from the start. I made the mistake of buying a .xyz domain and hosting through a VPS, two things that make my mail suspicious no matter what because of association with spammers. Apparently I bought a cyber place in a bad cyberhood. Mail and spam checkers flag these to me and there's nothing I can really do except completely redo my entire server setup and pay more for a more legitimate TLD. While this isn't the end of the world, combined with the whole mail warming thing it would take an inordinate amount of effort for me to complete the one task I set this whole thing up for: sending an email to someone and getting it to their inbox.