You could also solve this using the SigningTable
and KeyTable
options.
Editing your example, I commented out the old Domain
, KeyFile
, & Selector
options. This is not strictly required, since they are ignored when SigningTable
and KeyTable
are present:
# Sign for example.com with key in /etc/dkimkeys/dkim.key using
# selector '2007' (e.g. 2007._domainkey.example.com)
# These options are ignored if you use SigningTable / KeyTable
# Domain smtpmail.rapidseohost.com
# KeyFile /etc/dkimkeys/smtpmail.private
# Selector smtpmail
# Use tables to map domain (host) names to keys
SigningTable refile:/etc/opendkim/signing_table
# This doesn’t necessarily need to be a refile, a plain file works most of the time
KeyTable refile:/etc/opendkim/key_table
Then you need to create /etc/opendkim/signing_table
as a map from wildcard-emails to a key name:
# email key name
*@smtpmail.rapidseohost.com smtpmail
*@rapidseohost.com smtpmail
And create /etc/opendkim/key_table
to map key name(s) to tuples of domain:selector:key_file
# key name domain:selector:key_file
smtpmail rapidseohost.com:smtpmail-2023-07:/etc/dkimkeys/smtpmail-2023-07.private
The domain
portion of the KeyTable
record can be replaced with a %
. In that case the “apparent domain” (including any hostname) is substituted. A %
in the key_file
protion of the tuple is also replaced by the “apparent domain.” This could allow you to have a single record in the KeyTable
for multiple domains and simply create key files for each one.
For illustration:
all_domains %:dkim-2023-07:/etc/dkimkeys/%-2023-07.private
NOTE: It is recommended you rotate your DKIM keys periodically. The recommendation is every 6 months. That’s why I added the year & month on the selector and key files.
When it’s time to rotate, you can
- Replace this line with the new selector and key file.
- Add the new selector record to your DNS rather than replace the existing one.
Doing so should avoid any race conditions from downstream servers trying to validate on a key you have replaced.