ERROR: Failed to connect to server: Connec" /> ERROR: Failed to connect to server: Connec" /> ERROR: Failed to connect to server: Connec"/>

In contact form it's working fine in localhost. While hosted it's not working. Showing the error

**"SMTP -> ERROR: Failed to connect to server: Connection timed out (110) The following From address failed: [email protected] ERROR"** 

I attached my contact_submit.php code form

    include_once('class.phpmailer.php');    

   $mail->IsSMTP(); // 
    $mail->Host       = "smtp.gmail.com"; 
    $mail->SMTPDebug  = 1;                    
    $mail->SMTPAuth   = true;                 
    $mail->Host       = "smtp.gmail.com"; 
    $mail->Port       = 587;                    
    $mail->Username   = "[email protected]"; 
    $mail->Password   = "xxxx@123";        
    $mail->SMTPSecure = "tls";
    $mail->SetFrom($email, $name);

    $mail->AddReplyTo($email,$name);

    $mail->Subject    = "Contact - xxx";

    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; 

    $mail->MsgHTML($body);
    $mail->AddAddress("[email protected]","xxx");


    if(!$mail->Send()) 
    {
        echo $mail;
      echo "Mailer Error: " . $mail->ErrorInfo;
    } 
    else
        {
      echo '<META HTTP-EQUIV="Refresh" Content="0; URL=contact.php?id='.$id.'&send=success">';
      exit; 
    }

I'm using phpmailer 5.2.1.

I contacted the hosting side, but i'm not getting actual response.

3

There are 3 best solutions below

3
mti2935 On

I believe you have to connect to smtp.gmail.com on port 465, not port 587. Also, SSL is required. So, you should have:

$mail->Host       = "smtp.gmail.com";      
$mail->Port       = 465;                   
$mail->SMTPSecure = "ssl";                 
3
Kit Johnson On

I had a similar problem, with mail being sent correctly from my local server but not my live one on the internet. It turned out my host (Bluehost) blocked outgoing connections on port 465.

I found a wonderful how-to which fixed it for me:

  1. In your cPanel > Mail, find the MX (MX Entry) section, and select 'remote mail exchanger'.
  2. In the cPanel email accounts section, create the appropriate email address (don't skip this)
  3. Don't use "smtp.live.com" as your smtp host. Use the smtp host of your Shared Linux Hosting smtp. I don't know how you will get yours. Mine is boxXXXX.bluehost.com.
  4. Set your username and password to be the same as the email account you just set-up in cPanel.
1
Michika Iranga Perera On

You can increase the time out by prepending your code with:

set_time_limit(3600);

and then specifying the Timeout of the $mail object as such:

$mail->Timeout = 3600;