• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.
    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

HTML & CSS Contact form dont send email.

hpscorpion

New Coder
I'm using a template website that is practically finished, but the form submit dont work.
Pls anyone can help me?

Here is my basic html:
Code:
<form action="#" class="form-contact" id="contactForm">
  <div class="row">
    <div class="col-sm-6 col-md-6">
      <div class="form-group">
        <input type="text" class="form-control" id="p_name" placeholder="* Nome" required="">
        <div class="help-block with-errors"></div>
      </div>
    </div>
    <div class="col-sm-6 col-md-6">
      <div class="form-group">
        <input type="email" class="form-control" id="p_email" placeholder="* Email" required="">
        <div class="help-block with-errors"></div>
      </div>
    </div>
    <div class="col-sm-6 col-md-6">
      <div class="form-group">
        <input type="text" class="form-control" id="p_phone" placeholder="* Telefone">
        <div class="help-block with-errors"></div>
      </div>
    </div>
    <div class="col-sm-6 col-md-6">
      <div class="form-group">
        <input type="text" class="form-control" id="p_subject" placeholder="* Assunto">
        <div class="help-block with-errors"></div>
      </div>
    </div>
  </div>
  <div class="form-group">
    <textarea id="p_message" class="form-control" rows="6" placeholder="* Mensagem"></textarea>
    <div class="help-block with-errors"></div>
  </div>
  <div class="form-group">
    <div id="success"></div>
    <button type="submit" class="btn btn-primary">ENVIAR</button>
  </div>
</form>

here is my form-process.php:
Code:
<?php

$errorMSG = "";

// NAME
if (empty($_POST["name"])) {
    $errorMSG = "Name is required ";
} else {
    $name = $_POST["name"];
}

// EMAIL
if (empty($_POST["email"])) {
    $errorMSG .= "Email is required ";
} else {
    $email = $_POST["email"];
}

// SUBJECT
if (empty($_POST["subject"])) {
    $errorMSG .= "Subject is required ";
} else {
    $subject = $_POST["subject"];
}

// MESSAGE
if (empty($_POST["message"])) {
    $errorMSG .= "Message is required ";
} else {
    $message = $_POST["message"];
}


$EmailTo = "[email protected]";
$Subject = $subject;

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";

// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);

// redirect to success page
if ($success && $errorMSG == ""){
   echo "success";
}else{
    if($errorMSG == ""){
        echo "Something went wrong :(";
    } else {
        echo $errorMSG;
    }
}

?>

Here is my ajax/javascript for sending the email:
Code:
$("#contactForm").validator().on("submit", function (event) {
    if (event.isDefaultPrevented()) {
        // handle the invalid form...
        formError();
        submitMSG(false, "Did you fill in the form properly?");
    } else {
        // everything looks good!
        event.preventDefault();
        submitForm();
    }
});


function submitForm(){
    // Initiate Variables With Form Content
    var name = $("#p_name").val();
    var email = $("#p_email").val();
    var subject = $("#p_subject").val();
    var message = $("#p_message").val();

    $.ajax({
        type: "POST",
        url: "php/form-process.php",
        data: "name=" + name + "&email=" + email + "&subject=" + subject + "&message=" + message,
        success : function(text){
            if (text == "success"){
                formSuccess();
            } else {
                formError();
                submitMSG(false,text);
            }
        }
    });
}

function formSuccess(){
    $("#contactForm")[0].reset();
    submitMSG(true, "Message Submitted!")
}

function formError(){
    $("#contactForm").removeClass().addClass('shake animated').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
        $(this).removeClass();
    });
}

function submitMSG(valid, msg){
    if(valid){
        var msgClasses = "h3 text-center tada animated text-success";
    } else {
        var msgClasses = "h3 text-center text-danger";
    }
    $("#success").removeClass().addClass(msgClasses).text(msg);
}


I was never able to send a form, I always get the error "Did you fill in the form properly?"
 

hpscorpion

New Coder
@Malcolm ohh I find the problem is my Host.. I try on free host and work perfect. no problem with code. my isp host said dont use PHP mail().. and advised me to change to framework SMTP, like PHPMailer() or SwiftMailer(). No ideia how to change this.
 

hpscorpion

New Coder
I try this one, but dont work. but i think its close.

Code:
<?php
$errorMSG = "";

// NAME

if (empty($_POST["name"])) {

    $errorMSG = "Name is required ";

} else {

    $name = $_POST["name"];

}

// Phone

if (empty($_POST["phone"])) {

    $errorMSG = "Phone is required ";

} else {

    $phone = $_POST["phone"];
}


// EMAIL

