troubleshooting Question

phpmailer connection refused .. line 254

Avatar of Torquil Beavis
Torquil BeavisFlag for Canada asked on
PHPEmail ServersEmail Protocols
42 Comments13 Solutions3592 ViewsLast Modified:
My mail server is accepting mail() connections. However, I'm changing to phpmailer and got an error message that I don't understand.
Here's the phpmailer code that I'm using:
<?php
            $subject = "Registration Confirmation";

            // The non-HTML text
            $alt_body = "\r\n** CONFIDENTIAL **\r\n\r\n";
            $alt_body .= "Thank you for registering.\r\n\r\n";

            // HTML text
            $body = "** CONFIDENTIAL **<br><br>";
            $body .= "Thank you for registering.<br><br>";

            $mail = new PHPMailer;

            $mail->SMTPDebug = 3;                               // Enable verbose debug output   ####################### CHANGE TO 0 FOR PRODUCTION ##############################

            $mail->isSMTP();                                      // Set mailer to use SMTP
            $mail->Host = 'smtp.example.com';  // Specify main and backup SMTP servers
            $mail->SMTPAuth = true;                               // Enable SMTP authentication
            $mail->Username = "support@example.com";                 // SMTP username
            $mail->Password = "password";                           // SMTP password
            $mail->SMTPSecure = 'none';                            // Enable TLS encryption, `ssl` also accepted
            $mail->Port = 587;                                    // TCP port to connect to

            $mail->From = 'support@example.com';
            $mail->FromName = "Support";
            $mail->addAddress("name@gmail.com");     // Add a recipient
            $mail->addReplyTo('support@example.com', 'Support');
            $mail->WordWrap = 50;
            $mail->isHTML(true);                                  // Set email format to HTML

            $mail->Subject = $subject;
            $mail->Body    = $body;
            $mail->AltBody = $alt_body;

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

And the error message:
2015-04-10 18:46:57 Connection: opening to smtp.example.com:587, t=300, opt=array ( )

Error '2'
occurred in script '/folders/includes/class.smtp.php' on line 254:
stream_socket_client(): unable to connect to smtp.example.com:587 (Connection refused)
with 'Array'
Date/Time: 10-4-2015 14:46:57

2015-04-10 18:46:57 SMTP ERROR: Failed to connect to server: Connection refused (111) 2015-04-10 18:46:57 SMTP connect() failed. Message could not be sent.Mailer Error: SMTP connect() failed.temp exit

And the chunk of class.smtp.php code that's pointed at:
<?php
// In class.smtp.php ..
        if ($streamok) {
            $socket_context = stream_context_create($options);
            //Suppress errors; connection failures are handled at a higher level
            $this->smtp_conn = @stream_socket_client(
                $host . ":" . $port,
                $errno,
                $errstr,
                $timeout,
                STREAM_CLIENT_CONNECT,
                $socket_context
            );                                     // <<<<<<<<<<<<< LINE 254 IN class.smtp.php <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

I cannot find where STREAM_CLIENT_CONNECT is defined in either class.smtp.php or class.phpmailer.php . Could that be the issue?
Ideas?
ASKER CERTIFIED SOLUTION
Marcus Bointon

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 13 Answers and 42 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 13 Answers and 42 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros