PHP Mailer无法在php脚本中运行

时间:2018-05-28 12:46:57

标签: php phpmailer

我正在通过邮件链接获取应用程序的重置密码。当我尝试在没有php邮件时给出按钮正在工作,当我使用php邮件时按钮没有显示任何状态。当我提交没有phpmailer我获得状态成功和邮件实际上不去,当我使用PHP邮件时,我没有得到任何消息状态。这是我的PHP代码:

    session_start();
    require_once "function.php";
        use PHPMailer\PHPMailer\PHPMailer; 
        require_once "PHPMailer/PHPMailer.php";
        require_once "PHPMailer/Exception.php";

    if(isset($_POST['email'])) {

        $con = new mysqli('localhost','root','','demo');
        $email = $con->real_escape_string($_POST['email']);

        $sql = $con->query("SELECT id FROM users WHERE email = '$email'");


        if($sql->num_rows>0){
            $token = generateString();
            $con->query("UPDATE users SET token='$token' WHERE email='$email'");

            $mail = new  PHPMailer\PHPMailer\PHPMailer;
                $mail->IsSMTP();
                $mail->SMTPDebug = 2;
                $mail->SMTPAuth = true;
                $mail->SMTPSecure = 'ssl';
                $mail->Host = 'smtp.gmail.com';
                $mail->Port = 465;
                $mail->Username = "xxx@gmail.com";
                $mail->Password = "xxx";
                $mail->SetFrom("xxx@gmail.com", "Employee Leave Management System");

                $id=$_SESSION['id'];

                $select_query = mysqli_query("SELECT * FROM users WHERE email = '$email'");
                $select_sql = mysqli_fetch_array($select_query);
                $name=$select_sql['username'];
                var_dump($name);

                $select_query1 = mysqli_query("SELECT id FROM users WHERE email='$email'");
                $select_sql1 = mysqli_fetch_array($select_query1);
                $email=$select_sql1['email'];
                    var_dump($email);

                $mail->Subject = "HIE ";
                $mail->Body = "HIEHIE.";
                $mail->To = $email;
                $mail->AddAddress($email);

                if($mail->Send())
                {
                 exit(json_encode(array("status" => 1, "msg"=>'Please check your email')));
                }


            }else{

            exit(json_encode(array("status" => 0, "msg"=>'Please check your input!!!')));
        }
    }

?>

html代码:

   <!DOCTYPE html>
<html>
<head>
    <title>Forgot Password</title>
    <link rel="stylesheet" href="css_f/bootstrap.css">
</head>
<body>

    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-6 col-md-offset-3 align="center" style="margin-top: 200px;">
                <input type="text" name="email" placeholder="email" id="email" class="input-field"><br><br>
                <button type="button" name="submit" class="btn btn-primary">Reset Password</button><br><br>
                <p id="response"></p>
            </div>
        </div>  
    </div>

</body>

<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script type="text/javascript">
    var email = $("#email");
    $(document).ready(function() {
        $('button').on('click',function(){
            if(email.val()!=""){
                email.css('border','1px solid green');

                $.ajax({

                    url: 'forgot_pwd.php',
                    method: 'POST',
                    dataType: 'JSON',
                    data: {
                        email: email.val()
                    },success:function(response){
                        console.log(response);
                        if(!response.success) {
                            $("#response").html(response.msg).css('color','red');
                        }else{
                            $("#response").html(response.msg).css('color','green');
                        }
                    }
                });
            }else{
                email.css('border','1px solid red');
            }
        });
    });
</script>
</html>

0 个答案:

没有答案