我有一個<textarea>
表單用於用戶註釋,並且當內容傳遞給表單郵件時,換行符被轉換爲空格。我如何保留表單用戶輸入的換行符?PHP - 表單郵件轉換換行符空格
相關PHP:
$comments = $_REQUEST['comments'];
// This grabs the comments from the submitted form
//...
$to = $configEmail;
$subject = "Website Order Received: $offer";
$contents = "blah blah blah...";
if (!empty ($comments)) {
$contents = $contents."\nComments: $comments\n\n";
}
//...
mail($to, $subject, $contents);
並在窗體的HTML結束...(註釋投入的形式,如果它有錯誤提交,所以數據不會丟失)
<label>Comments/Questions</label>
<textarea name="comments"><?php echo $comments; ?></textarea>
如果鍵入:
line 1
line 2
line 3
它仍然像如果形式與錯誤提交,所以$comments = $_REQUEST['comments'];
是d無限期地保留換行符。但純文本電子郵件給我:
line 1 line 2 line 3
我怎樣才能保持換行符?
如果您使用'\ r \ n'而不是'\ n',它看起來如何。可能只是您挑剔的電子郵件客戶端。 – 2012-01-03 19:44:53
我有'/ n'的任何地方都能正常工作。我的猜測是'$ comments'中的字符串沒有'/ n',因爲它的分行符會破壞電子郵件內容的其他部分。 – 2012-01-03 19:48:35
你是用純文本還是html發送郵件?我理解了第一個,但到目前爲止的答案表明第二個。總之,它是\ n,不是/ n – 2012-01-03 19:51:09