Harry12345
Harry12345

Reputation: 1160

php contact form not sending mail after validation

I'm using a contact form for my website, which I validate and then email to myself, the validation is working correctly and it emails me if the user enters all the details correctly first time. However if the user enters incorrect data, then corrects it and hits send again, it won't send an email, below is the form and PHP code I have so far.

HTML code for contact form

<form action="contact.php" method="post">
<fieldset>

<label for="name">Name:<span class="star">*</span></label> <br />
<input type="text" name="name" id="name" placeholder="Enter your name" maxlength="50" required />
<label for="email">Email:<span class="star">*</span></label> <br />
<input type="email" name="email" id="email" placeholder="Enter your email address" maxlength="100" required />
<label for="number">Telephone: </label><input type="tel" name="number" id="number" placeholder="Enter your phone number" maxlength="12" />
<label for="message">Message:<span class="star">*</span></label>
<textarea name="message" id="message" placeholder="Enter your message" cols="54" rows="5" required></textarea>

<p class="small"><span class="star">*</span>&nbsp; Denotes a required field </p>

<input type="submit" id="send" name="send" value="Send" />

</fieldset>
</form>

PHP code for sending the form

    function fix_string($var)
{
    if(get_magic_quotes_gpc()) $var = stripslashes($var);
    $var = strip_tags($var);
    return $var;
}

{
    $details = array('name' => fix_string($_POST['name']),
            'email' => fix_string($_POST['email']),
            'number' => fix_string($_POST['number']),
            'message' => fix_string($_POST['message']));
}

$send = $_POST['send'];
$message = "";

 foreach ($details as $field => $detail)
    $message .= $field . ": " . $detail . "<br />";

$to = "[email protected]";
$subject = "Website contact form";
$message = wordwrap($message, 70, "/r/n");
$headers = "From ". $details['email'];

function trim_value(&$value)
{
    $value = trim($value);
}

array_walk($details, 'trim_value');

if ($send)
{
    foreach ($details as $field => $detail)
    {
        if (empty($detail) && $field!='number')
            echo "<p class='error'>Please fill in the required field: " . ucfirst($field) . "<br /></p>";

    }
}
else
{
    mail($to, $subject, $message, $headers);
    echo "<p class='success'>Mail was sent successfully</p>";
}

Upvotes: 0

Views: 1872

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

EDIT

In order for the code to work on the same page, you need to set the action to action=""

Otherwise, you need to use two pages. One for your form and one for contact.php which is your handler. I suggest you use two pages, but here is a version that will work inside one page.

<?php

if(isset($_POST['send'])) {

function fix_string($var)
{
    if(get_magic_quotes_gpc()) $var = stripslashes($var);
    $var = strip_tags($var);
    return $var;
}

    $details = array('name' => fix_string($_POST['name']),
            'email' => fix_string($_POST['email']),
            'number' => fix_string($_POST['number']),
            'message' => fix_string($_POST['message']));

function trim_value(&$value)
{
    $value = trim($value);
}

array_walk($details, 'trim_value');

    foreach ($details as $field => $detail)

    {
        if (empty($detail) && $field!='number')

            die("<p class='error'>Please fill in the required field: " . ucfirst($field) . "<br /></p>");

    }

$message = "";
 foreach ($details as $field => $detail)
    $message .= $field . ": " . $detail . "\n";

$send = $_POST['send'];
$email = $_POST['email'];

$to = "[email protected]";
$subject = "Website contact form";

$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();

    mail($to, $subject, $message, $headers);
    echo "<p class='success'>Mail was sent successfully</p>";


exit;

} // closing brace for if(isset($_POST['send'])) {

?>

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>

<body>

<form action="" method="post">
<fieldset>

<label for="name">Name:<span class="star">*</span></label> <br />
<input type="text" name="name" id="name" placeholder="Enter your name" maxlength="50"  />
<label for="email">Email:<span class="star">*</span></label> <br />
<input type="email" name="email" id="email" placeholder="Enter your email address" maxlength="100"  />
<label for="number">Telephone: </label><input type="tel" name="number" id="number" placeholder="Enter your phone number" maxlength="12" />
<label for="message">Message:<span class="star">*</span></label>
<textarea name="message" id="message" placeholder="Enter your message" cols="54" rows="5" ></textarea>

<p class="small"><span class="star">*</span>&nbsp; Denotes a required field </p>

<input type="submit" id="send" name="send" value="Send" />

</fieldset>
</form>

</body>
</html>

Original answer

This line is not properly formatted.

$message = wordwrap($message, 70, "/r/n");

change it to:

$message = wordwrap($message, 70, "\r\n");

You need to use \ instead of /

EDIT

The only way I could get your form to work, is to add a die function.

Try this now:

<?php

function fix_string($var)
{
    if(get_magic_quotes_gpc()) $var = stripslashes($var);
    $var = strip_tags($var);
    return $var;
}

    $details = array('name' => fix_string($_POST['name']),
            'email' => fix_string($_POST['email']),
            'number' => fix_string($_POST['number']),
            'message' => fix_string($_POST['message']));

function trim_value(&$value)
{
    $value = trim($value);
}

array_walk($details, 'trim_value');

    foreach ($details as $field => $detail)

    {
        if (empty($detail) && $field!='number')

            die("<p class='error'>Please fill in the required field: " . ucfirst($field) . "<br /></p>");

    }

$message = "";
 foreach ($details as $field => $detail)
    $message .= $field . ": " . $detail . "\n";

$send = $_POST['send'];
$email = $_POST['email'];

$to = "[email protected]";
$subject = "Website contact form";

$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();

    mail($to, $subject, $message, $headers);
    echo "<p class='success'>Mail was sent successfully</p>";


exit;

?>

Upvotes: 1

Related Questions