0

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>"; 
}
?> 
4

2 回答 2

1

您没有$nome在代码中的任何地方声明,也没有$mail在:

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

除此之外,正如此评论所提到的,您没有将标头传递给邮件功能。见邮件函数的第四个参数。

于 2012-09-07T13:48:05.777 回答
1

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

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

但是当你打电话时它不使用mail()

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

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

mail($to, $subject, $body, $headers);
于 2012-09-07T14:05:40.723 回答