0

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?

2 Answers2

1

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 . "\r\n";
mail($myemail, $subject, $message, $headers);

with a conditional statement:

if(mail($myemail, $subject, $message, $headers)){ 
   echo "Success"; } else{ echo "There was a problem.";}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • I tried making the edits you suggested, but it's still not working. I looked in the spam folder, but nothing is showing up. – IAMATinyCoder May 29 '14 at 01:39
  • Are you running this through a hosted website or from your own computer? @IAMATinyCoder – Funk Forty Niner May 29 '14 at 01:40
  • From a hosted website. http://www.christopherkenrick.com/requests.html My website is hosted through DigitalOcean. – IAMATinyCoder May 29 '14 at 01:41
  • Does that host offer `mail()`? Is it a free or paid host? @IAMATinyCoder Plus, try using a conditional statement instead `if(mail($myemail, $subject, $message, $headers)){ echo "Success";} else{ echo "There was a problem.";}` – Funk Forty Niner May 29 '14 at 01:43
  • I suggest you go through their FAQ's and this https://www.digitalocean.com/community/tags/email and/or contact their Tech support. @IAMATinyCoder plus this Q&A https://www.digitalocean.com/community/questions/php-mail-not-working-but-sendmail-through-command-prompt-works - I Google'd `"digitalOcean mail not working"` and that's what I found. – Funk Forty Niner May 29 '14 at 01:51
  • Thanks so much for your help. I'll try the suggestions from that post. – IAMATinyCoder May 29 '14 at 02:09
  • After running `sudo apt-get install sendmail` I finally started receiving the contents of the contact form. Thanks again for all of your help! – IAMATinyCoder May 29 '14 at 03:19
  • @IAMATinyCoder That's great news, am glad to hear it. – Funk Forty Niner May 29 '14 at 03:20
  • @IAMATinyCoder If you feel that this matter is resolved, you may consider to tick the white checkmark next to my answer till it turns Green. Otherwise your question will remain as unanswered. Not really mandatory, but it does help tell SO and others that a solution has been found. – Funk Forty Niner May 29 '14 at 03:27
0

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.