codeDevil codeDevil - 3 months ago 9
HTML Question

contact HTML / PHP form doesn't send mails

I'm using AJAX contact form but it doesn't send mails. I tried a lot of stuff, but I really don't know what the problem is.

<!-- START CONTACT FORM -->
<div id="contact_form" class="grid_6" style="margin:0;">
<div class="form-success">
<p>Ju falemnderit, mesazhi juaj eshte derguar!</p>
</div>

<div class="contact-form">
<form action="contact-form/send.php" method="post" class="form">
<label>Emri dhe mbiemri</label>
<input class="text" type="text" name="name">

<label>E-Mail</label>
<input class="text" type="text" name="email">

<!--
<label>Subject</label>
<input class="text" type="text" name="subject">
-->

<label>Koment</label>
<textarea name="message" rows="8" cols="60"></textarea>

<a href="javascript:;" id="submit" class="button">Dergo Email</a>

<div class="loading"></div>
</form>
</div>
</div>
<!-- END CONTACT FORM -->


and this is contact-form/send.php

<?php

//Your e-mail address goes here:

$to = "lorentsh@hotmail.com";
//


//Retrieve form data.
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$subject = ($_GET['subject']) ?$_GET['subject'] : $_POST['subject'];
$comment = ($_GET['comment']) ?$_GET['comment'] : $_POST['message'];

//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;

//Include email validator
require 'email-validator.php';
$validator = new EmailAddressValidator();

//Simple server side validation for POST data, of course, you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.';
if (!$comment) $errors[count($errors)] = 'Please enter your comment.';

$email = strip_tags($email);

if (!$validator->check_email_address($email)) {
$errors[count($errors)] = 'Invalid email address.';
}

//if the errors array is empty, send the mail
if (!$errors) {

//sender
$from = $name . ' <' . $email . '>';

//Structure of the message:
$subject = 'Message from ' . $name;
$message = '
<!DOCTYPE html>
<head></head>
<body>
<table>
<tr><td>Name:</td><td>' . $name . '</td></tr>
<tr><td>Email:</td><td>' . $email . '</td></tr>
<tr><td>Subject:</td><td>' . $subject . '</td></tr>
<tr><td>Message:</td><td>' . nl2br($comment) . '</td></tr>
</table>
</body>
</html>';

//End of the message structure


//send the mail
$result = sendmail($to, $subject, $message, $from);

//if POST was used, display the message straight away
if ($_POST) {
if ($result) echo 'Thank you! We have received your message.';
else echo 'Sorry, unexpected error. Please try again later';

//else if GET was used, return the boolean value so that
//ajax script can react accordingly
//1 means success, 0 means failed
} else {
echo $result;
}

//if the errors array has values
} else {
//display the errors message
for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
echo '<a href="../contact.html">Back</a>';
exit;
}


//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";

$result = mail($to,$subject,$message,$headers);

if ($result) return 1;
else return 0;
}

?>


Can you tell me wherethe problem is? To index.html or send.php. I need help from you guys.

Answer

The problem should be in line

<a href="javascript:;" id="submit" class="button">Dergo Email</a>

You need something like a button which submits your form with

type="submit"

Also please make sure your hosting service supports PHP.

You may also refer to Not recieving the mail.

I tested it and please see the screenshot below. Received

Sent

And the code is attached.

send.php

<?php

//Your e-mail address goes here: 

$to = "lorentsh@hotmail.com";
//


//Retrieve form data. 
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$subject = ($_GET['subject']) ?$_GET['subject'] : $_POST['subject'];
$comment = ($_GET['comment']) ?$_GET['comment'] : $_POST['message'];

//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;


//Simple server side validation for POST data, of course, you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.'; 
if (!$comment) $errors[count($errors)] = 'Please enter your comment.'; 

$email = strip_tags($email);


//if the errors array is empty, send the mail
if (true) {

    //sender
    $from = $name . ' <' . $email . '>';

    //Structure of the message:
    $subject = 'Message from ' . $name; 
    $message = '
    <!DOCTYPE html>
    <head></head>
    <body>
    <table>
        <tr><td>Name:</td><td>' . $name . '</td></tr>
        <tr><td>Email:</td><td>' . $email . '</td></tr>
        <tr><td>Subject:</td><td>' . $subject . '</td></tr>
        <tr><td>Message:</td><td>' . nl2br($comment) . '</td></tr>
    </table>
    </body>
    </html>';

    //End of the message structure


    //send the mail
    $result = sendmail($to, $subject, $message, $from);

    //if POST was used, display the message straight away
    if ($_POST) {
        if ($result) echo 'Thank you! We have received your message.';
        else echo 'Sorry, unexpected error. Please try again later';

    //else if GET was used, return the boolean value so that 
    //ajax script can react accordingly
    //1 means success, 0 means failed
    } else {
        echo $result;   
    }

//if the errors array has values
} else {
    //display the errors message
    for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
    echo '<a href="../contact.html">Back</a>';
    exit;
}


//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    $headers .= 'From: ' . $from . "\r\n";

    $result = mail($to,$subject,$message,$headers);

    if ($result) return 1;
    else return 0;
}

?>

HTML file

 <!-- START CONTACT FORM -->         
            <div id="contact_form" class="grid_6" style="margin:0;">
                <div class="form-success">
                    <p>Ju falemnderit, mesazhi juaj eshte derguar!</p>
                </div>

                <div class="contact-form"> 
                    <form action="contact-form/send.php" method="post" class="form">    
                        <label>Emri dhe mbiemri</label> 
                        <input class="text" type="text" name="name"> 

                        <label>E-Mail</label> 
                        <input class="text" type="text" name="email"> 

                        <!--     
                        <label>Subject</label> 
                        <input class="text" type="text" name="subject"> 
                         -->

                        <label>Koment</label> 
                        <textarea name="message" rows="8" cols="60"></textarea> 

                        <button class="submit" id="submit" type="submit">Dergo Email</button>

                         <div class="loading"></div> 
                    </form> 
                </div>
            </div>
            <!-- END CONTACT FORM -->

Hence, as what I said, it may because of your free-hosting server doesn't support that.

You may want to test it yourself on http://goo.gl/ynTnE, I will keep it there for a few days.