Email Server Setup With emailwiz

29 Sep 24


I set up an email server for akbatten.com. Primarily this is for education and my weird sense of fun, but it also lets me make a bot account for my nextcloud server to email new users, reset passwords, etc. I used Luke Smith's emailwiz script which automates a lot of the process. There were a few hiccups though; here is the process I ended up taking.

Setup

Acquire a VPS - I went with Hetzner. For email uses, they require that your account be 1 month old (to help prevent spam), so you may want to make your account now. During provisioning, I set the machine's host name to "mail.akbatten.com". I also edited the firewall to open ports 465, 993, and 80. Port 80 is used by the script to verify an SSL certificate; I closed it after the installation. I SSHed into the VPS. It's always good practice to immediately run

apt update && apt upgrade

I downloaded the script and made it an executable with:

curl -LO lukesmith.xyz/emailwiz.sh
chmod +x emailwiz.sh

I then headed to my DNS service and set up pre-requisite records. "mail.yourdomain.tld" should point to the VPS IP for both IPv6 and IPv4. I did this wrong initially; check the actual IP with the below command. Hetzner's dashboard left had my IPv6 ending with "::" instead of "::1". Most things work without the 1 at the end, but it caused problems later.

ip a
Installation

I kept getting DNS verification errors, but I knew my DNS was correct, so I found the offending lines and commented them out:

ipv4=$(host "$domain" | grep -m1 -Eo '([0-9]+\.){3}[0-9]+')
[ -z "$ipv4" ] && echo "\033[0;31mPlease point your domain ("$domain") to your server's ipv4 address." && exit 1
ipv6=$(host "$domain" | grep "IPv6" | awk '{print $NF}')
[ -z "$ipv6" ] && echo "\033[0;31mPlease point your domain ("$domain") to your server's ipv6 address." && exit 1

I'm not exactly sure how these commands work, but running "host $domain" returns 127.0.0.1 which certainly isn't right. With these lines removed, the script runs with no trouble. At the end, it prints out some more DNS records that I added to my DNS service - a couple TXT records and a MX record. The last thing is to set up reverse DNS with Hetzner. This way, a DNS query for the IP address returns the "mail.yourdomain.tld". Note that this must be done with whichever service owns the IP of the VPS, so it is most likely the VPS service and not the DNS service.

Adding Users

Adding an email user is done by adding a unix user to the VPS and including them in the "mail" group. Also give them a password to log in with.

useradd -m -G mail username
passwd username


Comments