联系表格不发送

时间:2013-02-21 05:08:09

标签: php forms email contact

我正在尝试设置一个简单的联系表单。一切都是正确的样式,但是当我点击提交它不会带我到任何地方,只是尝试打开contact.php。我认为代码中缺少实际发送消息的东西。我确信这是一件相当简单的事情,我很想念,但这有点过头了。任何帮助表示赞赏。

<form action="mail.php" method="POST">
    <p>Name</p> <input type="text" name="name">
    <p>Company</p> <input type="text" name="company">
    <p>Email</p> <input type="text" name="email">
    <p>Phone</p> <input type="text" name="phone">
    <p>Message</p><textarea name="message" rows="4" cols="25"></textarea><br />
    <input type="submit" value="Submit">
    </form>

<?php

$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "______@gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";

$to ='______@gmail.com';
$send_contact=mail($to,$subject,$message,$header);

// Check, if message sent to your email 
// display message "We've received your information"

if($send_contact){
echo "We've received your contact information";
}
else {
echo "ERROR";
}
?>
编辑:在添加mail.php的完整网址后,我终于收到了一封电子邮件...但是除了邮件之外没有包含任何信息。发件人被列为Apache ...如何确保表单中输入的信息将包含在电子邮件中?感谢迄今为止的所有帮助。

1 个答案:

答案 0 :(得分:0)

由于您在同一页面中指定代码,因此可以在表单中省略action。 此外,在运行脚本后,设置运行PHP的条件。

<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "______@gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";

$to ='______@gmail.com';
$send_contact=mail($to,$subject,$message,$header);

// Check, if message sent to your email 
// display message "We've received your information"

if($send_contact){
echo "We've received your contact information";
}
else {
echo "ERROR";
} }
?>
<form action = "<?= $_SERVER["PHP_SELF"]; ?>"method="POST">
    <p>Name</p> <input type="text" name="name">
    <p>Company</p> <input type="text" name="company">
    <p>Email</p> <input type="text" name="email">
    <p>Phone</p> <input type="text" name="phone">
    <p>Message</p><textarea name="message" rows="4" cols="25"></textarea><br />
    <input type="submit" value="Submit" name="submit">
    </form>

希望它有所帮助!

编辑:我不确定当前的文件名是什么,但要保持动态,我提到$_SERVER["PHP_SELF"]这不是推荐用法。 但是,试试吧! :)