1

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: xxxxx@gmail.com 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   = "xxxx@gmail.com"; 
    $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("xxx@gmail.com","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.

Dirty-flow
  • 2,306
  • 11
  • 30
  • 49
user3492847
  • 19
  • 1
  • 1
  • 2

3 Answers3

3

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";                 
mti2935
  • 11,465
  • 3
  • 29
  • 33
  • If anything i want to check in hosting side?? Bcoz i tried in another website this same code.. There it's working.. – user3492847 Apr 11 '14 at 16:23
  • Yes, it's possible that your host is blocking outgoing connections on one or more ports used for authenticated SMTP (e.g. 25, 587, 465). Some hosts (e.g. Godaddy) are notorious for this. – mti2935 Apr 11 '14 at 16:36
  • My host (Bluehost) was blocking this, but I found a solution. Posted below. – Kit Johnson Jul 05 '14 at 04:28
1

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.
Naser Sobhan
  • 33
  • 1
  • 10
Kit Johnson
  • 541
  • 2
  • 7
  • 15
1

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;       
Soravux
  • 9,653
  • 2
  • 27
  • 25
  • 1
    Welcome to Stack Overflow! When posting, check for the question date, it may be old and became irrelevant to the poster. If it is the case, make sure your answer can help other people with the same problem later on. Since your answer targets only a single element of the code, you should only write the relevant lines in your answer. – Soravux Sep 15 '14 at 04:08