A secure SMTP connection depends on a certificate. When your application connects with STARTTLS or implicit TLS, the server presents a certificate and the client checks whether it can trust it: whether it was issued by a trusted authority, whether it is still valid, and whether it matches the name that the client connected to. If any check fails, a well-configured client stops with an error such as certificate verify failed, unable to get local issuer certificate, self signed certificate, hostname mismatch or certificate has expired. The wrong response is to switch verification off and carry on, which removes the protection TLS exists to give. The right response is to find which check failed and repair the cause. This guide lists the common errors, shows the commands that reveal the cause, explains how to fix each one on the server and on the client side and covers when, and only when, a limited exception is acceptable.
Error messages and their meaning
| Message (varies by library) | Meaning | Usual cause |
|---|---|---|
| hostname mismatch / certificate is not valid for host | The name you connected to is not in the certificate | Connecting by IP or by an alias not listed in the certificate |
| certificate has expired | The validity period has ended | Renewal missed; or the client clock is wrong |
| certificate is not yet valid | Start date is in the future | Client clock behind, or a new certificate with wrong dates |
| self signed certificate | The certificate is its own issuer and not in the trust store | Internal or test server using a self-signed certificate |
| unable to get local issuer certificate / unable to verify the first certificate | Chain cannot be completed to a trusted root | Server did not send the intermediate certificate, or client lacks the root |
| certificate verify failed: unable to get issuer certificate | Same family | See above |
| wrong version number / SSL routines: unknown protocol | Not a certificate error: plain text or STARTTLS expected | Wrong port or wrong security mode; see below |
| handshake failure / no shared cipher / protocol version | Client and server cannot agree on a protocol | Old client with new server (or the reverse) |
Step 1: look at the certificate
# Implicit TLS (port 465)
openssl s_client -connect smtp.example.com:465 -servername smtp.example.com -showcerts
# STARTTLS (port 587 or 25)
openssl s_client -starttls smtp -connect smtp.example.com:587 -servername smtp.example.com -showcerts
# Print only the dates, subject and names
echo | openssl s_client -starttls smtp -connect smtp.example.com:587 -servername smtp.example.com 2>/dev/null \
| openssl x509 -noout -subject -issuer -dates -ext subjectAltNameLook at four things in the output. The subjectAltName list must include the exact host name you connect to. The notBefore and notAfter dates must contain today. The issuer should be a recognised authority. And the number of certificates shown after -showcerts should be at least two: the server certificate and the intermediate. The line Verify return code: 0 (ok) at the end means that openssl, using your system trust store, accepts the chain. See testing an SMTP connection.
Fixing each case
Hostname mismatch
Connect using the name that appears in the certificate, normally the provider's documented host name, not an IP address and not a private alias. If you use a CNAME to point a friendly name at the server, the certificate must list the friendly name or you should connect with the real one. If you run the server, issue a certificate that includes every name that clients use. Wildcard certificates cover one level only: *.example.com matches smtp.example.com but not a.smtp.example.com and does not match example.com itself.
Expired certificate
If the server certificate has expired, only the server operator can fix it by renewing and reloading the mail service. If you operate it, automate renewal and monitor expiry with an alert thirty days ahead. If the certificate is valid but the client reports expiry, check the client's clock; a machine with the wrong date breaks every TLS connection. Containers and virtual machines resumed from a snapshot are common culprits.
Missing intermediate certificate
A server must send its own certificate and the intermediate certificates that link it to a root. Browsers can sometimes fetch missing intermediates; mail clients and libraries generally cannot. The result is "unable to get local issuer certificate" for some clients and not others. The fix is on the server: configure the mail software with the full chain file (certificate followed by intermediates), not the leaf certificate alone. In Postfix this is smtpd_tls_chain_files or the cert file containing the chain; in Exim, the certificate file containing the chain.
Self-signed certificate
A self-signed certificate is not trusted by default, by design. For a production service, replace it with a certificate from a public authority (Let's Encrypt issues them free of charge). For an internal server, issue a certificate from your own certificate authority and install that authority's root certificate in the trust store of the clients. As a last resort in a closed test environment, pin the certificate or add it to the trust store; see the next section for what not to do.
Client trust store out of date
Old operating systems and runtimes may lack current root certificates, so a chain that is fine elsewhere fails there. Update the operating system or the certificate bundle (for example the ca-certificates package on Linux, or the certifi package for Python). Check that a proxy or antivirus program is not intercepting TLS and presenting its own certificate; if it is, the client sees an untrusted issuer, and the fix is to install that organisation's root or exclude mail traffic.
Wrong port or security mode
The error "wrong version number" or "unknown protocol" often appears when the client expects TLS immediately but the server starts in plain text (a STARTTLS port), or the reverse. Match the mode to the port: port 465 normally uses implicit TLS, port 587 uses STARTTLS. In library terms, choose the "SSL/TLS" option for 465 and "STARTTLS" for 587. See SMTP ports and connection timeouts and TLS errors.
Library settings
# Python: keep verification ON, and give the client the right CA bundle if needed
import smtplib, ssl
ctx = ssl.create_default_context() # uses system CAs
# ctx = ssl.create_default_context(cafile="/etc/ssl/certs/internal-ca.pem")
s = smtplib.SMTP("smtp.example.com", 587, timeout=30)
s.starttls(context=ctx)
// Node.js (Nodemailer): provide your CA instead of disabling checks
const transport = nodemailer.createTransport({
host: "smtp.example.com", port: 587, secure: false, requireTLS: true,
tls: { ca: fs.readFileSync("internal-ca.pem"), servername: "smtp.example.com" }
});Settings such as rejectUnauthorized: false in Node.js, ssl.CERT_NONE in Python, SMTPOptions with verify_peer => false in PHP and --insecure in curl turn off the check. They are tempting because they make the error go away. They also make your connection open to anyone who can intercept it, including the credentials you send. Use them only in an isolated test, never in production, and remove them afterwards. See the language guides: PHPMailer, Python smtplib and Nodemailer.
For operators: sending between servers
Server-to-server mail on port 25 is usually opportunistic: a sender that meets an invalid certificate often continues anyway, because refusing would block delivery. Policies such as MTA-STS and DANE change that for domains that publish them, requiring a valid certificate that matches the MX host. If your inbound server has a bad certificate, you may see failed deliveries from senders that honour these policies. See MTA-STS and TLS-RPT, DANE for SMTP and email TLS.
Checklist
- Connect by the documented host name, not by IP.
- Run openssl s_client and read subject names, dates and the verify result.
- Make sure the client clock is correct.
- Make sure the server sends the full chain.
- Update the client CA bundle, or add your internal CA to it.
- Match port and security mode (465 implicit TLS, 587 STARTTLS).
- Never ship code with certificate verification disabled.
- Set up alerts for certificate expiry on servers you run.
Certificate errors are precise messages: each names a check that failed. Read the message, inspect the certificate and repair the actual cause, and your SMTP connections will be both working and secure.
Use a relay with maintained certificates Connect with TLS to hosts whose certificates we renew and monitor.
References and further reading
Frequently asked questions
What does "certificate verify failed" mean for SMTP?
The client could not validate the server certificate: the chain, the dates or the host name check failed. Inspect it with openssl s_client to see which.
Why does the error say unable to get local issuer certificate?
The chain cannot be completed. Usually the server did not send the intermediate certificate, or the client lacks the root.
Is it safe to disable certificate verification?
Not in production. It exposes credentials and mail to interception. Fix the cause instead.
Why does a hostname mismatch occur when connecting by IP?
Certificates name hosts, not IPs. Connect using the host name listed in the certificate.
Why does "wrong version number" appear?
The client and server disagree on the TLS mode, for example implicit TLS on a STARTTLS port. Match the mode to the port.