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 霍尔传感器接放大器的应用
  • ¥20 针对计算后数据做一致性检验可以用Bland Altman法吗
  • ¥15 win32如何自绘编辑框的背景图片(语言-c++|操作系统-windows)
  • ¥15 微信夜间被转走了1w对,当天手机剪切板里就出现了这个乱码,有铁子可以看看是啥吗可以
  • ¥50 跑通github上的代码 深度学习 pytorch
  • ¥50 求写,批处理调用分区助手分区脚本
  • ¥15 求购HI3519AV100开发板
  • ¥15 请问1553 RT怎么测试,没有BC有方法吗
  • ¥100 业务编程如何选择学习方向和内容?
  • ¥15 wamp3.3.5安装完成后图标正常显示绿色,鼠标左右键点击图标均无反应。求解决方法。