0

I've created a simple AJAX, Jquery, PHP contact form on my website with inputs: Name, Email and Message. The problem is that when you write an actual email into the email field the message never comes to my inbox. It works only when the Email input field contains 1 word with no @ sign.

My HTML:

<p class="form">Name</p>
<input type="text" name="userName" id="userName">
<p class="form">Email</p>
<input id="userEmail" type="email" name="userEmail">
<p class="form">Message</p>
<textarea id="msg" name="msg"></textarea><button onClick="sendContact();"></button>

My JavaScript:

function sendContact() {

    jQuery.ajax({
        url: "contact_me.php",
        data:'userName='+$("#userName").val()+'&userEmail='+
        $("#userEmail").val()+'&msg='+
        $("#msg").val(),
        type: "POST",
        success:function(){
            sendSuccess();
        },
        error:function (){}
    });
};

My PHP:

<?php
$to = "dusset@gmail.com";
$from = $_POST['userEmail'];
$name = $_POST['userName'];
$subject = $name . "has sent you a message from .design";
$message = $name . " wrote the following:" . "\n\n" . $_POST['msg'];
$headers = "From:" . $from;

mail($to,$subject,$message,$headers);?>

Any idea what could be the problem, please?

user1207524
  • 251
  • 2
  • 12
  • 27
  • possible duplicate of [What's wrong with my AJAX contact form? (php mail())](http://stackoverflow.com/questions/4260455/whats-wrong-with-my-ajax-contact-form-php-mail) – Cwt May 11 '15 at 22:45
  • Checked that article, none of it seems to have solved my issue. – user1207524 May 11 '15 at 22:50
  • Please check the link:-http://stackoverflow.com/questions/24644436/php-mail-form-doesnt-complete-sending-e-mail – Alive to die - Anant May 11 '15 at 22:56
  • Could it be due to the fact that you're trying to spoof the from address? – PeterKA May 11 '15 at 22:56
  • Did you try echo or error_log the user email to see what you get if a @ is included? You may have to encode it. – Taplar May 11 '15 at 23:02
  • possible duplicate of [php mail() not sending headers when using phonegap and ajax](http://stackoverflow.com/questions/11006908/php-mail-not-sending-headers-when-using-phonegap-and-ajax) – Cwt May 11 '15 at 23:04
  • @Taplar - I did and it is included, I've also tried doing simply `$headers = "From: email@example.com"` but then it didn't work either, but with `$headers = "From: test"` it works. Basically as soon as the @ is involved it stops working. – user1207524 May 11 '15 at 23:10

1 Answers1

1

Try changing how you pass the data to Ajax. An example:

function sendContact() {
    jQuery.ajax({
        url: "contact_me.php",
        data: { userName: $("#userName").val(), userEmail: $("#userEmail").val(), msg:  $("#msg").val()},
        type: "POST",
        success:function(){
            sendSuccess();
        },
        error:function (){}
    });
};
Mackan
  • 6,200
  • 2
  • 25
  • 45
Jesus Cuesta
  • 175
  • 4
  • This will correctly encode post values, which was the original issue. `data` should be object, string or array and `it is converted to a query string, if not already a string`. – skobaljic May 12 '15 at 08:12