if (empty($_POST["email"])) {

    $errorMSG .= "Email is required ";

} else {

    $email = $_POST["email"];

}

// MESSAGE

if (empty($_POST["message"])) {

    $errorMSG .= "Message is required ";

} else {

    $message = $_POST["message"];

}

/* Contact Form Setup Begin */

    $send_name      = "No Name";      // Replace your name
    $send_title     = "ZAKAZ";        // Replace email sent title
    $send_address   = "[email protected]"; // Replace your email address

    $smtp_address   = "[email protected]";     // Replace your email GMail address
    $smtp_password  = "password";               // Replace your email password
    $smtp_server    = "smtp.gmail.com"; // Replace your email server address


    /* Contact Form Setup End */

    date_default_timezone_set('Etc/UTC');
    require '../inc/phpmailer/PHPMailerAutoload.php';

    $mail = new phpmailer(true);

    try{

        // $mail->SMTPDebug = 2;

        $mail->isSMTP();

        $mail->Host = $smtp_server;

        $mail->SMTPAuth = true;

        $mail->Username = $smtp_address;

        $mail->Password = $smtp_password;

        $mail->SMTPSecure = 'tls';

        $mail->Port = 587;

        // Recipients

        $mail->setFrom($smtp_address, $send_title);

        $mail->addAddress($send_address);

        $mail->addReplyTo($send_address);

        // Content

        $mail->isHTML(true);

        $mail->Subject = $send_title;

        $Body = "";

        $Body .= "Name: ";

        $Body .= $name;

        $Body .= "<br>";

        $Body .= "Email: ";

        $Body .= $email;

        $Body .= "<br>";

        $Body .= "Message: ";

        $Body .= $message;

        $Body .= "<br>";

        $Body .= "Phone: ";

        $Body .= $phone;

        $mail->Body = $Body;

        $mail->send();

        echo 'Message has been send!';

    } catch (Exception $e){

        // echo 'Message could not be send. Error: ', $mail->ErrorInfo;

        echo 'Message could not be send. Error: ';

    }
?>
 

edenwax

Active Coder
I try this one, but dont work. but i think its close.

Code:
<?php
$errorMSG = "";

// NAME

if (empty($_POST["name"])) {

    $errorMSG = "Name is required ";

} else {

    $name = $_POST["name"];

}

// Phone

if (empty($_POST["phone"])) {

    $errorMSG = "Phone is required ";

} else {

    $phone = $_POST["phone"];
}


// EMAIL

if (empty($_POST["email"])) {

    $errorMSG .= "Email is required ";

} else {

    $email = $_POST["email"];

}

// MESSAGE

if (empty($_POST["message"])) {

    $errorMSG .= "Message is required ";

} else {

    $message = $_POST["message"];

}

/* Contact Form Setup Begin */

    $send_name      = "No Name";      // Replace your name
    $send_title     = "ZAKAZ";        // Replace email sent title
    $send_address   = "[email protected]"; // Replace your email address

    $smtp_address   = "[email protected]";     // Replace your email GMail address
    $smtp_password  = "password";               // Replace your email password
    $smtp_server    = "smtp.gmail.com"; // Replace your email server address


    /* Contact Form Setup End */

    date_default_timezone_set('Etc/UTC');
    require '../inc/phpmailer/PHPMailerAutoload.php';

    $mail = new phpmailer(true);

    try{

        // $mail->SMTPDebug = 2;

        $mail->isSMTP();

        $mail->Host = $smtp_server;

        $mail->SMTPAuth = true;

        $mail->Username = $smtp_address;

        $mail->Password = $smtp_password;

        $mail->SMTPSecure = 'tls';

        $mail->Port = 587;

        // Recipients

        $mail->setFrom($smtp_address, $send_title);

        $mail->addAddress($send_address);

        $mail->addReplyTo($send_address);

        // Content

        $mail->isHTML(true);

        $mail->Subject = $send_title;

        $Body = "";

        $Body .= "Name: ";

        $Body .= $name;

        $Body .= "<br>";

        $Body .= "Email: ";

        $Body .= $email;

        $Body .= "<br>";

        $Body .= "Message: ";

        $Body .= $message;

        $Body .= "<br>";

        $Body .= "Phone: ";

        $Body .= $phone;

        $mail->Body = $Body;

        $mail->send();

        echo 'Message has been send!';

    } catch (Exception $e){

        // echo 'Message could not be send. Error: ', $mail->ErrorInfo;

        echo 'Message could not be send. Error: ';

    }
?>

What part doesnt work? Does PHP return any error?

Also, yeah a lot of hosting providers turn off PHP mail() because it can be exploited fairly easily.
 
Top Bottom