Contact Form fails to send out an email

192 views Asked by At

After filling up the contact form with valid information, and hitting submit, I get a message of success! But, on checking the my inbox, I don't the email. What is wrong ?

P.S. I have checked my web server. It sends out mail but this contact forms just fails.

Contact Form

<section class="span8 ">
    <div class="col-right">
        <hr>
        <h4>General Enquire or Apply</h4>

        <div id="message-contact"></div>
        <form method="post" action="assets/contact.php" id="contactform">
            <div class="row">
                <div class="span3">
                    <label>Name <span class="required">* </span></label>
                    <input type="text" class="span3 ie7-margin" id="name_contact">
                </div>
                <div class="span3">
                    <label>Last name <span class="required">* </span></label>
                    <input type="text" class="span3 ie7-margin" id="lastname_contact">
                </div>
            </div>
            <div class="row">
                <div class="span3">
                    <label>Email <span class="required">* </span></label>
                    <input type="email" id="email_contact" class="span3 ie7-margin">
                </div>
                <div class="span3">
                    <label>Phone <span class="required">* </span></label>
                    <input type="text" id="phone_contact" class="span3 ie7-margin">
                </div>
            </div>
            <div class="row">
                <div class="span3">
                    <label>Select a department</label>
                    <select id="subject_contact" class="span3">
                        <option value="Administration">Administration</option>
                        <option value="Admissions">Admissions</option>
                        <option value="Courses">Courses</option>
                        <option value="Apply">Apply</option>
                    </select>
                </div>
            </div>
            <div class="row">
                <div class="span3">
                    <label>Message <span class="required">*</span></label>
                    <textarea rows="5" id="message_contact" class="span6"></textarea>
                </div>
            </div>
            <div class="row">
                <div class="span3">
                    <label><span class="required">*</span> Are you human? 3 + 1 =</label>
                    <input type="text" id="verify_contact" class="span2">
                </div>
                <div class="button-align span3">
                    <input type="submit" id="submit-contact" value="Submit" class=" button_medium">
                </div>
            </div>
            <hr>
        </form>

        <h4>Plan a visit</h4>
        <div id="message-visit"></div>
        <form method="post" action="assets/visit.php" id="visit">
            <div class="row">
                <div class="span3">
                    <label>Name <span class="required">* </span></label>
                    <input type="text" class="span3 ie7-margin" id="name_visit">
                </div>
                <div class="span3">
                    <label>Last name <span class="required">* </span></label>
                    <input type="text" class="span3 ie7-margin" id="lastname_visit">
                </div>
            </div>
            <div class="row">
                <div class="span3">
                    <label>Email <span class="required">* </span></label>
                    <input type="email" id="email_visit" class="span3 ie7-margin">
                </div>
                <div class="span3">
                    <label>Phone <span class="required">* </span></label>
                    <input type="text" id="phone_visit" class="span3 ie7-margin">
                </div>
            </div>
            <div class="row">
                <div class="span3">
                    <div id="datetimepicker" class="input-append">
                        <label>Select a date <span class="required">* </span></label>
                        <input type="text" class=" dateinput" id="date_visit" readonly>
                        <span class="add-on"><i data-time-icon="icon-time" data-date-icon="icon-calendar"></i></span>
                    </div>
                </div>
                <div class="span3">
                    <label><span class="required">*</span> Are you human? 3 + 1 =</label>
                    <input type="text" id="verify_visit" class="span2">
                </div>
            </div>
            <!-- end row-->
            <input type="submit" id="submit-visit" value="Submit" class=" button_medium">
        </form>

    </div><!-- end col right-->
    </section>



contact.php

<?php

if(!$_POST) exit;

// Email verification, do not edit.
function isEmail($email_contact ) {
    return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email_contact ));
}

if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");

$name_contact     = $_POST['name_contact'];
$lastname_contact    = $_POST['lastname_contact'];
$email_contact    = $_POST['email_contact'];
$phone_contact   = $_POST['phone_contact'];
$subject_contact  = $_POST['subject_contact'];
$message_contact = $_POST['message_contact'];
$verify_contact   = $_POST['verify_contact'];

if(trim($name_contact) == '') {
    echo '<div class="error_message"><i class=" icon-warning-sign"></i> You must enter your Name.</div>';
    exit();
} else if(trim($lastname_contact ) == '') {
    echo '<div class="error_message"><i class=" icon-warning-sign"></i>You must enter your Last name.</div>';
    exit();
} else if(trim($email_contact) == '') {
    echo '<div class="error_message"><i class=" icon-warning-sign"></i>Please enter a valid email address.</div>';
    exit();
} else if(!isEmail($email_contact)) {
    echo '<div class="error_message"><i class=" icon-warning-sign"></i> You have enter an invalid e-mail address, try again.</div>';
    exit();
    } else if(trim($phone_contact) == '') {
    echo '<div class="error_message">Please enter a valid phone number.</div>';
    exit();
} else if(!is_numeric($phone_contact)) {
    echo '<div class="error_message">Phone number can only contain numbers.</div>';
    exit();
}
if(trim($subject_contact) == '') {
    echo '<div class="error_message"><i class=" icon-warning-sign"></i> Please select a department.</div>';
    exit();
} else if(trim($message_contact) == '') {
    echo '<div class="error_message"><i class=" icon-warning-sign"></i> Please enter your message.</div>';
    exit();
} else if(!isset($verify_contact) || trim($verify_contact) == '') {
    echo '<div class="error_message"><i class=" icon-warning-sign"></i> Please enter the verification number.</div>';
    exit();
} else if(trim($verify_contact) != '4') {
    echo '<div class="error_message"><i class=" icon-warning-sign"></i> The verification number you entered is incorrect.</div>';
    exit();
}

