PHP Warning: stream_socket_enable_crypto(): Peer certificate CN=example.host' did not match expected CN=smtp.sendgrid.net’

This PHP warning usually occurs with an email library such as phpMailer just before SMTP authentication fails.

The reason for the warning is because phpMailer is trying to verify the peer on the remote mail server but the SMTP request is rerouted through to your own server hostname so the certificates don’t match.

In WHM/cPanel the reason for this error is this setting:

Restrict outgoing SMTP to root, exim, and mailman (FKA SMTP Tweak)

And the description of the setting is:

Enabling this feature will redirect outgoing SMTP connections to the local mail server. root, exim, and mailman are still allowed to make direct connections.

Turning that off in WHM under Server Configuration > Tweak Settings solves the issue.