-1

Explanation

I download this website template from themefisher, but I can't get the contact form to work. I changed the $to section in the php file to my email address, so I can receive it; but after filling the form and sending it, even thought I got a success message, there's nothing in my inbox or junk.


Code

You can see the live preview here; or download the template here.

contact_me.js

$(function() {

    $("input,textarea").jqBootstrapValidation({
        preventSubmit: true,
        submitError: function($form, event, errors) {
            // additional error messages or events
        },
        submitSuccess: function($form, event) {
            event.preventDefault(); // prevent default submit behaviour
            // get values from FORM
            var name = $("input#name").val();
            var email = $("input#email").val();
            var phone = $("input#phone").val();
            var message = $("textarea#message").val();
            var firstName = name; // For Success/Failure Message
            // Check for white space in name for Success/Fail message
            if (firstName.indexOf(' ') >= 0) {
                firstName = name.split(' ').slice(0, -1).join(' ');
            }
            $.ajax({
                url: "././mail/contact_me.php",
                type: "POST",
                data: {
                    name: name,
                    phone: phone,
                    email: email,
                    message: message
                },
                cache: false,
                success: function() {
                    // Success message
                    $('#success').html("<div class='alert alert-success'>");
                    $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-success')
                        .append("<strong>Your message has been sent. </strong>");
                    $('#success > .alert-success')
                        .append('</div>');

                    //clear all fields
                    $('#contactForm').trigger("reset");
                },
                error: function() {
                    // Fail message
                    $('#success').html("<div class='alert alert-danger'>");
                    $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later!");
                    $('#success > .alert-danger').append('</div>');
                    //clear all fields
                    $('#contactForm').trigger("reset");
                },
            })
        },
        filter: function() {
            return $(this).is(":visible");
        },
    });

    $("a[data-toggle=\"tab\"]").click(function(e) {
        e.preventDefault();
        $(this).tab("show");
    });
});


/*When clicking on Full hide fail/success boxes */
$('#name').focus(function() {
    $('#success').html('');
});

contact_me.php

<?php
// Check for empty fields
if(empty($_POST['name'])        ||
   empty($_POST['email'])       ||
   empty($_POST['phone'])       ||
   empty($_POST['message']) ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
    echo "No arguments Provided!";
    return false;
   }

$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];

// Create the email and send the message
$to = 'myemail@gmail.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form:  $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: noreply@yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
Luiz
  • 1,275
  • 4
  • 19
  • 35
  • What does the `mail()` function return? Could you check if `true` or `false`? This indicates if the mail has successfully been delivered. – nicovank Oct 23 '16 at 03:20
  • I opened the website on Chrome, opened the JavaScript console, filled the contact form and sent. Now it shows a error message, as you can see [here](http://imgur.com/Ju30zOl) (the console log is also on the screenshot). – Luiz Oct 23 '16 at 03:46
  • Sorry if I made any mistake, I'm really new to js, so I don't know much about it. – Luiz Oct 23 '16 at 03:46
  • This error in your console is because your loading your page from your file system, not your local server. Open `localhost` on your browser and try again. – nicovank Oct 23 '16 at 03:47
  • If I'm doing the right thing, the console kept blank, as you can see [here](http://imgur.com/VLPks1R). I used CodeKit to generate this url. – Luiz Oct 23 '16 at 04:00
  • In your PHP file: `$sent = mail(....);` and then `echo $sent ? 'true' : 'false';` Then in your browser console, open the network tab, and check the last entry (which should be "contact_me.php"), and see if the response from the server is true or false. – nicovank Oct 23 '16 at 04:04
  • I did as you said and I got [this message](http://i.stack.imgur.com/bHCeH.png) when clicking in `contact_me.php`. I couldn't find any true or false. – Luiz Oct 23 '16 at 04:20

1 Answers1

1

You need an external server to send an email. It is not possible to send an email on a local server using PHP.

nicovank
  • 3,157
  • 1
  • 21
  • 42
  • Could you tell me how do I create it? or maybe some links so I can look for it? – Luiz Oct 23 '16 at 04:29
  • Maybe you could look for free PHP hosting, just to host this page and see if it works. I don't know any platforms that offer free service, but I know some exist. – nicovank Oct 23 '16 at 04:32
  • There isn't any way to do it with my computer only? I mean, host this page in a server running on my computer (which I have no idea if is possible). – Luiz Oct 23 '16 at 04:37
  • I then redirect you to this post: http://stackoverflow.com/questions/5342822/php-mail-function-on-localhost – nicovank Oct 23 '16 at 04:47