1

I've been trying to create a simple contact form using PHP. So, I've managed to get the form to work by plugging in values for all variables like so and it sends the email just fine:

$to = "[email protected]";
$subject = "Job";
$message = "Test";
$from = "[email protected]";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);

However, as soon as I replace the values with $_REQUEST[var], the email gets sent to my Junk email with no from address:

$to = "[email protected]";
$subject = "Job";
$message = $_REQUEST['clientMessage'];
$from = $_REQUEST['clientEmail'];
    $headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $from";
mail($to,$subject,$message,$headers);

Screenshot of the email: enter image description here

I'm getting the values through an ajax form using jQuery:

    var clientName = $("#name").val();
    window.alert(clientName);
    var clientEmail = $("#email").val();    
    window.alert(clientEmail);
    var clientMessage = $("#message").val();            
    window.alert(clientMessage);
     var data = "from=" + clientEmail + "&subject=" + clientName + "&message=" + clientMessage;
     $.ajax({
            type: "POST",
            url: "email.php",
            data: data,
            success: function() {
                $("#loading").fadeOut(100).hide();
                $("#name").val(' ');
                $("#email").val(' ');
                $("#message").val(' ');
                $("#message-sent").fadeIn(100).show();
            }
        });

I've printed out clientName, clientMessage, and clientEmail all right before sending them into the ajax form and they all print out with correct values. Any idea what I'm doing wrong?

HTML form:

 <form id="contactMe" data-abide>
 <input type="text" id="name">
  <input type="text" id="email">        
  <textarea  id="message"></textarea>
 <button type="submit" id="sendEmail" class="button">Submit</button>
14
  • 1
    Where is data defined? Also, use phpMailer or another lib to send emails instead of mail(). Commented Aug 4, 2013 at 6:05
  • Try adding these $headers = 'MIME-Version: 1.0' . "\r\n"; and $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; certain web sites will mark it as Spam if proper headers are not included. Commented Aug 4, 2013 at 6:05
  • @DCoder, my apologies, I added in data now, though it still doesn't seem to work.@Fred, where would I add those? Commented Aug 4, 2013 at 6:09
  • @Andrew Have you tried it without the "from=" + clientEmail +? Seems like you already took care of that with $from = $_REQUEST['clientEmail']; Commented Aug 4, 2013 at 6:10
  • @Andrew You would add those underneath $headers = "From:" . $from;, yet you would need to continue concatenates in headers = 'MIME-Version: 1.0' . "\r\n"; to be $headers .= 'MIME-Version: 1.0' . "\r\n"; the dot concatenates etc. Commented Aug 4, 2013 at 6:12

2 Answers 2

3

You're using the wrong variable to read the client email. In your JS, you're pulling it into a variable called clientEmail, but then you're giving it the name from when passing the AJAX request:

var data = "from=" + clientEmail + "&subject=" + clientName + "&message=" + clientMessage;

So from within PHP, you should be accessing it as $_REQUEST['from'], not $_REQUEST['clientEmail'].

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that seem to did it!
Good catch Nick, definitely worth a +1 here, cheers. Glad someone figured it out.
1

This is only a "supplement" to the accepted answer (good catch Nick), since the Email was treated as Junk/Spam.

Adding proper headers such as:

$headers  = "From: $from";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

will help reduce the chance of E-mail ending up in Junk or treated as Spam.

Certain web sites will mark it as Spam if proper headers are not included.

More information on the mail() can be found on PHP.net

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.