简单的联系表单无法发送

时间:2014-12-08 10:45:15

标签: php html contact-form

我有一个简单的联系表单,无法发送,我是PHP的新手,因此我很可能忘记了某些内容或代码错误。我知道围绕同一主题有很多问题,但我对我的代码不确定。

我收到错误消息:未定义索引:在第31行发送\ mail.php


HTML


<html>
<head>
<title>form</title>

</head>
<body>
  <h2>Contact Form</h2>
<form id="form_id" name="form_name" action="mail.php" method="post">

  <div>
<input type="text" name="name" id="name" placeholder="Name" required/>
  </div>
<div>
<input type="email" name="email" id="email" placeholder="Email" required/>
  </div>
<div>
<input type="number" name="tel" id="tel" placeholder="Phone" required/>
  </div>

<textarea name="message" type="text" id="message" rows="5" cols="30" placeholder="Message" required></textarea>
  </div>

<div>
<input type="submit" name="submit" value="submit" />
  </div>

</form>
</body>
</html>

PHP


<?php

$to = 'test@gmail.com';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$tel = $_POST['tel'];


$body = <<<EMAIL

This is a message for your website.

Name: $name
Email: $email
Tel: $tel
Message: $message

EMAIL;

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Bob <$to>, Bob <$to>' . "\r\n";
$headers .= 'From: Website <noreply@example.com>' . "\r\n";
$headers .= 'Cc: noreplt@example.com' . "\r\n";
$headers .= 'Bcc: noreply@example.com' . "\r\n";

if ($_POST['send']){
mail($to, $subject, $body, $header);
echo 'Message Sent.';
} else {

    die('Failed to Send');
} 


?>

1 个答案:

答案 0 :(得分:4)

使用mail($to, $subject, $body, $headers); 在这里你使用了$ header