PHP联系表单没有返回错误,但我仍然没有收到任何电子邮件

时间:2014-05-29 01:02:30

标签: php html contact-form

请原谅我这是一个愚蠢的问题,但我对PHP很新,我遇到了一些问题。我正在尝试使用HTML,CSS和PHP构建联系表单,但我似乎无法将我的PHP表单发送到我的电子邮件地址。这就是HTML的代码:

<div id="contact-form">
    <ul>
            <li><button id="quote" class="button1">Project Quote</button></li>
    </ul>

    <form class= "emai" action="mailer.php" method="post">
                    <p>Have a project in mind? Fill in the form for a quote!</p>
            <div>
                    <p><label for="name">What can I call you? <span>*</span></label></p>
                <input type="text" id="name" name="name">
            </div>
            <div>
                        <p><label for="email">What is your email? <span>*</span></label></p>
                <input type="email" name="email" id="email">
            </div>
            <div>
                        <p><label for="type">Type of Project? <span>*</span></label></p>
                <select id="type" name="type">
                        <option value="logo">Logo Design</option>
                    <option value="web dev">Website/WebApp Dev</option>
                        <option value="other">Other</option>    
                </select>
            </div>
            <div>
                    <p><label for="purpose">What is the main purpose of your project? <span>*</span></label></p>
                  <textarea id="purpose" name="purpose"></textarea>
            </div>
            <div>
                    <p><label for="features">Any extra features?</label></p>
                  <textarea id="features" name="features"></textarea>
            </div>

            <input class="button1" type="submit" value="submit">
    </form>

</div>

在一个名为&#34; mailer.php&#34;的单独文档中。这就是代码的样子:

<?php
/* Set e-mail recipient */
$myemail = "christopher.kenrick@gmail.com";
$subject = "Project Request";

/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Enter your name");
$email = check_input($_POST['email']);
$type = check_input($_POST['type'], "Select a type of project");
$purpose = check_input($_POST['purpose'], "What is the purpose of your project?");

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* Let's prepare the message for the e-mail */
$message = "

Name: $name
E-mail: $email
Type: $type
Purpose: $purpose
Features: $features

Message:
$message

";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location: thanks.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>
<html>
<body>

<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>

</body>
</html>
<?php
exit();
}
?>

如果有帮助,我的网站会link。有人能告诉我它是做错了吗?

2 个答案:

答案 0 :(得分:1)

您的代码缺少正确的mail headers。 (并且最初有一个缺失的主题变量,您现在已添加)。

使用以下代码添加和修改您当前的mail()功能,否则邮件将直接发送到垃圾邮件,就像我的测试一样。

$headers = 'From: ' . $email . "\r\n";
mail($myemail, $subject, $message, $headers);

带条件陈述:

if(mail($myemail, $subject, $message, $headers)){ 
   echo "Success"; } else{ echo "There was a problem.";}

答案 1 :(得分:0)

运行sudo apt-get install sendmail后,我终于开始收到联系表单的内容。此解决方案适用于使用DigitalOcean作为主机的用户。