我有一个包含3个字段和textarea的联系表格。
我使用jQuery来验证它然后php发送电子邮件。
此联系表单正常,但是当我收到电子邮件时,“发件人”字段不正确。我想要在“字段”中显示在联系表单的“名称”字段中键入的文本。现在我得到一个这样的From字段:<nameOfMyServerAdmin@serverUrl.com>
例如,如果用户在名称字段中键入“Matthew”,我希望在“发件人”字段中显示“Matthew”这个词。
这是我的代码(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);
?>
答案 0 :(得分:3)
您应该在“发件人:”标题中使用电子邮件地址。试试这个:
$headers = "From: $nome <$mail>\r\n";
$headers .= "Reply-To: $mail\r\n";