The website and the mailbox are served by different daemons
- Dovecot handles IMAP on ports 993 and 143, and POP3 on 995 and 110.
- Exim or Postfix handles SMTP on ports 465 and 587, and on 25 for server-to-server mail.
What happens on the wire when the client connects
- The client opens a TCP connection.
- Before any mail protocol text is exchanged, the client sends a TLS ClientHello message.
- The server reads the ClientHello, picks a certificate, and replies with ServerHello and its certificate chain.
- The client validates the chain against its trusted root store, then checks that the hostname it dialed appears in the certificate's SAN list.
- The server sends a greeting banner.
- The client sends EHLO for SMTP or CAPABILITY for IMAP, and the server advertises STARTTLS.
- The client sends STARTTLS, and the server agrees.
- From this point, the handshake is the same as above, starting with the ClientHello.
How Dovecot and Exim pick a certificate
TEXT
# Dovecot 2.3-style syntax (Dovecot 2.4 renamed these settings)
ssl_cert = </etc/ssl/certs/server-hostname.pem
ssl_key = </etc/ssl/private/server-hostname.key
local_name mail.yourdomain.com {
ssl_cert = </etc/ssl/certs/mail.yourdomain.com.pem
ssl_key = </etc/ssl/private/mail.yourdomain.com.key
}
# Exim main section: pick a file by SNI, fall back to the hostname cert
tls_certificate = {if exists{/etc/exim/certs/{tls_in_sni}.crt}{/etc/exim/certs/${tls_in_sni}.crt}{/etc/exim/certs/hostname.crt}}Why the client usually ends up with the hostname certificate
- No certificate covering mail.yourdomain.com exists on the server. Automated issuance often validates each name over HTTP or DNS. If the mail subdomain's DNS record points somewhere else, validation for that name can fail while the main domain succeeds.
- The mail subdomain resolves to a different server than the one hosting the mailbox. This is common after a migration where the A record for the mail subdomain was missed.
- The certificate exists, but the daemon was not reloaded after it was installed or renewed. In that case it may still be serving older material. Whether a reload is required depends on the version and configuration.
- The client sends no SNI, or connects using a different name than the one shown in its settings. Outlook's Autodiscover, for example, can supply the server hostname instead.
Confirming it with openssl s_client
BASH
# IMAP over implicit TLS (993), sending SNI for the mail subdomain
openssl s_client -connect mail.yourdomain.com:993 -servername mail.yourdomain.com </dev/null 2>/dev/null | openssl x509 -noout -subject -issuer -ext subjectAltName
# SMTP submission with STARTTLS (587)
openssl s_client -connect mail.yourdomain.com:587 -starttls smtp -servername mail.yourdomain.com </dev/null 2>/dev/null | openssl x509 -noout -subject -issuer -ext subjectAltName
# Same IMAP port with no SNI, to see the fallback certificate
openssl s_client -connect mail.yourdomain.com:993 -noservername </dev/null 2>/dev/null | openssl x509 -noout -subject -ext subjectAltNameTEXT
subject=CN = server1.example-host.net
issuer=C = XX, O = Example CA, CN = Example Issuing CA
X509v3 Subject Alternative Name:
DNS:server1.example-host.net- If the SNI test returns a certificate with mail.yourdomain.com in the SAN list, the server is fine. The client is either not sending SNI or is connecting with a different name, so check the exact server name in its account settings.
- If the SNI test returns the hostname certificate, the server has no match for that name. The fix belongs on the server.
- If the names match but the client still says untrusted, drop the pipe to openssl x509 and read the Verify return code line in the full s_client output. A missing intermediate certificate typically shows up there.
Fixing the server before touching the client
- Make sure the mail subdomain's DNS points to the server that holds the mailbox.
- Get mail.yourdomain.com included in a certificate. Depending on the setup, that means either reissuing the domain's certificate with the extra name or issuing a separate certificate for it.
- Make sure Dovecot and Exim map that name to the certificate, either through the panel or through config like the example above.
- Reload the services if your version requires it.
- Run the same s_client commands again. Only when they return the right name should anyone look at client settings.
