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

问题描述:

Forgive me if this is a stupid question, but I am pretty new to PHP and I am running into some issues. I am trying to build a contact form with HTML, CSS, and PHP, but I can't seem to get my PHP form to send the contents of the form to my email address. This is what the code looks like for the 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>

And in a separate doc called "mailer.php" this is what the code looks like:

<?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();
}
?>

Here is a link to my website if it will help. Can someone tell me what it is I am doing wrong?

Your code lacks proper mail headers. (and originally had a missing subject variable which you now added).

Add and modify your present mail() function with the following code, otherwise mail will be sent directly to Spam as it did for my test.

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

with a conditional statement:

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

After running sudo apt-get install sendmail I finally started receiving the contents of the contact form. This solution should work for those using DigitalOcean as their host.