douhu8851 2017-07-22 17:12
浏览 78
已采纳

使用PHP从HTML电子邮件中的数据库发送数据

i'm having trouble with my code, i want it to send a user their username via email when they have forgotten it.

What it does:

  1. Retrieves user information from database
  2. Send HTML Email to User's email

What it doesn't do:

  1. Display the user's login username when sent to the email.

The PHP code is below for the page "forgot-username.php"

<?php
require_once('../../Connections/localhost.php');
require('../../PHPMailer/PHPMailerAutoload.php'); 
if(isset($_POST) & !empty($_POST)){
$username = mysql_real_escape_string($_POST['username'], $localhost);
$sql = "SELECT * FROM database.users WHERE users.email = '$email'";
$res = mysql_query($sql, $localhost) or die(mysql_error());
$count = mysql_num_rows($res);
if($count == 1){
    $r = mysql_fetch_assoc($res);
    $username = $r['username'];
    $to = $r['email'];
    $subject = "Your Recovered Username";
    $message = file_get_contents("email_template.html");
    $headers = 'From: Your name <info@address.com>';
    if(mail($to,$subject,$message,$headers)){
        header('Location: username-sent.html');
    }else{
        echo "Failed to Recover your email, try again";
    }

}else{
    echo "email does not exist in database";
}
}

?>

<!DOCTYPE html>
<!--[if IE 8 ]><html class="no-js oldie ie8" lang="en"> <![endif]-->
<!--[if IE 9 ]><html class="no-js oldie ie9" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!-->
<html class="no-js" lang="en"> 
<!--<![endif]-->

<head>
<!-- ====== Basic Page Needs ====== -->
<meta charset="utf-8">
<title>Forgot Username</title>
<!-- ====== Mobile Specs Meta ====== -->
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<!-- ====== CSS ====== -->
<link rel="stylesheet" href="../../css/base.css">
<link rel="stylesheet" href="../../css/vendor.css">  
<link rel="stylesheet" href="../../css/main.css">
<link rel="stylesheet" href="../../css/zerogrid.css">  
<!-- ====== Java Scripts ====== -->
<script src="../../js/modernizr.js"></script>
<script src="../../js/pace.min.js"></script>
<!-- ====== Favicon ====== -->
<link rel="shortcut icon" href="../../favicon.ico" type="image/x-icon">
<link rel="icon" href="../../favicon.ico" type="image/x-icon">
</head>

<body id="top">
<!-- ====== Header Begin ====== -->
<header> 
<div class="header-logo">
      <a href="#">Logo</a>
    </div> 
    <a id="header-menu-trigger" href="#0">
        <span class="header-menu-text" style="color: white">Menu</span>
        <span class="header-menu-icon"></span>
    </a> 
    <nav id="menu-nav-wrap">
        <a href="#0" class="close-button" title="close"><span>Close</span></a>  
        <img alt="KBG" src="../../images/logo.png" style="height: 100px;width: 100px;" />  
        <ul class="nav-list">
            <li class="current"><a class="" href="../../index.html">Home</a></li>
            <li><a class="" href="../../about.html" title="">About</a></li>
            <li>
                <a class="" >Services</a>
            <div>
           <ul>
            <li><a href="../../Service1.html">Service 1</a></li>
            <li><a href="../../Service2.html">Service 2</a></li>
            <li><a href="../../Service3.html">Service 3</a></li>
            <li><a href="../../Service4.html">Service 4</a></li>
           </ul>
         </div>
           </li>
            <li><a class="" href="../../Service5.html" >Service 5</a></li>
            <li><a class="" href="../../competition.html" >Competition</a></li>
            <li><a class="" href="../../login.php" >Sign In</a></li>
            <li><a class="" href="../../signup.html" >Sign Up</a></li>
            <li><a href="../../contact-us.html">Contact Us</a></li>                     
        </ul>   
    </nav>  <!-- end #menu-nav-wrap -->