if(get_magic_quotes_gpc()) {
    $message_contact = stripslashes($message_contact);
}


//$address = "HERE your email address";
$address = "[email protected]";


// Below the subject of the email
$e_subject = 'You\'ve been contacted by ' . $name_contact . '.';

// You can change this if you feel that you need to.
$e_body = "You have been contacted by $name_contact $lastname_contact with attention to $subject_contact, with additional message is as follows." . PHP_EOL . PHP_EOL;
$e_content = "\"$message_contact\"" . PHP_EOL . PHP_EOL;
$e_reply = "You can contact $lastname_contact via email, $email_contact or via phone $phone_contact";

$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );

$headers = "From: $email_contact" . PHP_EOL;
$headers .= "Reply-To: $email_contact" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;

if(mail($address, $e_subject, $msg, $headers)) {

    // Success message
    echo "<div id='success_page' style='padding:20px'>";
    echo "<strong >Email Sent.</strong>";
    echo "Thank you <strong>$name_contact</strong>,<br> your message has been submitted. We will contact you shortly.";
    echo "</div>";

} else {

    echo 'ERROR!';

}
?>
2

There are 2 answers

7
Praveen D On BEST ANSWER

You need to add name attribute with each controller.

Example

<input type="text" class="span3 ie7-margin" id="name_contact">

Replace with

<input type="text" class="span3 ie7-margin" name="name_contact" id="name_contact">

[name="name_contact"] added

=====

If you get success message but mail not sent then check mail server logs at C:\xampp\apache\mailoutput

If mail() returns true, thus php job is done. there is server side issue.

2
robinp7720 On

You have to specify the input name with the "name" argument, not the with the id. So replace "id" with "name".

Replace this

<form method="post" action="assets/contact.php" id="contactform">
            <div class="row">
                <div class="span3">
                    <label>Name <span class="required">* </span></label>
                    <input type="text" class="span3 ie7-margin" id="name_contact">
                </div>
                <div class="span3">
                    <label>Last name <span class="required">* </span></label>
                    <input type="text" class="span3 ie7-margin" id="lastname_contact">
                </div>
            </div>
            <div class="row">
                <div class="span3">
                    <label>Email <span class="required">* </span></label>
                    <input type="email" id="email_contact" class="span3 ie7-margin">
                </div>
                <div class="span3">
                    <label>Phone <span class="required">* </span></label>
                    <input type="text" id="phone_contact" class="span3 ie7-margin">
                </div>
            </div>
            <div class="row">
                <div class="span3">
                    <label>Select a department</label>
                    <select id="subject_contact" class="span3">
                        <option value="Administration">Administration</option>
                        <option value="Admissions">Admissions</option>
                        <option value="Courses">Courses</option>
                        <option value="Apply">Apply</option>
                    </select>
                </div>
            </div>
            <div class="row">
                <div class="span3">
                    <label>Message <span class="required">*</span></label>
                    <textarea rows="5" id="message_contact" class="span6"></textarea>
                </div>
            </div>
            <div class="row">
                <div class="span3">
                    <label><span class="required">*</span> Are you human? 3 + 1 =</label>
                    <input type="text" id="verify_contact" class="span2">
                </div>
                <div class="button-align span3">
                    <input type="submit" id="submit-contact" value="Submit" class=" button_medium">
                </div>
            </div>
            <hr>
        </form>

with

<form method="post" action="assets/contact.php" id="contactform">
            <div class="row">
                <div class="span3">
                    <label>Name <span class="required">* </span></label>
                    <input type="text" class="span3 ie7-margin" name="name_contact"><!-- Replaced id with name -->
                </div>
                <div class="span3">
                    <label>Last name <span class="required">* </span></label>
                    <input type="text" class="span3 ie7-margin" name="lastname_contact"><!-- Replaced id with name -->
                </div>
            </div>
            <div class="row">
                <div class="span3">
                    <label>Email <span class="required">* </span></label>
                    <input type="email" name="email_contact" class="span3 ie7-margin"><!-- Replaced id with name -->
                </div>
                <div class="span3">
                    <label>Phone <span class="required">* </span></label>
                    <input type="text" name="phone_contact" class="span3 ie7-margin"><!-- Replaced id with name -->
                </div>
            </div>
            <div class="row">
                <div class="span3">
                    <label>Select a department</label>
                    <select name="subject_contact" class="span3"><!-- Replaced id with name -->
                        <option value="Administration">Administration</option>
                        <option value="Admissions">Admissions</option>
                        <option value="Courses">Courses</option>
                        <option value="Apply">Apply</option>
                    </select>
                </div>
            </div>
            <div class="row">
                <div class="span3">
                    <label>Message <span class="required">*</span></label>
                    <textarea rows="5" name="message_contact" class="span6"></textarea><!-- Replaced id with name -->
                </div>
            </div>
            <div class="row">
                <div class="span3">
                    <label><span class="required">*</span> Are you human? 3 + 1 =</label>
                    <input type="text" name="verify_contact" class="span2"><!-- Replaced id with name -->
                </div>
                <div class="button-align span3">
                    <input type="submit" name="submit-contact" value="Submit" class=" button_medium"><!-- Replaced id with name -->
                </div>
            </div>
            <hr>
        </form>

and regarding the way to send emails in php, check out this very detailed answer here: PHP mail form doesn't complete sending e-mail