PHP联系表格

时间:2014-12-08 05:51:58

标签: php html forms

我查看过很多页面,找到问题的答案。但我无法弄清楚为什么我的表格不起作用。 我尝试了预制示例中的不同版本的代码,但我无法使其工作。

这是我的表格上方的php

<?php
    $name = $_POST['name'];
    $from = $_POST['email'];
    $subject = $_POST['subject'];
    $message = $_POST['message']; 
    $to = 'me@email.com'; 

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if ($_POST['submit']) {
        if (mail ($to, $subject, $body, $email)) { 
            echo '<p style="color: #27ae60;">Your message has been sent!</p>';
        } else { 
            echo '<p style="color: #c0392b;">Something went wrong, go back and try again!</p>'; 
        }
    }
?>

这是我的表格

<form class="contact-form" method="post" action="contact.php">

    <label>Name</label>
    <input name="name" placeholder="Your Name">

    <label>Email</label>
    <input name="email" type="email" placeholder="Your Email">

    <label>Subject</label>
    <input name="subject" placeholder="Your Subject">

    <label>Message</label>
    <textarea class="contact-form-message" name="message" placeholder="Your Message"></textarea>

    <input id="submit" name="submit" type="submit" value="Send">

</form>

我在此示例中仅更改了我的电子邮件,其余部分相同。我正在使用php 5+支持的现代服务器上进行实时测试

基本上一切正常,但我没有收到电子邮件。 我无法找到如何使它工作悲伤。任何想法都会很酷:/

编辑:GOD DAMNIT IM A TOTAL IDIOT

Gmail垃圾邮件过滤器功能强大。

5 个答案:

答案 0 :(得分:1)

您尚未定义$from

这就是它不发送邮件的原因。

另外,请检查您的机器/服务器的SMTP设置。

SMTP ports可能未配置,这就是邮件未发送的原因。

答案 1 :(得分:0)

请尝试以下表单:

<form class="contact-form" method="post" action="contact.php">

    <label>Name</label>
    <input name="name" type="text" placeholder="Your Name">

    <label>Email</label>
    <input name="email" type="text" placeholder="Your Email">

    <label>Subject</label>
    <input name="subject" type="text" placeholder="Your Subject">

    <label>Message</label>
    <textarea class="contact-form-message" type="text" name="message" placeholder="Your Message"></textarea>

    <input id="submit" name="submit" type="submit" value="Send">

也请尝试遵循以下工作格式:

<?php
$to = "somebody@example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";

mail($to,$subject,$txt,$headers);
?>

你可以只替换&#34;中的值。 &#34;与后期功能。

编辑(这肯定会奏效):

 if (isset($_POST['submit'])) {
        if (mail ($to, $subject, $body, $email)) { 
            echo '<p style="color: #27ae60;">Your message has been sent!</p>';
        } else { 
            echo '<p style="color: #c0392b;">Something went wrong, go back and try again!</p>'; 
        }
    }

答案 2 :(得分:0)

邮件功能的第四个参数是标题。您可以在哪里设置邮件表单和其他人(http://php.net/manual/en/function.mail.php)。请设置,并检查SMTP配置为“编程学生”告诉你。

答案 3 :(得分:0)

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $message = $_POST['message']; 
    $to = 'to@test.com'; 
    $from = 'from@test.com'; 

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if (mail ($to, $subject, $body, $from)) { 
        echo '<p style="color: #27ae60;">Your message has been sent!</p>';
    } else { 
        echo '<p style="color: #c0392b;">Something went wrong, go back and try again!    </p>'; 
    }

?>

您错过了从变量中分配$

<form class="contact-form" method="post" action="contact.php">
    <label>Name</label>
    <input name="name" type="text" placeholder="Your Name">

    <label>Email</label>
    <input name="email" type="text" placeholder="Your Email">

    <label>Subject</label>
    <input name="subject" type="text" placeholder="Your Subject">

    <label>Message</label>
    <textarea class="contact-form-message" name="message" placeholder="Your Message">   </textarea>

    <input id="submit" name="submit" type="submit" value="Send">
</form>

确保php文件名为“contact.php”

答案 4 :(得分:0)

现在我的HTML就是这样 - &gt;

<body> Fields with * are mandatory. 
<form action="mail.php" method="POST">
<fieldset>
<legend>Contact Information</legend>
<p>Name*</p> <input type="text" name="name">
<p>Email*</p> <input type="text" name="email">
</fieldset>
<br />
<br />
<fieldset>
<legend>Other Information</legend>
<p>Website</p> <input type="text" name="website">

<p>Priority</p>
<select name="priority" size="1">
<option value="Low">Low</option>
<option value="Normal">Normal</option>
<option value="High">High</option>
</select>
<br />

<p>Type</p>
<select name="type" size="1">
<option value="update">Contact Us</option>
<option value="change">Information Change</option>
<option value="addition">Other</option>

</select>
<br />
</fieldset>
<br />
<fieldset>
<legend>Your Message</legend>
<p>Message*</p><textarea name="message" rows="8" cols="29"></textarea><br /><p>
<input type="submit" value="Send" class="but"> &nbsp;<input type="reset" value="Clear" class="but">
</fieldset>
</form>

</body>

现在PHP的主要内容就是这样 - &gt;

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['website'];
$priority = $_POST['priority'];
$type = $_POST['type'];
$message = $_POST['message'];
$formcontent=" From: $name \n Email: $email \n Website: $website \n Priority: $priority \n Type: $type \n Message: $message";
$recipient = "youremail@domain.com";
$subject = "Contact Form";
$mailheader = "$name submited the form.";
 if (filter_var("$email", FILTER_VALIDATE_EMAIL)) {
        if ($email === "" || $message === "" || $name === "") {
echo "ERROR! Email and message and Name are Mandatory. <a href='contact.html' style='text-decoration:none;color:#ff0099;'> Return Back</a>";
}
else { 
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!"); 
mail($email, "Name of Form Here", "Your Form has been submitted. Your problem will be noticed soon. This is a no reply mail address. Please dont reply back.  - WeBoosters India", "Thankyou") or die("Error!");
echo "Thank You!" . " -" . "<a href='index.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
}
 }
    else {
        echo "The email is not valid. Please write a proper email address. - <a href='contact.html' style='text-decoration:none;color:#ff0099;'> Return Back</a>";
    }
?>

希望你喜欢它!祝你好运。