Problem with From field in contact form and mail() function

Posted by Matthew on Stack Overflow See other posts from Stack Overflow or by Matthew
Published on 2010-12-28T18:30:44Z Indexed on 2011/01/17 19:53 UTC
Read the original article Hit count: 198

Filed under:

I've got a contact form with 3 fields and a textarea...
I use jQuery to validate it and then php to send emails.

This contact form works fine but, when I receive an email, From field isn't correct. I'd like to want that From field shows text typed in the Name field of the contact form. Now I get a From field like this: <[email protected]>

For example, if an user types "Matthew" in the name field, I'd like to want that this word "Matthew" appears in the From field.

This is my code (XHTML, jQuery, PHP):

<div id="contact">
    <h3 id="formHeader">Send Us a Message!</h3>
    <form id="contactForm" method="post" action="">
        <div id="risposta"></div> <!-- End Risposta Div -->
        <input type="text" id="formName" value="" /><br />
        <input type="text" id="formEmail" value="" /><br />
        <input type="text" id="formSubject" value="" /><br />
        <textarea id="formMessage" rows="9" cols="20"></textarea><br />
        <input type="submit" id="formSend" value="Send" />

<script type="text/javascript">

            var valid = '';
            var nome = $("#formName").val();
            var mail = $("#formEmail").val();
            var oggetto = $("#formSubject").val();
            var messaggio = $("#formMessage").val();

            if (nome.length<1) {
                valid += '<span>Name field empty.</span><br />';
            if (!mail.match(/^([a-z0-9._-][email protected][a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
                valid += '<span>Email not valid or empty field.</span><br />';
            if (oggetto.length<1) {
                valid += '<span>Subject field empty.</span><br />';

            if (valid!='') {
                $("#risposta").html("<span><b>Error:</b></span><br />"+valid);
            else {
                var datastr ='nome=' + nome + '&mail=' + mail + '&oggetto=' + oggetto + '&messaggio=' + encodeURIComponent(messaggio);
                $("#risposta").css("display", "block");
                $("#risposta").html("<span>Sending message...</span>");
            return false;
    function send(datastr){
            type: "POST",
            url: "contactForm.php",
            data: datastr,
            cache: false,
            success: function(html) {
            $("#risposta").html('<span>Message successfully sent.</span>');

$mail = $_POST['mail'];
$nome = $_POST['nome'];
$oggetto = $_POST['oggetto'];
$text = $_POST['messaggio'];
$to = "[email protected]";

$message = $text."<br /><br />IP: ".$ip."<br />";
$headers = "From: $nome \n";
$headers .= "Reply-To: $mail \n";
$headers .= "MIME-Version: 1.0 \n";
$headers .= "Content-Type: text/html; charset=UTF-8 \n";

mail($to, $oggetto, $message, $headers);

© Stack Overflow or respective owner

Related posts about php

Related posts about jQuery