【发布时间】:2023-04-11 08:55:01
【问题描述】:
我有一个带有 <meta charset="utf-8" /> 的 php 表单(我也尝试使用 <meta charset="ISO-8859-1" /> 得到相同的结果)
当我使用字符 " 和 ' 测试我的表单时,我收到的电子邮件会将这些字符转换为它们的 ASCII 字符:
Subject: \"\'
Message: \"\'
Sent by: \"\'
(不知何故,主题字段只在字符前添加了\)
我还注意到< 和> 之间的任何内容都在$formAuthor 和$formContent 中被删除,但在$formSubject 中没有被删除
此外,如果在验证码中提交了 from 错误,则表单会将字段中写入的内容保留在内存中,但 $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>
【问题讨论】: