发送联系表数据到电子邮件

时间:2019-05-20 08:24:58

标签: php email contact-form

我的目标是制作一个联系表,用户可以在其中输入姓名,电子邮件和消息。当他们按下“发送”的那一刻,我作为管理员将收到一封电子邮件,其中包含以前输入的所有信息。现在,电子邮件已发送,但电子邮件为空,例如:

  

回复:
  用户名:
  用户电子邮件
  用户留言

此外,当我单击“发送消息”时,它具有相同的形式,但没有CSS,肯定有一个代码错误,我不知道它是什么。

// Form contactos.php
<div class="col-md-7 mb-5 site-animate">
  <form action="index.php?cmd=contform" method="post">
    <div class="form-group">
      <label for="name" class="sr-only">Name</label>
      <input type="text" class="form-control" id="NomeM" placeholder="Nome">
    </div>
    <div class="form-group">
      <label for="email" class="sr-only">Email</label>
      <input type="text" class="form-control" id="EmailMen" placeholder="Email">
    </div>
    <div class="form-group">
      <label for="message" class="sr-only">Messagem</label>
      <textarea name="message" id="Mensagem" cols="30" rows="10" class="form-control" placeholder="Escreva a sua mensagem"></textarea>
    </div>
    <div class="form-group">
      <input type="submit" name="submit" class="btn btn-primary btn-lg" value="Enviar Mensagem">
    </div>
  </form>
</div>
//File contform.php

<?php
$sql="select * from Mensagem ";
$res=$lig->query($sql);
$lin = $res->fetch_array();


$NomeM = $_POST['NomeM'];
$Email = $_POST['EmailMen'];
$Mensagem = $_POST['Mensagem'];


$email_from = 'admin03@happygreen.pt';

$email_subject = "New Form Submission";

$email_body = "User Name:" .$lin['NomeM']."\n".
"User Email:".$lin['EmailMen']."\n".
"User Message:".$lin['Mensagem']."\n";


$to = "filipajoao1933@gmail.com";

$headers = "De: $email_from \r\n";

$headers .= "Responda a: ".$lin['EmailMen']."\r\n";

mail($to,$email_subject,$email_body,$headers);

header("Location: Contactos/contactos.php");

?>

1 个答案:

答案 0 :(得分:0)

表单字段(inputselecttextarea)需要一个name属性,您可以在PHP中引用该属性,您只有ID。另外,for中的label属性引用了表单字段的id

// Form contactos.php
<div class="col-md-7 mb-5 site-animate">
    <form action="index.php?cmd=contform" method="post">
      <div class="form-group">
        <label for="NomeM" class="sr-only">Name</label>
        <input type="text" class="form-control" name="NomeM" id="NomeM" placeholder="Nome">
      </div>
      <div class="form-group">
        <label for="EmailMen" class="sr-only">Email</label>
        <input type="text" class="form-control" id="EmailMen" name="EmailMen" placeholder="Email">
      </div>
      <div class="form-group">
        <label for="Mensagem" class="sr-only">Messagem</label>
        <textarea name="Mensagem" id="Mensagem" cols="30" rows="10" class="form-control" placeholder="Escreva a sua mensagem"></textarea>
      </div>
      <div class="form-group">
        <input type="submit" name="submit" class="btn btn-primary btn-lg" value="Enviar Mensagem">
      </div>
  </form>
</div>

您的$email_body应该是:

$email_body = "User Name:" .$NomeM."\n".
"User Email:".$EmailMen."\n".
"User Message:".$Mensagem."\n";