1

So I have this application that I built that uses PHPMailer to send mail. The code works perfectly from my local machine, and it was working perfectly on the server up until yesterday. However now when I run the code on my server I get:

Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

I have literally tried everything. I cannot figure out why this is not working. From the server as the user the site is installed to, I can telnet localhost 465 and I can telnet localhost 587 so the server is up, and its not blocking connections to itself.

The only thing I can think of is, this being a Cpanel server some sort of update must have happened with apache/PHP which caused something to not work right with this. I know the problem is not with the mail server and its not with the code (since it works when I run this exact code from my local machine) which only leaves out the PHP/Apache config on the server...

Does anyone have any insight on what this might be?

PS- I have to use SSL, its the only thing my server supports that I'm aware of,at least I've never had any luck using TLS or unencrypted.

Here is my code:

require("vendor/autoload.php");
$mail = new \PHPMailer;
// set up mail settings
$mail->isSMTP();
$mail->Host = 'my.host.com';
$mail->SMTPAuth = true;
$mail->Username = 'username';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;

$mail->Debugoutput = function($str, $level) {
    $GLOBALS['debug'][] = $Host . " - " . $str . " - " . $level;
};

$mail->SMTPDebug = 2;
$mail->CharSet = "UTF-8";
$mail->setFrom(.....);
$mail->addAddress(.....);
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body    = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional //EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><body>This is the HTML message body <b>in bold!</b></body></html>';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

EDIT:

I changed debug level to 3 (for connection debugging, derp) and I get this:

[0]=> string(88) " - Connection: opening to ssl://host.host.com:465, timeout=300, options=array ( ) - 3"

[1]=> string(291) " - Connection failed. Error #2: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed [/path/to/vendor/phpmailer/phpmailer/class.smtp.php line 294] - 3"

[2]=> string(175) " - Connection failed. Error #2: stream_socket_client(): Failed to enable crypto [/path/to/vendor/phpmailer/phpmailer/class.smtp.php line 294] - 3"

[3]=> string(215) " - Connection failed. Error #2: stream_socket_client(): unable to connect to ssl://my.host.com:465 (Unknown error) [/path/to/vendor/phpmailer/phpmailer/class.smtp.php line 294] - 3"

[4]=> string(52) " - SMTP ERROR: Failed to connect to server: (0) - 1"

[5]=> string(89) " - SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting - 3" }

So I was right, its SSL related... So anyone know how to fix this?

Rick Kukiela
  • 1,135
  • 1
  • 15
  • 34
  • Why the down vote? – Rick Kukiela Aug 10 '17 at 17:18
  • Next logical step would be to Google your errors a bit. Again, if you didn't change ANYTHING, maybe your email provider changed its settings? Or is it your own server? Did you try simply setting encryption to 'tls'? – GregKos Aug 10 '17 at 17:58
  • Yeah I am googling it. I know i can get past this with ssloptions but I would prefer to get the certificate chain working. I'm waiting to hear back from the server people. – Rick Kukiela Aug 10 '17 at 18:15
  • If you read the troubleshooting guide that the error message links to, you will find it has a detailed description of exactly this problem and how to solve it. – Synchro Aug 10 '17 at 18:28
  • It does not explain how to solve it. It gives some help, sure, however running the openssl command line commands actually show that the verify on the certificate of my mail server is valid so then why does PHP/openssl unable to verify it? The docs dont cover that. – Rick Kukiela Aug 10 '17 at 19:19
  • You can check this answer too https://stackoverflow.com/a/28759959/259881 – HasanG Nov 28 '17 at 12:29

1 Answers1

2

The exact same thing happened with one of our live sites a while back. Spent a few hours troubleshooting the code (we're using PHPMailer as well), but it ended up being a firewall update on the data center. Check your cPanel settings and/or open a ticket with your hosting provider.

If you didn't change anything on your code and it suddenly stopped working, it's most likely NOT an issue with your code. In my case it was the server firewall blocking outbound connections. Look into that.

GregKos
  • 256
  • 2
  • 11
  • I am working with my provider on this, howerver I have verified that its not a firewall issue, i even wrote a php script to open a socket connection to the same host and port and it returns true, so PHP is able to open a socket connection to the host and port I'm using in my config. Its gotta be SSL negotiation. – Rick Kukiela Aug 10 '17 at 17:42
  • Have you tried going to verbose debug mode with PHPMailer? – GregKos Aug 10 '17 at 17:45
  • OMG I never realized there was higher debug level than 2. 3 is what I needed. See edit to question for update! Thanks! – Rick Kukiela Aug 10 '17 at 17:50
  • Actually, there is also 4 and I think 5 too. Although the documentation doesn't mention them. – GregKos Aug 10 '17 at 17:52
  • This is technically the answer to my question, thought the real issue is another question entirely. Thanks for the help. – Rick Kukiela Aug 10 '17 at 18:16
  • Np, thanks for the mark and gl further. Unfortunately I am not an ssl expert! – GregKos Aug 10 '17 at 18:19
  • If this was a firewall problem, telnet would have failed. There is no debug level 5, and all the levels are documented. – Synchro Aug 10 '17 at 18:27