Test SMTP and STARTTLS with openssl

· 4 min read

Whether a mail server encrypts, which TLS version it speaks and whether its certificate is valid: one command tells you. This guide explains openssl s_client -starttls smtp for ports 25, 587 and 465, the curl alternative, and the differences between OpenSSL 3 and the LibreSSL shipped with macOS that can skew the result.

The command for each port

Enter the mail server's host name, not the domain. The MX record tells you which server accepts mail for a domain: dig +short MX example.com or nslookup -type=MX example.com.

What to check
Tool

Only inserted into the commands in your browser.

Port 25 (inbound, MX) · openssl · Linux, macOS

openssl connects, sends STARTTLS and performs the TLS handshake. The filter shows the certificate (0 s:), the TLS version and the verification result. "Verify return code: 0 (ok)" means the chain is valid.

Shell
openssl s_client -connect mail.example.com:25 -starttls smtp -servername mail.example.com </dev/null 2>/dev/null | grep -E "Protocol|Cipher is|Verif|^ 0 s:"
Sample output
 0 s:CN=mail.example.com
Verification: OK
New, TLSv1.3, Cipher is TLS_AES_256_GCM_SHA384
Protocol: TLSv1.3
Verify return code: 0 (ok)

Port 25 (inbound, MX) · curl · Linux, macOS

curl speaks SMTP as well. --ssl-reqd aborts if the server does not offer STARTTLS; no message is sent. Handy where openssl is missing.

Shell
curl -sv --ssl-reqd smtp://mail.example.com:25 -o /dev/null 2>&1 | grep -E "SSL connection|subject:|expire date|issuer:"
Sample output
* SSL connection using TLSv1.3 / AEAD-CHACHA20-POLY1305-SHA256 / [blank] / UNDEF
*  subject: CN=mail.example.com
*  expire date: Dec 30 19:09:07 2026 GMT
*  issuer: C=US; O=Example Trust Services; CN=Example CA 1

Port 587 (submission) · openssl · Linux, macOS

Port 587 is what mail clients use for sending, also with STARTTLS. It is reachable from networks that block port 25.

Shell
openssl s_client -connect mail.example.com:587 -starttls smtp -servername mail.example.com </dev/null 2>/dev/null | grep -E "Protocol|Cipher is|Verif|^ 0 s:"
Sample output
 0 s:CN=mail.example.com
Verification: OK
New, TLSv1.3, Cipher is TLS_AES_256_GCM_SHA384
Protocol: TLSv1.3
Verify return code: 0 (ok)

Port 587 (submission) · curl · Linux, macOS

The same check with curl. The "expire date" line gives the certificate's expiry.

Shell
curl -sv --ssl-reqd smtp://mail.example.com:587 -o /dev/null 2>&1 | grep -E "SSL connection|subject:|expire date|issuer:"
Sample output
* SSL connection using TLSv1.3 / AEAD-CHACHA20-POLY1305-SHA256 / [blank] / UNDEF
*  subject: CN=mail.example.com
*  expire date: Dec 30 19:09:07 2026 GMT
*  issuer: C=US; O=Example Trust Services; CN=Example CA 1

Port 465 (implicit TLS) · openssl · Linux, macOS

On port 465 TLS starts immediately, so there is no -starttls. With -starttls openssl reports "Didn't find STARTTLS in server response" here.

Shell
openssl s_client -connect mail.example.com:465 -servername mail.example.com </dev/null 2>/dev/null | grep -E "Protocol|Cipher is|Verif|^ 0 s:"
Sample output
 0 s:CN=mail.example.com
Verification: OK
New, TLSv1.3, Cipher is TLS_AES_256_GCM_SHA384
Protocol: TLSv1.3
Verify return code: 0 (ok)

Port 465 (implicit TLS) · curl · Linux, macOS

The smtps:// scheme stands for TLS from the first byte.

Shell
curl -sv smtps://mail.example.com:465 -o /dev/null 2>&1 | grep -E "SSL connection|subject:|expire date|issuer:"
Sample output
* SSL connection using TLSv1.3 / AEAD-CHACHA20-POLY1305-SHA256 / [blank] / UNDEF
*  subject: CN=mail.example.com
*  expire date: Dec 30 19:09:07 2026 GMT
*  issuer: C=US; O=Example Trust Services; CN=Example CA 1

The "</dev/null" ends the session right after the handshake; otherwise openssl waits for input. Without the grep filter you see the full output including the certificate chain and session details.

