定义标题中断后不再起作用
在我使用 UTF-8 定义 PHP 标头后,我对 $msg 的中断不再起作用。 问题出在哪里?还有其他办法可以休息吗?
谢谢!
代码:
<?php
$username = $_POST['name'];
$lastname = $_POST['lastname'];
$useremail = $_POST['mail'];
$fon = $_POST['phone'];
$usermsg = $_POST['nachricht'];
$myemail = "Mail@XY";
$msg = "Antwort an: $useremail\n". "Name: $username $lastname \n". "Telefonnummer: $fon \n". "Nachricht: $usermsg";
$subject = "Neue Nachricht vom Kontaktformular";
$headers = 'Content-type: text/html; charset=utf-8'. "\r\n"
.'From: ' . $myemail . "\r\n";
if(empty($username)||empty($useremail)||empty($usermsg)){
header("location:index.html?error");
}
else{
$to = "Mail@XY";
mail($to, $subject, $msg, $headers);
}
?>
after I define a PHP header with UTF-8 my breaks on $msg don't work anymore.
Where is the problem? Is there another way to make breaks?
Thanks!
Code:
<?php
$username = $_POST['name'];
$lastname = $_POST['lastname'];
$useremail = $_POST['mail'];
$fon = $_POST['phone'];
$usermsg = $_POST['nachricht'];
$myemail = "Mail@XY";
$msg = "Antwort an: $useremail\n". "Name: $username $lastname \n". "Telefonnummer: $fon \n". "Nachricht: $usermsg";
$subject = "Neue Nachricht vom Kontaktformular";
$headers = 'Content-type: text/html; charset=utf-8'. "\r\n"
.'From: ' . $myemail . "\r\n";
if(empty($username)||empty($useremail)||empty($usermsg)){
header("location:index.html?error");
}
else{
$to = "Mail@XY";
mail($to, $subject, $msg, $headers);
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
造成这种情况的不是
utf-8
,而是text/html
。您告诉接收邮件客户端将电子邮件解析并显示为 HTML 文档。正如您希望知道的,在 HTML 中,使用 指定换行符
标签。 HTML 渲染引擎不会注意到\n
或\r\n
- 这些仅在纯文本文档中有用。这:
应该修复它。
It's not the
utf-8
which has caused this, it's thetext/html
.You're telling the receiving mail client to parse and display the email as a HTML document. As you hopefully know, in HTML a line break is specified using the
<br>
tag. HTML rendering engines do not take any notice of\n
or\r\n
- these are only useful in plain-text documents.This:
should fix it.
如果您的目标是 HTML 邮件格式,则
$msg
中的\n
必须替换为 HTML 标记
。如果您的目标是文本邮件格式,请尝试 PHP_EOL 或
\r\n
而不是$msg
中的\n
。If you aim HTML-mail format,
\n
s in the$msg
must be replaced with the HTML tag<br>
.If you aim text-mail format then try PHP_EOL or
\r\n
instead of\n
in your$msg
.