The catch is how your script does it right now. You’re writing data/conf/postfix/sni.map and data/conf/dovecot/sni.conf yourself, but mailcow regenerates both files from scratch every time the postfix/dovecot containers start (reboot,..). It only includes cert folders in data/assets/ssl/ that contain a domains file. Yours don’t have one, so after every restart both files end up empty and every client gets the default *.domain1.com cert, until your script runs again. That’s probably why the error only shows up “lately” and everything looks fine when you check it. Easy to reproduce:
docker compose restart dovecot-mailcow
openssl s_client -connect imap.domain2.com:993 -servername imap.domain2.com </dev/null 2>/dev/null | openssl x509 -noout -subject
You should now see *.domain1.com, until you run your script again.
(On the fallback question: the server has to present some certificate. If the SNI name isn’t in the map, or the map is empty, it serves the default data/assets/ssl/cert.pem. It isn’t “trying” domain1, that’s just the default.)
Fix is to let mailcow build the SNI config itself. Put a domains file (a space-separated list of hostnames) next to each cert.pem/key.pem, and on every start Postfix, Dovecot and Nginx will pick it up. It’s the same mechanism mailcow’s own ACME uses. Everything stays in data/assets/ssl/, which updates don’t touch. Your script would then look like this (LLM generated, no guarantee):
#!/bin/bash
ZORAXY_HOST="root@####"
SSH_PORT="#####"
ZORAXY_DIR="/opt/zoraxy/conf/certs"
MAILCOW_DIR="/opt/mailcow-dockerized/data/assets/ssl"
# Hash of all certs + domains files, to restart only when something changed
state() { sha256sum "${MAILCOW_DIR}"/*/cert.pem "${MAILCOW_DIR}"/*/domains 2>/dev/null | sha256sum; }
BEFORE=$(state)
echo "Fetching certificate list from Zoraxy..."
REMOTE_FILES=$(ssh -p "${SSH_PORT}" "${ZORAXY_HOST}" "ls -1 ${ZORAXY_DIR}/_.*.pem")
for FILE_PATH in $REMOTE_FILES; do
FILENAME=$(basename "$FILE_PATH")
DOMAIN=${FILENAME#_.}
DOMAIN=${DOMAIN%.pem}
echo "Processing wildcard certificate for: ${DOMAIN}"
# 1. Create a single clean folder per domain and pull certs
mkdir -p "${MAILCOW_DIR}/${DOMAIN}"
scp -P "${SSH_PORT}" -q "${ZORAXY_HOST}:${ZORAXY_DIR}/_.${DOMAIN}.pem" "${MAILCOW_DIR}/${DOMAIN}/cert.pem"
scp -P "${SSH_PORT}" -q "${ZORAXY_HOST}:${ZORAXY_DIR}/_.${DOMAIN}.key" "${MAILCOW_DIR}/${DOMAIN}/key.pem"
chmod 644 "${MAILCOW_DIR}/${DOMAIN}/cert.pem"
chmod 600 "${MAILCOW_DIR}/${DOMAIN}/key.pem"
# 2. Hostnames this cert is served for; mailcow builds Postfix/Dovecot/Nginx SNI from this on every container start
echo "mail.${DOMAIN} imap.${DOMAIN} smtp.${DOMAIN} autodiscover.${DOMAIN} autoconfig.${DOMAIN}" > "${MAILCOW_DIR}/${DOMAIN}/domains"
# If this is domain1.com(primary), update the root fallback cert
if [ "${DOMAIN}" == "domain1.com" ]; then
cp -f "${MAILCOW_DIR}/${DOMAIN}/cert.pem" "${MAILCOW_DIR}/cert.pem"
cp -f "${MAILCOW_DIR}/${DOMAIN}/key.pem" "${MAILCOW_DIR}/key.pem"
fi
done
if [ "$BEFORE" == "$(state)" ]; then
echo "No certificate changes, nothing to restart."
exit 0
fi
# Restart, not reload: SNI config is only generated at container start
echo "Certificates changed, restarting mail services..."
cd /opt/mailcow-dockerized || exit 1
docker compose restart postfix-mailcow dovecot-mailcow nginx-mailcow
echo "All domains synced and live!"