Skip to content

Instantly share code, notes, and snippets.

@jfstenuit
Created August 2, 2023 07:40
Show Gist options
  • Save jfstenuit/8bdf0acb1b661f1823ec885c503c30b1 to your computer and use it in GitHub Desktop.
Save jfstenuit/8bdf0acb1b661f1823ec885c503c30b1 to your computer and use it in GitHub Desktop.
Easily add domains to OpenDKIM

Configure a new domain in OpenDKIM

#!/bin/bash

DOMAIN="$1"
SELECTOR="mail"

if [ -z "${DOMAIN}" ]; then
        echo "Usage: $0 domain.name"
        exit
fi

if [ -z "$( dig +short ${DOMAIN} mx)" ]; then
        echo "Domain has no MX records"
        exit
fi

mkdir /etc/opendkim/keys/${DOMAIN}
echo "${SELECTOR}._domainkey.${DOMAIN} ${DOMAIN}:mail:/etc/opendkim/keys/${DOMAIN}/${SELECTOR}.private" >>/etc/opendkim/KeyTable
echo "*@${DOMAIN} ${SELECTOR}._domainkey.${DOMAIN}" >>/etc/opendkim/SigningTable
echo "${DOMAIN}" >>/etc/opendkim/TrustedHosts
sudo opendkim-genkey -b 1024 -d "${DOMAIN}" -D /etc/opendkim/keys/${DOMAIN} -s ${SELECTOR} -v
sudo chown opendkim:opendkim /etc/opendkim/keys/${DOMAIN}/${SELECTOR}.private

sudo cat /etc/opendkim/keys/${DOMAIN}/${SELECTOR}.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment