Connection: opening to ssl - Connection failed. Error #2: stream_socket_client()

Hello, I have an issue with sending an email, which only happens on a live site and it works properly on localhost.

$mail = new PHPMailer;
        $mail->isSMTP();                                 // Set mailer to use SMTP
        $mail->SMTPDebug = 3;
        $mail->Host = '';       // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                          // Enable SMTP authentication
        $mail->Username = '[email protected]';      // SMTP username
        $mail->Password = 'mypasswordhere';              // SMTP password
        $mail->SMTPSecure = 'ssl';                       // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 465;                               // TCP port to connect to

I use Cloudflare redirection from http:// to https:// and from www to http://(non-www) please tell me if this can be a reason.

Live - network tab - response

2017-08-08 10:30:55 Connection: opening to ssl://, timeout=300, options=array (
2017-08-08 10:30:56 Connection failed. Error #2: stream_socket_client(): unable to connect to ssl:// (Connection refused) [/home/milemija/public_html/phpmailer/class.smtp.php line 293]
2017-08-08 10:30:56 SMTP ERROR: Failed to connect to server: Connection refused (111)
2017-08-08 10:30:56 SMTP connect() failed. Troubleshooting · PHPMailer/PHPMailer Wiki · GitHub
Message could not be sent.Mailer Error: SMTP connect() failed. Home · PHPMailer/PHPMailer Wiki · GitHub

Localhost with the same code as on live site - network tab - response

{“signal”:“ok”,“msg”:“Form submitted”}

If is your Cloudflare-fronted server, you’re trying to connect to a Cloudflare IP address.

You’ll need to change the “Host” entry to an IP address or a DNS name that points to the origin server’s raw IP address. I’d go with the raw IP address so you won’t have to set up a DNS entry that would expose your origin server’s IP address. Just remember that you’ll have to edit your script if your IP address changes.

@sdayman thank you for reply, but I’m not sure I understood you completely. I use $mail->Host = ‘’; that works properly , but on the live server does not.

DNS section in attachment

Hmmm, I just tried to telnet the mail server you listed and here’s the output:

$ telnet 465
telnet: Unable to connect to remote host: Resource temporarily unavailable

So, it could be a host issue.

My domain is on Cloudflare,

I think that I need to use Cloudflare server , is that right ? What is SMTP Host of Cloudflare?

There’s no SMTP here. You’ll have to connect to an outside mail server.

Which ‘localhost’ were you using when you were testing?

localhost -

I still think it’s trying to connect to the Cloudflare IP address.

When testing locally, it probably knows the origin IP address. Have you tried using the server’s origin IP address for the Host = parameter?