将表格发送到电子邮件

Posted

技术标签:

【中文标题】将表格发送到电子邮件【英文标题】:Send form to email 【发布时间】:2020-08-11 01:28:36 【问题描述】:

大家好,我正在努力在 php 中选择 datepicker 的值,它总是说无法发送消息。我找不到问题。你们知道我是否应该在 PHP 中选择 jquery 函数。

我想要的是,一旦表格被填写,它会将所有已填写并选择的值发送到我的电子邮件。

<?php
$error = ""; $successMessage = "";   

if ($_SERVER['REQUEST_METHOD'] === "POST")       
 if(!$_POST['name']) 

   $error .= "<li>Please introduce a name.</li>";
 
 if (!$_POST['date']) 
   $error .= "<li>Please pick a date.</li>";

if (!$_POST['email'] && filter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false) 
   $error .= "<li>Invalid Email.</li>";  

if ($error != "") 
   $error = '<div class="alert alert-danger d-inline-block mt-0" role="alert">
<p>There were error(s) in your form:</p>' . $error . '</div>';
 else  

$mailTo = "me@domain.com"; 
$name = $_POST['name'];
$date = $_POST['date'];
$member = $_POST['member'];
$headers = "From: ".$_POST['email'];

 if (mail($mailTo, $name, $date, $member, $headers)) 
   $successMessage = '<div class="alert alert-success d-inline-block" role="alert">Your message was sent, we\'ll get back to you ASAP!</div>';
  else 
$error = '<div class="alert alert-danger pt-4 d-inline-block" role="alert"><p><strong>Your message couldn\'t be sent - please try again later</div>';
    
      

?>

html 代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!--Jquery-->
    <link rel="stylesheet" href="js/jquery-ui-1.12.1/jquery-ui.css">
    <link rel="stylesheet" href="js/jquery-ui-1.12.1/jquery-ui.structure.css">
    <link rel="stylesheet" href="js/jquery-ui-1.12.1/jquery-ui.theme.css">
    <!-- CSS -->
    <link rel="stylesheet" href="css/nav.css">
    <link rel="stylesheet" href="css/attendance.css">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

    <!-- Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;500&display=swap" rel="stylesheet">

    <title>YesCom - Home</title>
</head>
<body>
<div class="navigation fixed-top">
    <nav class="navbar navbar-light bg-light text-center px-3" id="header">
        <a class="navbar-brand text-center p-3 pl-4 lead" href="#"><span class="yes text-danger border-danger h3" id="nav-logo-yes">Yes</span><span id="nav-logo-com" class="com text-warning border-warning h3">Com</span></a>
      </nav>

      <div class="bor text-center"></div>

      <!-- Menu -->
      <nav class="navbar navbar-expand-lg navbar-light bg-light">
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon text"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
          <ul class="navbar-nav">
            <li class="nav-item active">
              <a class="nav-link" href="home.html">Home</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">About</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">Events</a>
            </li>
              <li class="nav-item">
                <a class="nav-link" href="#">Blog</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">Forum</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="attendance.php">Attendance</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" id="contact-m" href="php/index.php">Contact</a>
              </li>
          </ul>
        </div>
        <hr class="d-xl-none d-md-block d-sm-block text-center">
        <form class="form-inline pull-sm-right d-md-block text-center">
          <input class="form-control" type="email" placeholder="Email">
          <input class="form-control" type="password" placeholder="Password">
          <button class="btn btn-success my-2" id="login-btn" type="submit">Log in</button>
        </form>
      </nav>
    </div>

    <!-- Video header -->

        <div class="container-fluid text-center" id="attendform" style="top: 50%;transform: translateY(25%) !important;position: relative;">    
            <!--Form for join us-->
            <div class="container bg-light p-5 mt-5 d-inline-block text-center">
            <h2 class="join-title text-center display-4 mb-0">Join us!</h2><br><br>

              <div class="mt-0 mb-3" id="error"><? echo $error.$successMessage; ?></div>

                  <div class="container text-center">
                    <form class="" method="post">
                      <label for="name" class="text-dark border-top-0 border-right-0 border-left-0" >Name: </label> <br>
                      <input class="form-control" type="text" name="name" id="name" placeholder="Bro./Sis."><br>

                      <label for="email">Email</label>
                      <input type="email" name="email" class="form-control" id="email" placeholder="user@domain.com" required> <br>
                  <!---->
                        <label for="date" class="d-inline-block">Date: </label> 
                        <input type="text" id="datepicker" name="date" class="form-control"></input></p><br>

                      <label for="member">Are you already a member of the youth?</label><br>
                      <input type="radio" name="member" value="Yes">Yes
                      <input type="radio" name="member" value="No">No <br><br>

                      <input class="btn btn-dark" id="sendbtn" type="submit" value="Send">
                      </form>
                    </div>
                </div>
            </div>


                </form>
            </div>
        </div>
