简体   繁体   中英

PHPMailer: “Could not connect to SMTP host”

$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

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"); 

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.

"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 .

$mailer->Host = 'ssl://smtp.gmail.com:465';

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.

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.

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

从 whm 禁用 SMTP 限制并尝试

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM