-
Notifications
You must be signed in to change notification settings - Fork 27
A Complete Setup Guide
Mail servers can be a tricky thing to set up. This guide is supposed to run you
through the most important steps to achieve a 10/10 score on mail-tester.com.
What you need:
- A server with a public IP (referred to as
server-IP) - A Fully Qualified Domain Name (
FQDN) where your server is reachable, so that other servers can find yours. Common FQDN includemx.example.com(whereexample.comis a domain you own) ormail.example.com. The domain is referred to asserver-domain(example.comin the above example) and theFQDNis referred to byserver-FQDN(mx.example.comabove). - A list of domains you want to your email server to serve. (Note that this
does not have to include
server-domain, but may of course). These will be referred to asdomains. As an example,domains = [ example1.com, example2.com ].
The following describes a server setup that is fairly complete. Even though
there are more possible options (see default.nix), these should be the most
common ones.
{ config, pkgs, ... }:
{
imports = [
(builtins.fetchTarball "https://github.com/r-raymond/nixos-mailserver/archive/v2.1.3.tar.gz")
];
mailserver = {
enable = true;
fqdn = <server-FQDN>;
domains = [ <domains> ];
# A list of all login accounts. To create the password hashes, use
# mkpasswd -m sha-512 "super secret password"
loginAccounts = {
"[email protected]" = {
hashedPassword = "$6$/z4n8AQl6K$kiOkBTWlZfBd7PvF5GsJ8PmPgdZsFGN1jPGZufxxr60PoR0oUsrvzm2oQiflyz5ir9fFJ.d/zKm/NgLXNUsNX/";
aliases = [
"[email protected]"
"[email protected]"
];
# Make this user the catchAll address for domains example.com and
# example2.com
catchAll = [
"example.com"
"example2.com"
];
};
"[email protected]" = { ... };
};
# Extra virtual aliases. These are email addresses that are forwarded to
# loginAccounts addresses.
extraVirtualAliases = {
# address = forward address;
"[email protected]" = "[email protected]";
};
# Use Let's Encrypt certificates. Note that this needs to set up a stripped
# down nginx and opens port 80.
certificateScheme = 3;
# Enable IMAP and POP3
enableImap = true;
enablePop3 = true;
enableImapSsl = true;
enablePop3Ssl = true;
# Enable the ManageSieve protocol
enableManageSieve = true;
# whether to scan inbound emails for viruses (note that this requires at least
# 1 Gb RAM for the server. Without virus scanning 256 MB RAM should be plenty)
virusScanning = false;
};
}After a nixos-rebuild switch --upgrade your server should be good to go. If
you want to use nixops to deploy the server, look in the subfolder nixops
for some inspiration.
Add a DNS record to the domain server-domain with the following entries
| Name (Subdomain) | TTL | Type | Priority | Value |
|---|---|---|---|---|
server-FQDN |
10800 | A | server-IP |
This resolved DNS equries for server-FQDN to server-IP. You can test if your
setting is correct by
ping <server-FQDN>
64 bytes from <server-FQDN> (<server-IP>): icmp_seq=1 ttl=46 time=21.3 ms
...
Note that it can take a while until a DNS entry is propagated.
Wherever you have rented your server, you should be able to set reverse DNS
entries for the IP's you own. Add an entry resolving server-IP to
server-FQDN
You can test if your setting is correct by
host <server-IP>
<server-IP>.in-addr.arpa domain name pointer <server-FQDN>.
Note that it can take a while until a DNS entry is propagated.
For every domain in domains do:
-
Add a
MXrecord to the domaindomainName (Subdomain) TTL Type Priority Value domainMX 10 server-FQDN
You can test this via
dig -t MX <domain>
...
;; ANSWER SECTION:
<domain> 10800 IN MX 10 <server-FQDN>
...
Note that it can take a while until a DNS entry is propagated.
For every domain in domains do:
-
Add a
SPFrecord to the domaindomainName (Subdomain) TTL Type Priority Value domain10800 TXT v=spf1 ip4:<server-IP> -all
You can check this with dig -t TXT <domain> similar to the last section. Note
that SPF records are set as TXT records since RFC1035.
Note that it can take a while until a DNS entry is propagated. If you want to use multiple servers for your email handling, don't forget to add all server IP's to this list.
In this section we assume that your dkimSelector is set to mail. If you have a different selector, replace
all mail's below accordingly.
For every domain in domains do:
-
Go to your server and navigate to the dkim key directory (by default
/var/dkim). There you will find a public key for any domain in thedomain.txtfile. It will look likemail._domainkey IN TXT "v=DKIM1; r=postmaster; g=*; k=rsa; p=<really-long-key>" ; ----- DKIM mail for domain.tld -
Add a
DKIMrecord to the domaindomainName (Subdomain) TTL Type Priority Value mail._domainkey. domain10800 TXT v=DKIM1; p=<really-long-key>
You can check this with dig -t TXT mail._domainkey.<domain> similar to the last section.
Note that it can take a while until a DNS entry is propagated.
Write an email to your aunt (who has been waiting for your reply far too long), and sign up for some of the finest newsletters the Internet has. Maybe you want to sign up for the SNM Announcement List?
Besides that, you can send an email to mail-tester.com and see how you score, and let mxtoolbox.com take a look at your setup, but if you followed the steps closely then everything should be awesome!