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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)