定义标题中断后不再起作用

发布于 2025-01-13 10:25:07 字数 747 浏览 0 评论 0原文

在我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔少女心 2025-01-20 10:25:07

造成这种情况的不是 utf-8,而是 text/html

您告诉接收邮件客户端将电子邮件解析并显示为 HTML 文档。正如您希望知道的,在 HTML 中,使用 指定换行符
标签
。 HTML 渲染引擎不会注意到 \n\r\n - 这些仅在纯文本文档中有用。

这:

$msg = "Antwort an: $useremail<br>". "Name: $username $lastname <br>". "Telefonnummer: $fon <br>". "Nachricht: $usermsg";

应该修复它。

It's not the utf-8 which has caused this, it's the text/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:

$msg = "Antwort an: $useremail<br>". "Name: $username $lastname <br>". "Telefonnummer: $fon <br>". "Nachricht: $usermsg";

should fix it.

自我难过 2025-01-20 10:25:07
$msg = "Antwort an: $useremail\n". "Name: $username $lastname \n". "Telefonnummer: $fon \n". "Nachricht: $usermsg";

如果您的目标是 HTML 邮件格式,则 $msg 中的 \n 必须替换为 HTML 标记

$msg = "Antwort an: $useremail<br>". "Name: $username $lastname<br>". "Telefonnummer: $fon<br>". "Nachricht: $usermsg";

如果您的目标是文本邮件格式,请尝试 PHP_EOL\r\n 而不是 $msg 中的 \n

$msg = "Antwort an: $useremail\n". "Name: $username $lastname \n". "Telefonnummer: $fon \n". "Nachricht: $usermsg";

If you aim HTML-mail format, \ns in the $msg must be replaced with the HTML tag <br>.

$msg = "Antwort an: $useremail<br>". "Name: $username $lastname<br>". "Telefonnummer: $fon<br>". "Nachricht: $usermsg";

If you aim text-mail format then try PHP_EOL or \r\n instead of \n in your $msg.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文