1
$email_message = "Form details below.\n\n";

function clean_string($string) {
    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad,"",$string);
}

$email_message .= "Full Name: ".clean_string($_POST['full_name'])."\n";
$email_message .= "Email: ".clean_string($_POST['email'])."\n";
$email_message .= "Telephone number: ".clean_string($_POST['telephone'])."\n";
$email_message .= "Message: ".clean_string($_POST['comments'])."\n";


$mail             = new PHPMailer();
$body             = $email_message;
$body             = str_replace('\\', '', $body);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth   = true;             // enable SMTP authentication
$mail->SMTPSecure = "ssl";            // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port       = 465;              // set the SMTP port for the GMAIL server
$mail->Username   = "xxx@gmail.com";  // GMAIL username
$mail->Password   = "XXXXXX";         // GMAIL password

$mail->SetFrom('from-email@domain.com', 'First Last');

$mail->Subject    = "Imperia";

$mail->AltBody    =                   // optional, comment out and test
    "To view the message, please use an HTML compatible email viewer!";

$mail->MsgHTML($body);

$address = "xxx@gmail.com";
$mail->AddAddress($address, "To Name");

if(!$mail->Send()) {

    echo "<font color='red'> Message error </font>". $mail->ErrorInfo;
} else {
    echo "<font color='red'> Message sent </font>";
} 

I am using this code to try to send email using SMTP, but I have an error:

SMTP Error: Could not connect to SMTP host

Could anyone tell me what is wrong here? I can't find a way to fix this. Thanks

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
fish40
  • 5,738
  • 17
  • 50
  • 69

8 Answers8

4

I ran into this as well, and daemoni's solution worked for me. More specifically, I changed my code to this:

$mail->Host = gethostbyname("smtp.gmail.com"); 
2
$mailer->Host = 'ssl://smtp.gmail.com:465';
khaled_webdev
  • 1,420
  • 15
  • 20
  • 1
    this works if your server does not support ipv6 communication. you can check the same by telnet smtp.gmail.com 587 If it gives you anything other than 220, do the above to connect to the SMTP server via ipv4. – Alok Rajasukumaran Sep 25 '20 at 17:32
2

I had a similar issue yesterday, Turns out that smtp.gmail.com was resolving to the IPv6 address, then everything was more or less broken from there on.

My workaround was to put in the IPv4 address instead of smtp.gmail.com

(In my case 173.194.79.108, I'm not sure if that's global, so maybe ping to get the IP first)

IT's a temporary solution, but will probably work for a long time.

daemonl
  • 477
  • 5
  • 7
  • This is what I ended up doing too. how long did your ip last for it? – oneadvent Jan 21 '13 at 21:07
  • Thanks for the reminder -- my 'temporary solution' is still in live code. Luckily, the IP address is still the same, it might be safeish to keep it that way. – daemonl Jan 29 '13 at 03:12
  • I configured my network settings to use Google's DNS servers (8.8.8.8,8.8.4.4) which return IPv4 addresses instead of my ISP's IPv6 for smtp.gmail.com. And that works. So, the sollution for me was 'switch to different dns server'. – Victor.Palyvoda Nov 04 '14 at 17:07
1

"Could not connect to SMTP host" tells you you don't have a connection from your source server to the server you're targetting to, in this case smtp.gmail.com.

Possible reasons are

  • Firewall blocking
  • PHP not allowed to make such connections

Please test with, for example, telnet if you can connect to smtp.gmail.com at port 465 at all (from the server hosting your PHP code).

Also, I assume you have enabled required extensions.

Community
  • 1
  • 1
eis
  • 51,991
  • 13
  • 150
  • 199
1

Had a similar error and had to turn on "Access for less secure apps" feature in the gmail account.

You can find this in the account settings.

xerocool
  • 13
  • 3
0

Check these things

  1. Check the php.ini line extension=php_openssl.dll
  2. Turn off your firewall. It might be blocking the ssl connection.
0

Check the php.ini line extension=php_openssl.dll and turn off your firewall. It might be blocking the ssl connection.

Lendl Leyba
  • 2,287
  • 3
  • 34
  • 49
0

Disable SMTP Restrictions from whm and try

Mohammed Navab
  • 131
  • 1
  • 2