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

时间:2017-07-22 17:12:49

标签: php html mysql sql

我的代码出现问题,我希望它在忘记密码时通过电子邮件向用户发送用户名。

它的作用:

  1. 从数据库中检索用户信息
  2. 将HTML电子邮件发送给用户的电子邮件
  3. 它没有做什么:

    1. 发送到电子邮件时显示用户的登录用户名。
    2. PHP代码如下页面" 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>
      

      这是发送给用户电子邮件的代码&#34; email_template.html&#34;

      <!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>
      

      一切都按预期进行,但用户名不会显示,而是#34; $ username&#34;显示在电子邮件中。

      有人请帮忙。提前谢谢。

3 个答案:

答案 0 :(得分:0)

file_get_contents()函数不解析内容。因此,在将内容输入$ message后,您必须添加此行:

$ message = str_replace('$ username',$ username,$ message);

我建议你用%USERNAME%之类的东西替换你的html中的$ username,你就这样使用它:

$ message = str_replace('%USERNAME%',$ username,$ message);

答案 1 :(得分:0)

首先,email_template.html.html文件,您需要将其更改为.php文件。

其次,您将文件内容变为变量,您希望$username变量在同一范围内如何?

可能的解决方案是:

获取文件内容,然后将$username文件内容中的.html替换为$username值。

$message = file_get_contents("email_template.html");
$message = str_replace('$username', $username, $message);

答案 2 :(得分:0)

您不能在HTML文档中使用PHP代码。 HTML文档中的任何PHP代码都将解析为字符串。

将'email_template.html'的文件扩展名更改为。腓:

email_template.php

请注意,现在您必须使用PHP echo来打印任何HTML。我建议阅读php的工作原理:http://www.php.net