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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