echo json_encode($ response);包含邮件代码

时间:2017-12-30 07:27:03

标签: php

我的用于注册的php文件

<?php
    require("password.php");
    $connect = mysqli_connect("localhost", "root", "", "accounts");

    $first_name = $_POST["first_name"];
    $last_name = $_POST["last_name"];
    $email = $_POST["email"];
    $password = $_POST["password"];

     function registerUser() {
        global $connect, $first_name, $last_name, $email, $password;
        $passwordHash = password_hash($password, PASSWORD_DEFAULT);
        $statement = mysqli_prepare($connect, "INSERT INTO users (first_name, last_name, email, password, hash, active) VALUES (?, ?, ?, ?, 123, 0)");
        mysqli_stmt_bind_param($statement, "ssss", $first_name, $last_name, $email, $passwordHash);
        mysqli_stmt_execute($statement);
        mysqli_stmt_close($statement);

        // Send registration confirmation link (verify.php)
        $to      = $email;
        $subject = 'Account Verification ( Name.com )';
        $message_body = '
        Hello '.$first_name.',
        Thank you for signing up!
        Please click this link to activate your account:
        http://localhost/accounts/verify.php?email='.$email.'&hash='.'123';  
        mail( $to, $subject, $message_body );
    }

    function emailAvailable() {
        global $connect, $email;
        $statement = mysqli_prepare($connect, "SELECT * FROM users WHERE email = ?"); 
        mysqli_stmt_bind_param($statement, "s", $email);
        mysqli_stmt_execute($statement);
        mysqli_stmt_store_result($statement);
        $count = mysqli_stmt_num_rows($statement);
        mysqli_stmt_close($statement); 
        if ($count < 1){
            return true;            
        }else {
            return false; 
        }
    }

    $success = '';
    $response = array();
    $response["success"] = false;

    if (emailAvailable()){
        registerUser();
        mysqli_set_charset($con,"utf8");
        $response["success"] = true;
        }

    echo json_encode($response);
?>

添加代码后

    // Send registration confirmation link (verify.php)
    $to      = $email;
    $subject = 'Account Verification ( Name.com )';
    $message_body = '
    Hello '.$first_name.',
    Thank you for signing up!
    Please click this link to activate your account:
    http://localhost/accounts/verify.php?email='.$email.'&hash='.'123';  
    mail( $to, $subject, $message_body );

我的申请没有回应

    $response["success"] = true;
    }

    echo json_encode($response);

然而$ response [&#34; success&#34;] = false;仍然有效并输出错误值。

我正在寻找一种方法来实现邮件系统,使其不会影响$ response [&#34; success&#34;] = true;

非常感谢任何可以帮助您的人提供的指导!

1 个答案:

答案 0 :(得分:0)

您需要配置php.ini文件并注释以下代码

/*[mail function]
; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury
; SMTP = localhost
; smtp_port = 25
*/

/*; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = postmaster@localhost
*/ 

如果它在本地主机中不起作用。 您可以通过以下链接获取免费主机来实现它 https://www.000webhost.com

如果它不起作用,请评论我。