dsuoedtom207012191 2018-01-25 03:26
浏览 24
已采纳

php变量不会显示在电子邮件中

The assignment I'm working on (an E-commerce course) asks that I use php to generate a new password for a user and send the user an email with the new password. I successfully generate the password, send the email from my school's server to myself (a gmail account), using php mail() however the php variable representing the password is always blank. I have been looking for answers to this on here and other websites but cannot find what I'm doing wrong. I am looking to solve this particular issue and am not looking to use PHPMailer or some other alternative. Also I am not looking to discuss more secure ways to send email, or discuss encryption, just looking to discuss this particular issue and why it is or isn't working. Thank you in advance for any advice.

if ($mysqli->conect_errno) {
        die("Error: Could not connect to database." . $mysqli->connect_error);
    } else {
        echo "<p>Connected<br></p>";
    }

    $email = $_POST['email_input'];
    try {
        $password = reset_password($email, $mysqli);
        notify_password($email, $password, $mysqli);
        echo 'Your password has changed and has been emailed to you.<br>';
    }
    catch(Exception $e) {
        echo 'Your password could not be reset';
    }

    function reset_password($email, $mysqli){
        $new_password = randomString(8, 12);
        if ($new_password == false) {
            throw new Exception('could not generate new password');
        }
        $rand_number = rand(0, 999);
        $new_password .= $rand_number;
        echo "NEW PASSWORD: " .$new_password."
";

        $query = "UPDATE registration 
        SET password = sha1('".$new_password."')
        WHERE email = '".$email."'";

        $result = $mysqli->query($query);
        if($result) {
            echo "<br>Password Reset<br>";
        }else {
            echo "An error has occured";
        }

    }
    function randomString($min_length, $max_length){
        return substr(str_shuffle("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), $min_length, $max_length);       
    }

    function notify_password($email, $password, $mysqli){
        $query = "SELECT email FROM registration WHERE email='".$email."'";
        $result = $mysqli->query($query);
        if(!$result){
            throw new Exception('could not find email address');
        }else if ($result->num_rows == 0) {
            throw new Exception('Could not find email address:user not in database');    
        }else {
            $row = $result->fetch_object();
            $email = $row->email;
            $from = "From support@HelpFinder 
";
            $mesg = "Your password has been changed to ".$password."
"."Please change it the next time you log in.
";

            if(mail($email, 'HelpFinder Login Information', $mesg, $from)) {
                return true;
            }else {
                throw new Exception('Could not send email.');
            }
        }

    }

the email message that arrives

example from text book I'm learning from

  • 写回答

2条回答 默认 最新

  • duanmangxie7131 2018-01-25 07:10
    关注

    check if you are sending the $password variable as parameter correctly, may be its empty

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 QTableWidget重绘程序崩溃
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含