简体   繁体   English

php邮件中的SMTP连接错误

[英]SMTP connection error in php mail

<?php
require '/etc/PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "my@gmail.com";
$mail->Password = "password";
$mail->SetFrom("from@gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("to@gmail.com");
if(!$mail->Send())
{
    echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
    echo "Message has been sent";
}
?>

This is my mail functionality code & it gives me the error 这是我的邮件功能代码,它给了我错误

SMTP ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: No address associated with hostname

I don't have extension=php_openssl.dll in my php.ini file but still I've included this and the same issue 我的php.ini文件中没有extension=php_openssl.dll ,但我仍然包含了这个问题和相同的问题

To avoid the problem of dynamic IP (every time i ping smtp.gmail.com I see a slight difference in the last 3digit chunk), you just have to use php built-in gethostbyname() to read the IP in real-time. 为了避免动态IP的问题(每次我对smtp.gmail.com ping时,我都会在最后3个数字块中看到微小的差异),您只需要使用php内置的gethostbyname()即可实时读取IP。

 $smtp_host_ip = gethostbyname('smtp.gmail.com');

Then use that as host. 然后将其用作主机。 Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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