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 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题