联系表单上的字段中的电子邮件地址未在电子邮件标题中显示为“来自”

时间:2012-09-07 13:45:50

标签: php forms field

iv在表单代码中添加了标题字段,但是当我收到电子邮件时,电子邮件中的“发件人”字段显示乱码,而不是发送邮件的人的电子邮件地址。这是我的消息处理代码:

<?php session_start();
if(isset($_POST['Submit'])) {   if( $_SESSION['chapcha_code'] == $_POST['chapcha_code'] && !empty($_SESSION['chapcha_code'] ) ) {
$youremail = 'I removed my address for privacy on stack overflow';
$fromsubject = 'A message from your website';
$title = $_POST['title'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$mail = $_POST['mail'];
$address = $_POST['address']; 
$city = $_POST['city']; 
$zip = $_POST['zip']; 
$country = $_POST['country']; 
$phone = $_POST['phone']; 
$subject = $_POST['subject']; 
$message = $_POST['message']; 
$headers = "From: $nome <$mail>\r\n";

    $to = $youremail; 
    $mailsubject = 'Message received from'.$fromsubject.' Contact Page';
    $body = $fromsubject.'

    The person that contacted you is  '.$fname.' '.$lname.'
     Address: '.$address.'
    '.$city.', '.$zip.', '.$country.'
     Phone Number: '.$phone.'
     E-mail: '.$mail.'
     Subject: '.$subject.'

     Message: 
     '.$message.'

    |---------END MESSAGE----------|'; 
echo "Thank you for your feedback. I will contact you shortly if needed.<br/>Go to <a href='/index.php'>Home Page</a>"; 
                                mail($to, $subject, $body);
        unset($_SESSION['chapcha_code']);
   } else {
        echo 'Sorry, you have provided an invalid security code';
   }
 } else { 
echo "You must write a message. </br> Please go to <a href='/contact.php'>Contact Page</a>"; 
}
?> 

2 个答案:

答案 0 :(得分:1)

您尚未在代码中的任何位置声明$nome,也未在$mail中声明:

$headers = "From: $nome <$mail>\r\n";

除此之外,正如this comment提到的那样,你没有将标题传递给mail函数。请参阅mail function的第四个参数。

答案 1 :(得分:1)

你在这里声明了你的^ From:header`:

$headers = "From: $nome <$mail>\r\n";

但是,当您拨打mail()

时,不会使用它
mail($to, $subject, $body);

您需要将$headers作为第四个参数传递。否则标题不会生效

mail($to, $subject, $body, $headers);