Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Can someone help me with figuring out why this php script isn't sending an email?

So i found this php code online on a treehouse article and i followed step by step. The code work perfectly a few times. perfect meaning i got the information to send to my email, but it ended up in my spam. Now none of that is happening and i didn't change a thing. The site is live now i figure try the form live instead of locally. Same problem. i host my site thru dreamhost, if that information helps any. Heres the site: www.dpwebdevelopment.com Heres the code:

        <form id="ajax-contact" class="center-block" method="POST" action="mailer.php">
          <div class="form-group">
            <label class="sr-only" for="name">Full name</label>

            <input id="name" class="form-control" type="text" name="name" placeholder="john Smith" required />
          </div>

          <div class="form-group">
            <label for="email" class="sr-only">Email Address</label>

            <input id="email" name ="email" class="form-control" type="email" placeholder="example@mail.com" required />
          </div>

          <div class="form-group">
            <label for="message" class="sr-only">Your Message</label>

            <textarea id="message" class="form-control" name="message" cols="30" rows="8" required></textarea>
          </div>

          <div class="form-group">
            <button class="btn btn-default center-block" type="submit" name="submit" value="Hire me">Hire me</button>
          </div>
        </form>```

<?php

// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get the form fields and remove whitespace.
    $name = strip_tags(trim($_POST["name"]));
            $name = str_replace(array("\r","\n"),array(" "," "),$name);
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $message = trim($_POST["message"]);

    // Check that data was sent to the mailer.
    if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        // Set a 400 (bad request) response code and exit.
        http_response_code(400);
        echo "Oops! There was a problem with your submission. Please complete the form and try again.";
        exit;
    }

    // Set the recipient email address.
    // FIXME: Update this to your desired email address.
    $recipient = 'dpwebdevelopment21@gmail.com';

    // Set the email subject.
    $subject = "New contact from $name";

    // Build the email content.
    $email_content = "Name: $name\n";
    $email_content .= "Email: $email\n\n";
    $email_content .= "Message:\n$message\n";

    // Build the email headers.
    $email_headers = "From: $name <$email>";

    // Send the email.
    if (mail($recipient, $subject, $email_content, $email_headers)) {
        // Set a 200 (okay) response code.
        http_response_code(200);
        echo "Thank You! Your message has been sent.";
    } else {
        // Set a 500 (internal server error) response code.
        http_response_code(500);
        echo "Oops! Something went wrong and we couldn't send your message.";
    }

} else {
    // Not a POST request, set a 403 (forbidden) response code.
    http_response_code(403);
    echo "There was a problem with your submission, please try again.";
}

?>```