Setting up mail autoconfiguration

Published on December 29, 2020

Installation

Install the build dependencies:

apk add build-base git openssl-dev

Install Rustup:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env

Clone the repository:

git clone https://github.com/StephanvanSchaik/automail
cd automail

Build automail:

RUSTFLAGS="-C target-feature=-crt-static" cargo build --release

Install the binary:

cp target/release/automail /usr/bin

Create the automail user:

useradd -MU automail

Edit /etc/init.d/automail:

#!/sbin/openrc-run

name=$RC_SVCNAME
cfgfile="/etc/$RC_SVCNAME/$RC_SVCNAME.conf"
command="/usr/bin/automail"
command_args=""
command_user="automail"
pidfile="/run/$RC_SVCNAME/$RC_SVCNAME.pid"
start_stop_daemon_args=""
command_background="yes"

depend() {
        need net
}

start_pre() {
        checkpath --directory --owner $command_user:$command_user --mode 0775 \
                /run/$RC_SVCNAME /var/log/$RC_SVCNAME
}

Mark /etc/init.d/automail as executable:

chmod +x /etc/init.d/automail

Configuration

Create the /etc/automail directory:

mkdir /etc/automail

Edit /etc/automail/Rocket.toml:

[default]
address = "0.0.0.0"
port = 3721

Edit /etc/automail/config.toml:

[ssl]
key = "/etc/letsencrypt/live/example.com/privkey.pem"
cert = "/etc/letsencrypt/live/example.com/cert.pem"
chain = "/etc/letsencrypt/live/example.com/chain.pem"

[[domain]]
domain = "example.com"
name = "Example Mail Configuratioon"
short_name = "Example"

[[domain.server]]
protocol = "imap"
hostname = "imap.example.com"
port = 993
auth = "plain"
encrypt = "ssl"

[[domain.server]]
protocol = "imap"
hostname = "imap.example.com"
port = 143
auth = "plain"
encrypt = "starttls"

[[domain.server]]
protocol = "pop3"
hostname = "pop3.example.com"
port = 995
auth = "plain"
encrypt = "ssl"

[[domain.server]]
protocol = "pop3"
hostname = "pop3.example.com"
port = 110
auth = "plain"
encrypt = "starttls"

[[domain.server]]
protocol = "smtp"
hostname = "smtp.example.com"
port = 587
auth = "plain"
encrypt = "starttls"

[[domain.server]]
protocol = "smtp"
hostname = "smtp.example.com"
port = 465
auth = "plain"
encrypt = "ssl"

Start the service:

/etc/init.d/automail start
rc-update add automail

Edit /etc/nginx/sites-available/01_mail.example.com:

index index.html;

server {
        listen 80;
        listen [::]:80;
        server_name autoconfig.example.com autodiscover.example.com mail.example.com;
        return 301 https://$host$request_uri;
}

server {
        listen 443 ssl;
        listen [::]:443 ssl;
        server_name autoconfig.example.com autodiscover.example.com mail.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot

        location / {
                proxy_pass http://localhost:3721;
        }

        location /.well-known {
                alias /var/www/example.com/htdocs/.well-known;
        }
}

Create the symlink:

ln -s /etc/nginx/sites-available/01_mail.example.com /etc/nginx/sites-enabled/01_mail.example.com

Reload the Nginx configuration:

/etc/init.d/nginx reload

Add the following DNS-record:

_autodiscover._tcp SRV 0 1 443 autodiscover.example.com.

Add the subdomains to the Let's Encrypt certificate.

Testing

Use an e-mail client with an account wizard like Thunderbird to test the setup. It should now be able to automatically detect the right settings for e-mail.

You should also be able to download a MobileConfig file for Apple devices via https://mail.example.com/email.mobileconfig?email=test@example.com.


If you like my work or if my work has been useful to you in any way, then feel free to donate me a cup of coffee. Any donation is much appreciated!