【问题标题】:PHP form e-mail doesn't sendPHP表单电子邮件不发送
【发布时间】:2013-11-12 19:46:06
【问题描述】:

我已经为我的单页投资组合的联系表单编写了一些 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>";
}
?>

【问题讨论】:

  • 我在你的 html 表单中看不到人类字段??
  • 对不起,我错过了它现在已添加到上面的代码中的表单底部
  • 我用你的&lt;label for="human"&gt;What is 2 + 2 ?&lt;/label&gt; 编辑了我的答案,但是我输入的原始答案与更改无关。现在试试我的代码,完全如图所示,表单/处理程序。 @BenHumphries

标签: php html email


【解决方案1】:

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

在您的 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>";
}
?>

【讨论】:

  • 谢谢你,现在我只需要尝试实现 ajax 和 jquery 来将消息发布到页面而不是新页面
猜你喜欢
  • 1970-01-01
  • 2014-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-04
  • 2016-03-11
  • 2018-01-26
相关资源
最近更新 更多