<div id="myDiv"></div>
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
    <!--script-->
    <script src="js/jquery-3.4.1.min.js"></script>
    <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js" integrity="sha256-0YPKAwZP7Mp3ALMRVB2i8GXeEndvCq3eSl/WsAl1Ryk=" crossorigin="anonymous"></script>
    <script type="text/javascript">
          $(function() 
               $("#datepicker").datepicker();
        );
      </script>
  </body>
</html>

【问题讨论】:

单独的 JS 无法发送电子邮件。它所能做的就是打开默认的电子邮件客户端。如果您想自动发送电子邮件,您需要向服务器端代码发出请求,以便从那里发送电子邮件 嗨,Rory,对不起,我忘了放 php 您在页面中添加了两次 jQuery。这是不必要的,也是一个坏主意 - 它可能会导致奇怪的冲突。 P.S. 我正在努力在 PHP 中选择 datepicker 的值,它总是说无法发送消息 ...目前尚不清楚这两个问题是否/如何直接相关。可能有many reasons 为什么您的电子邮件没有被发送。您需要调试并更清楚问题发生在哪里,以及这与您的日期选择器有何关系。 话虽如此,我可以看出您对mail() 的调用毫无意义。查看文档:php.net/manual/en/function.mail.php。您可以传递给邮件的参数是:to , subject , message, additional_headers additional_parameters。您正在发送$mailTo, $name, $date, $member, $headers。因此,在您的情况下,您使用 $date 作为“消息”值,并使用 $member 作为“additional_headers”值。你能明白为什么这可能是一个问题吗?您需要创建一个消息字符串,其中包含要放入消息正文的所有内容。 【参考方案1】:

您好,感谢您的支持。我设法调试,这是我的答案:

HTML 表单:

<div class="container-fluid text-center" id="attendform" style="top: 50%;transform: translateY(30%) !important;position: relative;">    
            <!--Form for join us-->
            <div class="container-fluid bg-light p-5 mt-5 d-inline-block text-center">
            <h2 class="join-title text-center display-4 mb-0">Join us!</h2><br><br>

              <div class="mt-0 mb-3" id="error"><? echo $error.$success; ?></div>

                  <div class="container text-center">
                    <form class="" method="post">
                      <label for="name" class="text-dark border-top-0 border-right-0 border-left-0" >Name: </label> <br>
                      <input class="form-control" type="text" name="name" id="name" placeholder="Bro./Sis."><br>

                      <label for="email">Email</label>
                      <input type="email" name="email" class="form-control" id="email" placeholder="user@domain.com" required> <br>
                  <!---->
                      <p class="font-weight-bold">Week: </p>
                      <select name="week">
                        <option  id="week" value="1st week">1st week</option>
                        <option  id="week" value="2nd week">2nd week</option>
                        <option  id="week" value="3rd week">3rd week</option>
                        <option  id="week" value="4th week">4th week</option>
                        <option  id="week" value="5th week">5th week</option> 
                      </select><br> <br>

                      <label for="days" class="font-weight-bold">Day: </label> <br>
                        <input type="checkbox" name="day" id="day" value="Saturday">Saturday
                        <input type="checkbox" name="day" id="day" value="Sunday">Sunday
                        <input type="checkbox" name="day" id="day" value="Both">Both
                        <input type="checkbox" name="day" id="day" value="None">None <br><br>


                      <p>Are you already a member of the youth?</p>
                      <label for=member>
                      <input type="radio" name="member" value="Yes member">Yes
                      <input type="radio" name="member" value="Non member">No 
                      </select><br><br>
                      <input class="btn btn-dark" id="sendbtn" type="submit" name="send" value="Send">
                      </form>
                    </div>
                </div>
            </div>

PHP 代码:

<?php


if (isset($_POST['send']))       
  $name = $_POST['name'];
  $email = $_POST['email'];
  $subject = "Attendance Form";
  $date = $_POST['date'];
  $week = $_POST['week'];
  $day = $_POST['day'];
  $member = $_POST['member'];
  $message = "Bro./Sis. ".$name." "."($member)"."\n\n"."Schedule:"."\n\n"."Time of the week: ".$week."\n"."Day: ".$day;


  $emailTo = "me@domain.com";
  $mailHeaders = "From: " . $name . "<". $email .">\r\n";
  if (mail($emailTo, $subject, $message, $mailHeaders)) 
    $success ='<div class="alert alert-success d-inline-block" role="alert">Your message has been sent.</div>';
    $type = "success";
   else 
    $error = '<div class="alert alert-danger d-inline-block" role="alert">Something went wrong, please try again.</div>';
  


?>

【讨论】:

以上是关于将表格发送到电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

访问 VBA 以表格格式将查询结果发送到 Outlook 电子邮件

Apps 脚本允许用户将数据发送到他们无法查看的电子表格?

有没有办法查看联系表格将其内容发送到哪个电子邮件地址

Swift 将表单作为 PDF 发送到电子邮件

Google 表单:将数据发送到电子表格

如何通过 Automic 将 SQL 脚本结果作为电子邮件正文中的表格发送