Eddy
Eddy

Reputation: 11

Contact form wont submit and keeps going to error page

When I fill in all the fields in the contact form it keeps on going to the error page. I don't know why this is happening. I want the contact form to send me the all the information to my personal e-mail. I have looks at almost all solutions but cant seem to fix the problem.

The code for my form is:

<div id="form">
    <form action="sendmail.php" method="post">
    <p>
        <label for="name">Name:</label>
        <input name="name" id="name" type="text" class="required">
        <span>Please enter your name</span>
    </p>
    <p>
        <label for="email">Email:</label>
        <input name="email" id="email" type="text" class="required"> 
        <span>Please enter a valid email address</span>
    </p>

    <p>
        <label for="subject">Subject:</label>
        <input name="subject" id="subject" type="text"> 
        <span>Please enter your subject</span>
    </p>
    <p>
        <label for="message">Message</label>
        <textarea name="message" id="message" class="required"></textarea> 
        <span>Please enter your message</span>
    </p>
    <p class="submit">
        <input type="submit" value="Submit" class="btn-submit">
    </p>
    </form>
</div>

My PHP Code is :

<?php

// This function checks for email injection. Specifically, it checks for carriage returns 
$injections = array('(\n+)',
    '(\r+)',
    '(\t+)',
    '(%0A+)',
    '(%0D+)',
    '(%08+)',
    '(%09+)'
);

$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str)) {
    return true;
}
else {
    return false;
}


// Load form field data into variables.
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;


// If the user tries to access this script directly, redirect them to feedback form,
if (!isset($_REQUEST['email'])) {
    header( "Location: contact.html" );
}

// If the form fields are empty, redirect to the error page.
elseif (empty($email_address) || empty($comments)) {
    header( "Location: error.html" );
}

// If email injection is detected, redirect to the error page.
//elseif ( isInjected($email) ) {
    // header( "Location: error.html" );
}

// If we passed all previous tests, send the email!
else {
    mail( "[email protected]", "Feedback Form Results",
    $comments, "From: $email" );
    header( "Location: thankyou.html" );
}
?>

Upvotes: 0

Views: 112

Answers (2)

mowgli
mowgli

Reputation: 2869

The code is quite messy

Where is $str set?

You check it for injections: if(preg_match($inject,$str)), but what is $str?

And you check empty($email_address) || empty($comments) But these are not the vars you set (you set $email = $_REQUEST['email'], $message = $_REQUEST['message'] etc.)

Upvotes: 0

meda
meda

Reputation: 45490

Because you didn't put the email check logic into a function, the return statements will stop the code from executing even if it's true. the proper way would be:

if(preg_match($inject,$str)) {
  //process
}
else {
  exit();
}

EDIT

Here is how you can put that logic in a function

/**
 *This function checks for email injection. 
 *Specifically, it checks for carriage returns 
 *@return Boolean upon failure or success
 */
 function has_injection($email){
    $injections = array('(\n+)',
                        '(\r+)',
                        '(\t+)',
                        '(%0A+)',
                        '(%0D+)',
                        '(%08+)',
                        '(%09+)'
                 );

    $inject = join('|', $injections);
    $inject = "/$inject/i";
    if(preg_match($inject,$email)) {
        return true;
    }
    else {
        return false;
    }
 }

Then use it:

<?php

if(!has_injection($_REQUEST['email']){
    //process code
}else{
    die('terminating script because of email injection!');
}

Upvotes: 2

Related Questions