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

ssl

#1

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 = 'myhostingserver.name.here';       // 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://myhostingserver.name.here:465, timeout=300, options=array (
)
2017-08-08 10:30:56 Connection failed. Error #2: stream_socket_client(): unable to connect to ssl://myhostingserver.name.here:465 (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. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting{“signal”:null,“msg”:null}

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

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


#2

If myhostingserver.name.here 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.


#3

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

DNS section in attachment


#4

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

$ telnet nextweb.space 465
Trying <REDACTED-IP-ADDRESS>...
telnet: Unable to connect to remote host: Resource temporarily unavailable

So, it could be a host issue.


#5

My domain is on CloudFlare, milemijatovic.com

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


#6

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

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


#7

localhost - http://www.milemijatovicc.com:8080/


#8

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?