2013-04-16 194 views
1

我正在嘗試使用Gmail SMTP和PHPmailer發送電子郵件。使用Gmail SMTP發送電子郵件時出錯

的問題是,我有這樣的錯誤:

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

SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host. 

下面是我使用的代碼:我已經在網上搜索,但沒有找到解決

?php 

    require_once($_SERVER['DOCUMENT_ROOT'].'/FreeUni/Kiosk/class.phpmailer.php'); 

    $mail = new PHPMailer(); 
    $mail->IsSMTP(); 

    //GMAIL config 
    $mail->SMTPAuth = true;     // enable SMTP authentication 
    $mail->SMTPSecure = "ssl";     // sets the prefix to the server 
    $mail->Host  = "ssl://smtp.gmail.com:465";  // sets GMAIL as the SMTP server 

    $mail->Username = "[email protected]"; // GMAIL username 
    $mail->Password = "XXXXXX";   // GMAIL password 
    $mail->SMTPDebug = 1; 
    //End Gmail 

    $mail->From  = "[email protected]"; 
    $mail->FromName = "you name"; 
    $mail->Subject = "some subject"; 
    $mail->MsgHTML("the message"); 

    //$mail->AddReplyTo("[email protected]","reply name");//they answer here, optional 
    $mail->AddAddress("[email protected]","name to"); 
    $mail->IsHTML(true); // send as HTML 

    if(!$mail->Send()) {//to see if we return a message or a value bolean 
     echo "Mailer Error: " . $mail->ErrorInfo; 
    } else echo "Message sent!"; 

?> 

+0

我有同樣的問題,解決不了:/ – gkiko

+3

我會說'Host'應設置爲只是「smtp.gmail.com」,而不是「SSL://smtp.gmail.com :465" 。錯誤消息意味着主機名無法解析爲IP地址。 – leftclickben

+0

您是否啓用了openssl擴展? – Sabari

回答

0

我替換

$mail->Host = "ssl://smtp.gmail.com:465";

只有

$mail->Host ="smtp.gmail.com:465";

和它的工作!

謝謝大家。

+0

我使用了你的代碼,但是顯示了相同的錯誤 –

1

我用這個&它工作在java PHP的道具以及你可以設置相同的方式。

String to = "[email protected]"; 
    //change accordingly 

    //Get the session object 

    Properties props = new Properties(); 
    props.put("mail.smtp.host", "smtp.gmail.com"); 
    props.put("mail.smtp.socketFactory.port", "465"); 
    props.put("mail.smtp.socketFactory.class", 
      "javax.net.ssl.SSLSocketFactory"); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.port", "465"); 

    Session session = Session.getDefaultInstance(props, 
      new javax.mail.Authenticator() { 
     protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication("[email protected]", "<your password");//change accordingly 
     } 
    }); 
相關問題