PHP表单电子邮件不发送

时间:2013-11-12 19:46:06

标签: php html5 email

我已经为我的单页组合编写了一些联系表单的PHP代码,当它被提交时,它只是打开一个空白页面,而且电子邮件没有发送,文本也没有回显。我对PHP不太自信,因为它对我来说还是个新手。如果有人能帮忙那会很棒吗?

html -

<form id="cont-form" method="post" action="mail.php">
<fieldset>
<legend>Send me a message</legend>
<ol>
        <li>
            <label for="name">Name</label>
            <input id="name" name="name" type="text" placeholder="First and last name" required autofocus>
        </li>
        <li>
            <label for="email">Email</label>
            <input id="email" name="email" type="email" placeholder="example@domain.com" required>
        </li>
        <li>
            <label for="phone">Phone</label>
            <input id="phone" name="phone" type="tel" placeholder="Eg. 07500000000" required>
        </li>
        <li>
            <label for="subject">Subject</label>
            <input id="subject" name="subject" type="text" placeholder="What the message is about" required>
        </li>
        <li>
        <label for="message">Message</label>
        <textarea id="message" name="message" placeholder="Insert your message or question here" rows="10" cols="50">
        </textarea>
        </li>
          <li>
        <label for="human">What is 2 + 2 ?</label>
        <input id="human" name="human" type="number" placeholder="Please insert answer" required>
        </li>
    </ol>
</fieldset>
<fieldset>
    <input class="button" id="submit" type="submit" value="Send it!">
</fieldset>
</form>

php -

<?php
$name = $_POST['name']; //'name' has to be the same as the name value on the form input element
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: $email'; 
$to = 'ben_humphries@hotmail.co.uk'; //set to the default email address
$subject = $_POST['subject'];
$human = $POST['human'];    

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

if ($_POST['submit'] && $human == '4') {                 
    if (mail ($to, $subject, $body, $from)) { //mail sends it to the SMTP server side which sends the email
    echo "<p>Your message has been sent!</p>";
} else { 
    echo "<p>Something went wrong, go back and try again!</p>"; 
} 
} else if ($_POST['submit'] && $human != '4') {
echo "<p>You answered the anti-spam question incorrectly!</p>";
}
?>

1 个答案:

答案 0 :(得分:0)

您的表单和处理程序有一些问题。

在您的HTML表单中,您没有human的输入,所以我补充说,加上您的submit按钮没有被命名,因此单独就行不了。

<input class="button" id="submit" name="submit" type="submit" value="Send it!">

在您的PHP处理程序中,您的(mail headers)格式不正确,因此最终出现在我的垃圾邮件框中,所以我也添加了它。

$from = $_POST['email'];

以及标题加上我将您的上一个条件修复为:

if (!isset($_POST['submit']) && ($_POST['human']) != '4')

HTML表单

<form id="cont-form" method="post" action="mail.php">
<fieldset>
<legend>Send me a message</legend>
<ol>
    <li>
        <label for="name">Name</label>
        <input id="name" name="name" type="text" placeholder="First and last name" required autofocus>
    </li>
    <li>
        <label for="email">Email</label>
        <input id="email" name="email" type="email" placeholder="example@domain.com" required>
    </li>
    <li>
        <label for="phone">Phone</label>
        <input id="phone" name="phone" type="tel" placeholder="Eg. 07500000000" required>
    </li>
    <li>
        <label for="subject">Subject</label>
        <input id="subject" name="subject" type="text" placeholder="What the message is about" required>
    </li>


      <li>
    <label for="human">What is 2 + 2 ?</label>
    <input id="human" name="human" type="number" placeholder="Please insert answer" required>
    </li>

    <li>
    <label for="message">Message</label>
    <textarea id="message" name="message" placeholder="Insert your message or question here" rows="10" cols="50">
    </textarea>
    </li>
    </ol>
</fieldset>
<fieldset>
    <input class="button" id="submit" name="submit" type="submit" value="Send it!">
</fieldset>
</form>

PHP处理程序,经过测试并为您工作。

<?php
$name = $_POST['name']; //'name' has to be the same as the name value on the form input element
$email = $_POST['email'];
$message = $_POST['message'];
$human = $_POST['human'];
$from = $_POST['email'];
$to = 'ben_humphries@hotmail.co.uk'; //set to the default email address
$subject = $_POST['subject'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";

$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();

if(isset($_POST['submit']) && ($_POST['human']) == '4') {               
mail ($to, $subject, $body, $headers);  //mail sends it to the SMTP server side which sends the email
    echo "<p>Your message has been sent!</p>";
} 

else { 
    echo "<p>Something went wrong, go back and try again!</p>"; 
} 
if (!isset($_POST['submit']) && ($_POST['human']) != '4') {
echo "<p>You answered the anti-spam question incorrectly!</p>";
}
?>