2

从代码中可以看出,用户首先输入姓名和消息,然后单击发送。问题是文本区域。当我换行时,我收到的电子邮件中仍然没有任何换行符。

<?php
$to = "abc@xyz.com";
$subject = "Message from contact form";

//begin of HTML message
$message = <<<EOF
<html>
<body>
<b>Name:</b><br>
{$_POST['name']}
<br>
<b>Message:</b><br>
{$_POST['message']}
</body>
</html>
EOF;
//end of message

$header  = "MIME-Version: 1.0" . "\r\n";
$header .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$header .= "from:abc@xyz.com";

mail($to, $subject, $message, $header);
echo "Email sent!";
?>

<form action="sendmail.php" method="post">
Name: <br>
<input type="text" name="name" style="width: 200px;"> <br><br>

Message: <br>
<textarea name="message" style="width: 200px; height: 100px;"></textarea> <br><br>

<input type="submit" value="Send">
</form>
4

3 回答 3

14

这就是 HTML 的本意。无论您Enter在 HTML 源代码中点击多少次,浏览器只会在有 HTML 标记告诉它这样做时才显示换行符。

您可以使用nl2br()插入此类标签。您还想使用htmlspecialchars()转义原始输入。

$name = htmlspecialchars($_POST['name']);
$message = nl2br(htmlspecialchars($_POST['message']));

//begin of HTML message
$message = <<<EOF
<html>
<body>
<b>Name:</b><br>
{$name}
<br>
<b>Message:</b><br>
{$message}
</body>
</html>
EOF;
于 2013-04-01T16:40:45.330 回答
2

用这个:-

echo nl2br("My name is\n user2232809");

将输出

我的名字是
user2232809

此函数返回<br /> or <br>在所有换行符之前插入的字符串。因此,当您按 Enter 时,它会返回它的 html 等效项<br>

于 2013-04-01T16:49:28.497 回答
0

在 textarea 的文本上使用 PHP 的 nl2br()....

于 2013-04-01T16:40:35.527 回答