SMTP connect() failed… while i am trying to send mail from my php file

人盡茶涼 提交于 2020-01-04 09:12:18

问题


i ve been trying to send mail from my php file, i got an error like this

**"SMTP ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: No such host is known. (0) SMTP connect() failed."**

i am in the deadline of my project, if anybody knows the solution or error what i have done over here, please share and help. i am sharing my code here.........

<?php
    require("C:/xampp/htdocs/conference/PHPMailer-master/class.phpmailer.php");
    require("C:/xampp/htdocs/conference/PHPMailer-master/class.smtp.php");

    $mail = new PHPMailer();

    $mail->IsSMTP();  // telling the class to use SMTP
    $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth   = true; // SMTP authentication
    $mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
    $mail->Host       = "smtp.gmail.com"; // SMTP server
    $mail->Port       = 465; // SMTP Port
    $mail->Username   = "my email address"; // SMTP account username
    $mail->Password   = "my password";        // SMTP account password

    $mail->SetFrom('my email address', 'xxxx'); // FROM
    $mail->AddReplyTo('my email address', 'xxxx'); // Reply TO

    $mail->AddAddress('someone email address', 'yyyy'); // recipient email

    $mail->Subject    = "First SMTP Message"; // email subject
    $mail->Body       = "Hi! \n\n This is my first e-mail sent through Google SMTP using PHPMailer.";

    if(!$mail->Send()) {
      echo 'Message was not sent.';
      echo 'Mailer error: ' . $mail->ErrorInfo;
    } else {
      echo 'Message has been sent.';
    }
?>

回答1:


I found a solution for this problem,

Check whether your PHP is using openSSL extension or not...!

  1. Edit your php.ini from your installed php folder
  2. Search for extension=php_openssl.dll
  3. The initial will look like this ;extension=php_openssl.dll
  4. Remove the ';' and it will look like this extension=php_openssl.dll
  5. If you can't find the extension=php_openssl.dll, add this line extension=php_openssl.dll.
  6. Then restart your Xampp or LAMP or APACHE server (depends upon which of these you're using).

Hope this method would solve your problem...




回答2:


There are double quotes in

$mail->Host       = "smtp.gmail.com"; // SMTP server

Please remove the double quotes.

$mail->Host       = 'smtp.gmail.com'; // SMTP server


来源:https://stackoverflow.com/questions/19111887/smtp-connect-failed-while-i-am-trying-to-send-mail-from-my-php-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!