简体   繁体   English

联系人表单和mail()函数中“发件人”字段的问题

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

I've got a contact form with 3 fields and a textarea... 我有一个包含3个字段和一个textarea的联系表...
I use jQuery to validate it and then php to send emails. 我使用jQuery对其进行验证,然后使用php发送电子邮件。

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: <nameOfMyServerAdmin@serverUrl.com> 现在,我将收到一个From字段,如下所示: <nameOfMyServerAdmin@serverUrl.com>

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. 例如,如果用户在名称字段中键入“ Matthew”,则希望此单词“ Matthew”出现在“发件人”字段中。

This is my code (XHTML, jQuery, PHP): 这是我的代码(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 -->
        <span>Name:</span>
        <input type="text" id="formName" value="" /><br />
        <span>E-mail:</span>
        <input type="text" id="formEmail" value="" /><br />
        <span>Subject:</span>
        <input type="text" id="formSubject" value="" /><br />
        <span>Message:</span>
        <textarea id="formMessage" rows="9" cols="20"></textarea><br />
        <input type="submit" id="formSend" value="Send" />
    </form>
</div>


<script type="text/javascript">
     $(document).ready(function(){
        $("#formSend").click(function(){

            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._-]+@[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").fadeIn("slow");
                $("#risposta").html("<span><b>Error:</b></span><br />"+valid);
                $("#risposta").css("background-color","#ffc0c0");
            }
            else {
                var datastr ='nome=' + nome + '&mail=' + mail + '&oggetto=' + oggetto + '&messaggio=' + encodeURIComponent(messaggio);
                $("#risposta").css("display", "block");
                $("#risposta").css("background-color","#FFFFA0");
                $("#risposta").html("<span>Sending message...</span>");
                $("#risposta").fadeIn("slow");
                setTimeout("send('"+datastr+"')",2000);
            }
            return false;
        });
    });
    function send(datastr){
        $.ajax({    
            type: "POST",
            url: "contactForm.php",
            data: datastr,
            cache: false,
            success: function(html) {
            $("#risposta").fadeIn("slow");
            $("#risposta").html('<span>Message successfully sent.</span>');
            $("#risposta").css("background-color","#e1ffc0");
            setTimeout('$("#risposta").fadeOut("slow")',2000);
            }
        });
    }
    </script>

<?php
$mail = $_POST['mail'];
$nome = $_POST['nome'];
$oggetto = $_POST['oggetto'];
$text = $_POST['messaggio'];
$ip = $_SERVER['REMOTE_ADDR'];
$to = "support@matthewlabs.com";

$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);
?>

You should be using an email address in the "From:" header. 您应该在“发件人:”标题中使用电子邮件地址。 Try this: 尝试这个:

$headers = "From: $nome <$mail>\r\n";
$headers .= "Reply-To: $mail\r\n";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM