
Let's say the following is being returned when attempting to send an email using PHPMailer.
SMTP Error: Could not connect to SMTP host.
Your PHP should have the host and port of the email server defined.
$mail->Host = "mx1.example.com";
$mail->Port = 25;
Assuming PHPMailer is running on a Linux system, there are a number of different tools that can be used determine if you are able to connect to the email server with SMTP.
- Test connection to an email server on SMTP port 25 using openssl
- Test connection to an email server on SMTPS port 587 using openssl
If the connection is successful, both CONNECTED and return code: 0 (ok) should be included in the output.
~]$ openssl s_client -connect mx1.example.com:25
CONNECTED(00000003)
Verify return code: 0 (ok)
If you are connecting to an email server that uses SSL / TLS encryption (e.g. STARTTLS), ensure that you have the openssl module enabled in your PHP.
~]$ php --modules
[PHP Modules]
openssl
If you are connecting to an email server that uses SSL / TLS encryption (e.g. STARTTLS), openssl can be used to determine if the SSL certificate being used is OK. Notice in this example that verify error "self signed certificate" is included in the output.
~]$ openssl s_client -starttls smtp -connect mx1.example.com:587
CONNECTED(00000003)
depth=0 C = US, ST = WI, L = Appleton, O = FreeKB, OU = FreeKB, CN = FreeKB, emailAddress = support@freekb.net
verify error:num=18:self signed certificate
If you are using a self signed certificate, include the following PHP to allow the use of self signed certificates.
$mail->SMTPOptions = [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true,
]
];
Did you find this article helpful?
If so, consider buying me a coffee over at