PHP联系表单将HTML实体代码更改为ASCII字符

时间:2013-12-10 16:37:28

标签: php html utf-8 ascii

我有一个带有<meta charset="utf-8" />的php表单(我也试过<meta charset="ISO-8859-1" />给了我相同的结果)

当我使用字符"'测试表单时,我收到的电子邮件会将这些字符转换为ASCII字符:

Subject: \"\'   

Message: \&#34;\&#39;

Sent by: \&#34;\&#39;

(不知何故,主题字段只在字符前添加\

我还注意到<>之间的任何内容都在$formAuthor$formContent中被删除,但在$formSubject

中没有删除

此外,如果在验证码中提交了一个错误,则表单会在内存中保留已写入字段的内容,但“$formAuthor$formSubject中的任何内容都会被删除但不会在$formContent(这很奇怪,因为它的确遵循与电子邮件问题相同的逻辑)

这不是服务器问题,因为我这个表单只有这个问题,而不是其他问题。

非常感谢你的帮助!

这是php表单的一部分:

// if all the fields have been entered correctly and there are no recaptcha errors build an email message
    if (($resp->is_valid) && (!isset($hasError))) {
    $emailTo = 'contact@website.com'; // here you must enter the email address you want the email sent to
    $subject = 'Message from: ' . $formAuthor . ' | ' . $formSubject;  // This is how the subject of the email will look like
    $body = "Email: $formEmail  \n\nSubject: $formSubject \n\nMessage: $formContent  \n\nSent by: $formAuthor";// This is the body of the email
    $headers = 'From: <'.$formEmail.'>' . "\r\n" . 'Reply-To: ' . $formEmail . "\r\n" . 'Return-Path: ' . $formEmail; // Email headers

//send email
    mail($emailTo, $subject, $body, $headers);

// set a variable that confirms that an email has been sent
    $emailSent = true;
            }
$emailSent = true;
}
// if there are errors in captcha fields set an error variable
if (!($resp->is_valid)){
$captchaErrorMsg = true;
}
}
} ?>

这是HTML:

<div id="singleParagraphInputs">
<div>
<label for="formAuthor">Name</label>
<input class="requiredField <?php if($authorError) { echo 'formError'; } ?>" type="text" name="formAuthor" id="formAuthor" value="<?php if(isset($_POST['formAuthor']))  echo $_POST['formAuthor'];?>" size="40" />
</div>
<div>
<label for="formEmail">Email</label>
<input class="requiredField <?php if($emailError) { echo 'formError'; } ?>" type="text" name="formEmail" id="formEmail" value="<?php if(isset($_POST['formEmail']))  echo $_POST['formEmail'];?>" size="40" />
</div>
<div>
<label for="formSubject">Subject</label>
<input type="text" name="formSubject" id="formSubject" value="<?php if(isset($_POST['formSubject']))  echo $_POST['formSubject'];?>" size="40" />
</div>
<div id="commentTxt">
<label for="formContent">Message</label>
<textarea class="requiredField <?php if($commentError) { echo 'formError'; } ?>" id="formContent" name="formContent" cols="40" rows="5"><?php if(isset($_POST['formContent'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['formContent']); } else { echo $_POST['formContent']; } } ?></textarea>
</div>

1 个答案:

答案 0 :(得分:0)

试试这个

$emailTo  = 'contact@website.com';

$subject  = "Message from: " . ' <'. $formEmail .'> ' . "|" . ' <'. $formSubject .'> ';

$body = "Email: " . ' <'. $formEmail .'> ' . "\n\n Subject: " . ' <'. $formSubject .'> ' . "\n\n Message: " . ' <'. $formContent .'> ' . "\n\n Sent by: " . ' <'. $formAuthor .'> ';
// Body alternative $body = "Email: " . ' <'. $formEmail .'> ' . "\n\n" . " Subject: " . ' <'. $formSubject .'> ' . "\n\n" . "Message: " . ' <'. $formContent .'> ' . "\n\n" . "Sent by: " . ' <'. $formAuthor .'> ';

$headers  = "From: " . ' <'. $formEmail .'> ' . "\r\n";
$headers .= "Reply-To: " . ' <'. $formEmail .'> ' . "\r\n";
$headers .= "Return-Path: " . ' <'. $formEmail .'> ' . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/plain; charset=utf-8" . "\r\n";

$mail     = mail($emailTo, $subject, $body, $headers);

if($mail) {
  // If success
}
else {
  // If fail
}