【问题标题】:PHP contact form change HTML entity codes into ASCII charactersPHP 联系表单将 HTML 实体代码更改为 ASCII 字符
【发布时间】: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>

【问题讨论】:

    标签: php html utf-8 ascii


    【解决方案1】:

    试试这个

    $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
    }
    

    【讨论】:

    • 非常感谢您对 Mdesdev 的帮助!我尝试了您的提议,但这并没有改变我接收电子邮件的方式。我还注意到&lt;&gt; 之间的任何内容都在$formAuthor 和$formContent 中被删除,但在$formSubject 中没有。知道是否相关吗?
    • 感谢您的更新。我确实尝试过(以及 body 替代方案),但它并没有改变我接收邮件的方式:仍然在 $formSubject 中的 " 和 ' 之前获得 \ 并且它们在 $formAuthor 中更改为它们的 ASCII 字符和$formContent。仍然在 $formAuthor 和 $formContent 中删除了 之间的任何内容。我还更新了我的问题中的 php 代码,也许它会对您有更多帮助。再次感谢您的帮助!
    • 不客气,很抱歉我帮不上忙,但请查看PHP Mail Manual,也许这会帮助您解决问题。
    • 感谢您的回复。我注意到一个新问题:当从验证码中提交错误时,表单会保留在该字段中写入的内容,但 $formAuthor$formSubject 中的 " 之后的任何内容都被删除,但 @987654327 中没有@(这很奇怪,因为它确实遵循与电子邮件问题相同的逻辑)。感谢您的链接,我要去研究它!
    猜你喜欢
    • 2012-01-15
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多