【问题标题】:Error sending e-mail using Gmail SMTP使用 Gmail SMTP 发送电子邮件时出错
【发布时间】:2013-04-16 18:20:00
【问题描述】:

我正在尝试使用 Gmail SMTP 和 PHPmailer 发送电子邮件。

问题是我有这个错误:

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   = "XXXX@gmail.com";  // GMAIL username
    $mail->Password   = "XXXXXX";            // GMAIL password
    $mail->SMTPDebug = 1;
    //End Gmail

    $mail->From       = "gvakh10@gmail.com";
    $mail->FromName   = "you name";
    $mail->Subject    = "some subject";
    $mail->MsgHTML("the message");

    //$mail->AddReplyTo("reply@email.com","reply name");//they answer here, optional
    $mail->AddAddress("XXXX@gmail.com","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!";

?>

我已经在网上搜索过,但找不到解决方案

【问题讨论】:

  • 我遇到了同样的问题,无法解决:/
  • 我想说Host 应该设置为“smtp.gmail.com”而不是“ssl://smtp.gmail.com:465”。错误消息意味着无法将主机名解析为 IP 地址。
  • 你启用了 openssl 扩展吗?
  • @leftclickben 你的回答真的帮了我很多,非常感谢。
  • @Sabari 是的,我已经启用了 openssl 分机。

标签: php smtp gmail phpmailer


【解决方案1】:

我用过这个,它在java中工作 PHP prop 也可以这样设置。

    String to = "shafi0907@gmail.com";
    //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("shafi0907@gmail.com", "<your password");//change accordingly
        }
    });

【讨论】:

    【解决方案2】:

    我换了

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

    只有

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

    成功了!!!

    谢谢大家。

    【讨论】:

      猜你喜欢
      • 2014-01-04
      • 2016-07-18
      • 2021-06-17
      • 2016-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多