dspld86684 2013-11-12 19:46
浏览 22
已采纳

PHP表单电子邮件不发送

I have written some PHP code for a contact form for my one page portfolio, when it is submitted it just opens up a blank page and also the e-mail isnt sent and the text is not echo'd out. I am not very confident with PHP as it is fairly new to me. if anyone could help that would be great?

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
 E-Mail: $email
 Message:
 $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条回答 默认 最新

  • doume5227 2013-11-12 20:35
    关注

    There were a few things wrong with your form and handler.

    In your HTML form, you did not have an input for human so I added that, plus your submit button was not named, so that alone would not have worked.

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

    And in your PHP handler, your (mail headers) were improperly formatted, so that ended up in my SPAM box so I added that as well.

    $from = $_POST['email'];
    

    as well as headers plus I fixed your last condition to:

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

    HTML form

    <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 handler, tested and working for you.

    <?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
     E-Mail: $email
     Message:
     $message";
    
    $headers = "From: $email" . "
    " .
    "Reply-To: $email" . "
    " .
    "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>";
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?