我有一个带有<meta charset="utf-8" />
的php表单(我也试过<meta charset="ISO-8859-1" />
给了我相同的结果)
当我使用字符"
和'
测试表单时,我收到的电子邮件会将这些字符转换为ASCII字符:
Subject: \"\'
Message: \"\'
Sent by: \"\'
(不知何故,主题字段只在字符前添加\
)
我还注意到<
和>
之间的任何内容都在$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>
答案 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
}