Error sending e-mail using Gmail SMTP

丶灬走出姿态 提交于 2019-12-31 03:46:31

问题


I'm trying to send e-mail using Gmail SMTP and PHPmailer.

The problem is that i have this Error:

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.

Here's the code I am using:

?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!";

?>

I've already searched the web but couldn't find the solution


回答1:


I used this & it worked in java PHP prop as well you can set the same way.

    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:


I replaced

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

with just

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

and it worked !!!

Thanks everybody.



来源:https://stackoverflow.com/questions/16044304/error-sending-e-mail-using-gmail-smtp

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