</header>     
<section id="services">
    <div class="overlay"></div>
    <div class="zerogrid">
        <div class="row">
            <!--Start Box-->
            <div class="col-1-3 offset-1-3">
                <div class="wrap-col">
                    <div class="row">
                        <div class="animate-this">
                            <h1 style="color: white">Forgot username?</h1>
                            <p class="lead" style="color: white">Fill in your email below and we will send you your username.</p>
                             <?php if(isset($smsg)){ ?><div class="alert alert-success" role="alert"> <?php echo $smsg; ?> </div><?php } ?>
                             <?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?>      
                            <form method="post">
                                <div class="form-field">
                                    <input name="email" style="color: white" class="full-width" type="text" id="email" placeholder="Email" value="" minlength="5" required>
                                </div>
                                <div class="form-field">
                                    <input type="submit" class="submitform full-width" style="background-color: white; color: black" value="Send" name="forgotusername">
                                </div>
                                <a href="login.html" style="color: white">Go Back</a>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
<footer>
    <div class="footer-bottom">
        <div class="row">
            <div class="col-full">
                <div class="copyright">
                    <span>© Copyright Company 2017.</span>
                </div>
            </div>
        </div>
    </div>
    <div id="go-top">
        <a class="smoothscroll" title="Back to Top" href="#top">
            <i class="fa fa-long-arrow-up" aria-hidden="true"></i>
        </a>
    </div>
</footer>
<div id="preloader"> 
    <div id="loader"></div>
</div> 

<!-- ====== Java Scripts ====== --> 

<script src="../../js/jquery-2.1.3.min.js"></script>
<script src="../../js/plugins.js"></script>
<script src="../../js/main.js"></script>
<script src="../../js/cookiechoices.js"></script>
<script>
    cookieChoices.showCookieBar({
    linkHref: '../../cookie-policy.html',
    language: 'en'
    });
</script>

</body>
</html>

this is the code that is sent to the users email "email_template.html"

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="shortcut icon" href="https://www.website.com/favicon.png" type="image/x-icon">
<link rel="icon" href="https://www.website.com/favicon.png" type="image/x-icon">
<link rel="stylesheet" href="https://www.website.com/css/base.css">
<link rel="stylesheet" href="https://www.website.com/css/vendor.css">
<link rel="stylesheet" href="https://www.website.com/css/main.css">
<link rel="stylesheet" href="https://www.website.com/css/zerogrid.css"> 
</head>
<body style="max-width: 100%">
<section style="background-color: black; text-align: center; height: 20%; min-height: 5%"><img src="https://www.website.com/images/logo.png" alt="logo" style="max-height: 150px; max-width: 150px; margin-top: 1%" /></section>
<section id="services">
    <div class="overlay"></div>
    <div class="zerogrid">
        <div class="row">
            <!--Start Box-->
            <div class="col-1-3 offset-1-3">
                <div class="wrap-col">
                    <div class="row">
                        <div class="animate-this">
                            <h1 style="color: white">Forgot Your Username?</h1>
                            <p class="lead" style="color: white">Not a problem, we've searched our database for your username.</p>
                            <p class="lead" style="color: white">Your username is:</p>
                                <div class="form-field">
                                    <input type="button" class="full-width" style="background-color: white; color: black" value="$username">
                                </div>
                            <br />
                            <br />
                            <p class="lead" style="color: white">Regards,<br />Support Team</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
</body>
</html>

Everything does as it should but the username doesn't show up, instead "$username" shows up in the email.

Someone please help. Thanks in advance.

  • 写回答

3条回答 默认 最新

  • dounianji7883 2017-07-22 17:25
    关注

    First of all, email_template.html is an .html file, you need to change it to .php file.

    Second, you are getting the file contents to variable how would you expect $username variable to be in same scope ?

    Probable solution would be :

    Get the file content and then replace the $username in your .html file content with $username value.

    $message = file_get_contents("email_template.html");
    $message = str_replace('$username', $username, $message);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