Phpmailer: Connection failed. Error #2

Created on 8 Jun 2018  路  7Comments  路  Source: PHPMailer/PHPMailer

hi! I know this question has been answered many times (Godaddy & smtp) but I have no idea to solve this.
FYI I am using Linux hosting with cpanel.

<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

require_once 'PHPMailer/src/PHPMailer.php';
require_once 'PHPMailer/src/SMTP.php';

$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 3; // Enable verbose debug output
    $mail->isSMTP(); // Set mailer to use SMTP
    $mail->Host = 'relay-hosting.secureserver.net'; // Specify main and backup SMTP servers
    $mail->SMTPAuth = false; // Enable SMTP authentication
    $mail->Username = ''; // SMTP username
    $mail->Password = ''; // SMTP password
    $mail->SMTPSecure = false; //'ssl'; //'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 25; //587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('[email protected]', 'no-name');
    $mail->addAddress('[email protected]', 'John Doe'); // Add a recipient

    //Content
    $mail->isHTML(true); // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
Connection: opening to relay-hosting.secureserver.net:25, timeout=300, options=array()
Connection failed. Error #2: stream_socket_client(): unable to connect to relay-hosting.secureserver.net:25 (Connection refused)
SMTP ERROR: Failed to connect to server: Connection refused (111)

Most helpful comment

I've struggled with this and Godaddy support as well, and this is working using a gmail account as the From account. I finally came across a post on SO with these options: $mail->SMTPAutoTLS = false; and $mail->Host = 'localhost';. Unfortunately, I accidentally exited out of the tab to that post so I cannot offer explanation or credit the poster :/

Here's a test script that works for me:

isSMTP();
$mail->SMTPDebug = 3;
$mail->Host = 'localhost'; //'relay-hosting.secureserver.net' didn't work
$mail->Port = 25;
$mail->SMTPAuth = false;
$mail->SMTPSecure = false;
$mail->SMTPAutoTLS = false;


$mail->setFrom('[email protected]', 'no-name'); // Add a sender
$mail->addAddress('[email protected]'); // Add a recipient
$mail->isHTML(true);                      // Set email format to HTML
$mail->Subject = 'Test Email';
$mail->Body    = 'Some body';
$mail->CharSet = 'utf-8';
$mail->AltBody = 'alt body';

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    //http_response_code(204);
}

All 7 comments

As far as I know, you're doing everything right here if you want to use SMTP - I would ask GoDaddy support. Alternatively, I think that they provide a local mail server on shared hosting, so you could try using isMail() instead of isSMTP(). Alternatively, switch ISPs - scaleway are very cheap and you can get them to allow SMTP directly.

I've struggled with this and Godaddy support as well, and this is working using a gmail account as the From account. I finally came across a post on SO with these options: $mail->SMTPAutoTLS = false; and $mail->Host = 'localhost';. Unfortunately, I accidentally exited out of the tab to that post so I cannot offer explanation or credit the poster :/

Here's a test script that works for me:

isSMTP();
$mail->SMTPDebug = 3;
$mail->Host = 'localhost'; //'relay-hosting.secureserver.net' didn't work
$mail->Port = 25;
$mail->SMTPAuth = false;
$mail->SMTPSecure = false;
$mail->SMTPAutoTLS = false;


$mail->setFrom('[email protected]', 'no-name'); // Add a sender
$mail->addAddress('[email protected]'); // Add a recipient
$mail->isHTML(true);                      // Set email format to HTML
$mail->Subject = 'Test Email';
$mail->Body    = 'Some body';
$mail->CharSet = 'utf-8';
$mail->AltBody = 'alt body';

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    //http_response_code(204);
}

Fairly likely I wrote that :) That is indeed a correct config for sending via a local mail server without using auth or encryption. It's probably SMTPAutoTLS that was causing a problem - if the server running on localhost offers STARTTLS, PHPMailer will try to use it - but it will always fail with a host name mismatch; this is why you can turn it off.

Oh good, then thanks for that haha. The only thing that is somewhat bothersome is that it takes a lot longer for emails to go through. Well, as opposed to not going through at all it's great but it does take a few minutes to show up in an inbox..

To help others struggling with GoDaddy, these two variations worked for me:

Hosting: GoDaddy shared/cPanel

Sending "From:" a GMail address

PHPMailer version 6.0.5

  $mail = new PHPMailer;
  $mail->isSMTP();

  $mail->SMTPDebug = 2;

  $send_using_config = 1; // set to 1, 2, etc. to test different settings
  switch ($send_using_config):
    case 1:
      $mail->Host = 'localhost';
      $mail->Port = 25;
      $mail->SMTPSecure = FALSE;
      $mail->SMTPAuth = FALSE;
      $mail->SMTPAutoTLS = FALSE;
      break;
    case 2:
      # Host and Port info obtained from:
      #   Godaddy > cPanel Home > Email > cPanel Email > Mail Configuration > "Secure SSL/TLS Settings" > Outgoing Server
      $mail->Host = 'a2plcpnxyzw.prod.iad2.secureserver.net';
      $mail->Port = 465;
      $mail->SMTPSecure = 'ssl';
      $mail->SMTPAuth = FALSE;
      $mail->SMTPOptions = array(
          'ssl' => array(
            'verify_peer' => FALSE,
            'verify_peer_name' => FALSE,
            'allow_self_signed' => TRUE
          )
      );
      break;
  endswitch;

  $mail->Username = '[email protected]';
  $mail->Password = 'your_gmail_password';

  $mail->setFrom($from);
  $mail->addAddress($to);
  $mail->Subject = $subject;
  $mail->msgHTML($message);
  $mail->send();

These settings are based on the question posted here and modified with the advice from @Synchro on StackOverflow and GitHub. Thank you @Synchro!

I've struggled with this and Godaddy support as well, and this is working using a gmail account as the From account. I finally came across a post on SO with these options: $mail->SMTPAutoTLS = false; and $mail->Host = 'localhost';. Unfortunately, I accidentally exited out of the tab to that post so I cannot offer explanation or credit the poster :/

Here's a test script that works for me:

isSMTP();
$mail->SMTPDebug = 3;
$mail->Host = 'localhost'; //'relay-hosting.secureserver.net' didn't work
$mail->Port = 25;
$mail->SMTPAuth = false;
$mail->SMTPSecure = false;
$mail->SMTPAutoTLS = false;


$mail->setFrom('[email protected]', 'no-name'); // Add a sender
$mail->addAddress('[email protected]'); // Add a recipient
$mail->isHTML(true);                      // Set email format to HTML
$mail->Subject = 'Test Email';
$mail->Body    = 'Some body';
$mail->CharSet = 'utf-8';
$mail->AltBody = 'alt body';

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    //http_response_code(204);
}

Thanks. Your approach worked and saved me a lot of time

$mail->SMTPAutoTLS = false;

Had same issue with the GoDaddy server and applied the same. Thanks

Was this page helpful?
0 / 5 - 0 ratings