Nodemailer: Error: self signed certificate in certificate chain (code: 'ESOCKET') / unable to verify the first certificate
Nodemailer refused the SMTP server's TLS certificate because it is self-signed, expired, hostname-mismatched or missing its intermediate certificate.
Error: self signed certificate in certificate chainself-signed certificate nodemailerunable to verify the first certificate nodemailerERR_TLS_CERT_ALTNAME_INVALID nodemailerHostname/IP does not match certificate's altnames Meaning
Node verifies the server certificate by default. For your own mail server, install a valid certificate (Let's Encrypt) with the full chain. In controlled test setups you can trust a specific CA with tls.ca; disabling verification (rejectUnauthorized: false) exposes passwords to man-in-the-middle attacks and should not be used in production.
Common causes
- Self-signed certificate on the mail server
- Certificate expired or issued for another hostname
- Missing intermediate chain
- Corporate TLS inspection proxy
- Connecting by IP instead of the certificate's hostname
⚡ Quick fix
- Connect using the hostname in the certificate
- Install a trusted certificate with the full chain
- For internal CAs set
tls: { ca: fs.readFileSync("ca.pem") }
Detailed fix by platform
Linux
openssl s_client -starttls smtp -connect mail.example.com:587 -servername mail.example.com </dev/null | openssl x509 -noout -subject -issuer -dates
Code examples
Trust your own CA (not disabling verification)
const fs = require("fs");
const t = nodemailer.createTransport({
host: "mail.internal.example", port: 587, secure: false,
tls: { ca: fs.readFileSync("/etc/ssl/internal-ca.pem") },
});How to diagnose
- Cert — Issuer, dates and CN?
- Host — Matches the certificate name?
- Chain — Intermediates present?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- 421 4.7.0 Try again later (rate limited) SMTP: 421 4.7.0 Our system has detected an unusual rate of unsolicited mail originating from your IP address. Mail sent from your IP address has been temporarily rate limited
- 530 5.7.0 Authentication Required SMTP: 530 5.7.0 Must issue a STARTTLS command first / 530 5.7.1 Authentication required
- Laravel: Connection could not be established with host Laravel: Symfony\Component\Mailer\Exception\TransportException: Connection could not be established with host "smtp.mailtrap.io:2525": stream_socket_client(): Unable to connect
Most viewed in Email / SMTP
Other ways to find it
Where this error sits
Origin: Network. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 4 Nov 2026