0

I'm using a php mail script from http://blog.teamtreehouse.com/create-ajax-contact-form connected to a contact form on a website I'm building.

I have everything put together and it seems to function properly (doesn't display any errors), but I am not receiving most of the emails. If I test the contact form using one of my personal email accounts I will receive the message. If I use any other emails (for example something from http://www.fakemailgenerator.com) I won't receive anything. I've attached all of the code below. Is there something I'm missing that anyone can spot?

If it helps, the site is hosted in an AWS LAMP linux instance and the domain is with GoDaddy.

Thanks in advance.

contact.html

<form id="ajax-contact" method="post" action="mailer.php">
    <div class="field">
        <input type="text" id="name" name="name" placeholder="name*" required>
    </div>

    <div class="field">
            <input type="email" id="email" name="email" placeholder="email*" required>
    </div>

    <div class="field">
            <input type="text" id="school" name="school" placeholder="school" >
    </div>

    <div class="field">
            <input type="tel" id="phone" name="phone" placeholder="phone" >
    </div>

    <div class="field">
            <textarea id="message" name="message" placeholder="message" ></textarea>
    </div>

    <div class="field">
            <button type="submit">Submit</button>
    </div>
</form>

mailer.php

<?php
    // My modifications to mailer script from:
    // http://blog.teamtreehouse.com/create-ajax-contact-form
    // Added input sanitizing to prevent injection

    // 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);
        $phone = trim($_POST["phone"]);
        $school = trim($_POST["school"]);
        $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 "There was a problem with your submission. Please ensure all required fields are filled out.";
            exit;
        }

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

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

        // Build the email content.
        $email_content = "Name: $name\n";
        $email_content .= "Email: $email\n\n";
        $email_content .= "School: $school\n";
        $email_content .= "Phone: $phone\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. We'll get back to you as soon as possible.";
        } 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. Please try again later.";
        }

    } 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.";
    }

?>

app.js

$(function() {

    // Get the form.
    var form = $('#ajax-contact');

    // Get the messages div.
    var formMessages = $('#form-messages');

    // Set up an event listener for the contact form.
    $(form).submit(function(e) {
        // Stop the browser from submitting the form.
        e.preventDefault();

        // Serialize the form data.
        var formData = $(form).serialize();

        // Submit the form using AJAX.
        $.ajax({
            type: 'POST',
            url: $(form).attr('action'),
            data: formData
        })
        .done(function(response) {
            // Make sure that the formMessages div has the 'success' class.
            $(formMessages).removeClass('error');
            $(formMessages).addClass('success');

            // Set the message text.
            $(formMessages).text(response);

            // Clear the form.
            $('#name').val('');
            $('#email').val('');
            $('#phone').val('');
            $('#school').val('');
            $('#message').val('');

        })
        .fail(function(data) {
            // Make sure that the formMessages div has the 'error' class.
            $(formMessages).removeClass('success');
            $(formMessages).addClass('error');

            // Set the message text.
            if (data.responseText !== '') {
                $(formMessages).text(data.responseText);
            } else {
                $(formMessages).text('Oops! An error occured and your message could not be sent.');
            }
        });

    });

});

2 Answers2

1

I think it's simply Amazon that filter suspicious mail provider. Most of SMTP server comes with filters. Thoses filters prevents user to receive spam.

As said from Amazon FAQ :

Amazon SES uses in-house anti-spam/anti-virus technologies to filter email messages containing poor-quality content and prevent them from being sent.

GMartigny
  • 126
  • 8
0

Your message formatting is just wrong, I'm not going to begin to correct it. Use a library to do it right - you already tagged this question with PHPMailer, but you're not using it. Base your code on one of the examples provided with PHPMailer, not one you find elsewhere as nearly all are obsolete or wrong.

You're also forging the from address, so many services will just reject or bounce your messages. Send from your own address and put the submitter's address in reply-to.

Also ensure that your SPF record is configured correctly, and ideally sign your messages with DKIM too.

Synchro
  • 35,538
  • 15
  • 81
  • 104
  • I'm not actually using PHPMailer, I didn't realize that was a library when I tagged it. My form is based purely on the teamtreehouse post linked above. Thanks for your other suggestions. – Gideon Shils Aug 04 '15 at 19:53