Read the certificate and its expiry date

Pipe the output into openssl x509 to inspect the mail server's certificate. -checkend tests whether it expires within the given number of seconds (1209600 seconds are 14 days) and sets an exit code you can use in scripts.

Shell
openssl s_client -connect mail.example.com:25 -starttls smtp </dev/null 2>/dev/null | openssl x509 -noout -subject -issuer -enddate -checkend 1209600

The answer lists subject, issuer and "notAfter", plus "Certificate will not expire" or "Certificate will expire". The name in the certificate has to match the host name from the MX record, otherwise delivery fails for domains that enforce MTA-STS.

OpenSSL 3 versus LibreSSL (macOS)

macOS ships LibreSSL as /usr/bin/openssl. The commands above run on both, but the output differs, and two of the differences lead to wrong conclusions:

  • Certificate names: OpenSSL 3.2 and later prints "s:CN=mail.example.com", older versions such as 1.1.1 and 3.0 "s:CN = mail.example.com" with spaces, LibreSSL "s:/CN=mail.example.com". A filter written for one form misses the other.
  • The "New, …" line: LibreSSL prints "New, TLSv1/SSLv3" even when TLS 1.3 was negotiated. Go by the "Protocol" line.
  • Testing old TLS versions: OpenSSL 3 refuses -tls1_1 by itself in its default configuration ("no protocols available") without ever asking the server. That proves nothing about the server. LibreSSL 3.3.6 still negotiates TLS 1.1 and so shows whether the server accepts it.
  • Options: -brief (short summary) and -verify_hostname (checks the name) exist only in OpenSSL; LibreSSL answers "unknown option".
  • Host name: without -verify_hostname openssl checks the chain only, not whether the certificate matches the name. "Verify return code: 0" therefore does not mean the name is right.

When it fails

The commands above hide error messages and filter the output. For troubleshooting drop both, that is "2>/dev/null" and everything from "| grep" on; only then do the following messages appear. What they tell you:

  • No output, then "Operation timed out" after a while: port 25 is blocked, usually outbound by your ISP or office network. Test from a server or use port 587.
  • "Didn't find STARTTLS in server response, trying anyway...": the server does not offer STARTTLS, or the port speaks TLS right away (465) or no SMTP at all.
  • "wrong version number": you left out -starttls on a STARTTLS port and are speaking TLS to a server that expects plaintext.
  • "Verify return code: 21 (unable to verify the first certificate)": the server does not send the intermediate certificate. The mail server needs the full chain configured.
  • "Verify return code: 10 (certificate has expired)": the certificate is past its date, often because the mail server was not reloaded after renewal.
  • "Verify return code: 18 (self-signed certificate)": self-signed. Most senders still deliver to port 25; with MTA-STS in enforce mode they will not.
  • "Protocol: TLSv1" or "TLSv1.1": outdated. Current guidance, including Germany's BSI TR-03108, asks for TLS 1.2 at least.

Checking without a terminal

The quick check based on BSI TR-03108 tests STARTTLS, TLS version and certificate of a domain's mail servers together with DNSSEC, DANE, TLS-RPT, SPF, DKIM and DMARC and reports each criterion as met or not. The SSL checker reads mail server certificates through its port selector. Why mail server certificates expire unnoticed is the subject of a separate guide.

Frequently asked questions

How do I test STARTTLS with openssl?
openssl s_client -connect mail.example.com:25 -starttls smtp. If a certificate comes back and the output ends with "Verify return code: 0 (ok)", the server encrypts with a valid chain. For the submission port use 587.
What is the difference between ports 25, 587 and 465?
Port 25 carries delivery between mail servers, 587 is for sending from mail clients, both using STARTTLS. On 465 TLS starts immediately with no STARTTLS command.
Why do I get a timeout on port 25?
Many ISPs and office networks block outbound connections on port 25 to curb spam. The server is not down. Test from a machine in a data centre or with an online check.
Does openssl tell me whether the certificate matches the host name?
Only with the option -verify_hostname mail.example.com, which OpenSSL has and LibreSSL lacks. Without it s_client checks the chain but not the name.
Check now

Free quick check of STARTTLS, DANE, DNSSEC, TLS-RPT, SPF, DKIM and DMARC, criteria based on BSI TR-03108 and TR-03182, with recommendations.

DomainWarn checks certificates on web and mail ports of all client domains and reports expiry, wrong host names and incomplete chains before delivery stalls.

Monitor this domain continuously14-day free trial, no credit card.

More guides