PHP 换行符似乎不起作用
我有以下使用 mail() 函数发送消息的代码。 一切工作正常,除了每行末尾的换行符似乎不起作用。 正如你所看到的,我正在使用“ \r\n ”,希望这会给我一个换行符,但事实并非如此,我添加了
来换行,但我宁愿如果有人没有 HTML 电子邮件客户端,请不要使用它。
<?php
$to = '[email protected]'; // Was a valid e-Mail address, see comment below
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
$content = 'Name: '.$name."\r\n";
$content .= 'Email: '.$email."\r\n";
$content .= 'Subject: '.$subject."\r\n";
$content .= 'Message: '.$message."\r\n";
$headers = 'MIME-Version: 1.0' ."\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' ."\r\n";
// Additional headers
$headers .= 'To: iVEC Help <[email protected]>'. "\r\n";
$headers .= 'From: '.$name.' <'.$email.'>' . "\r\n";
mail( $to, $subject, $content, $headers);
?>
<p> You sent it ... good work buddy </p>
<p> <?php '$name' ?> </p>
I have the following code that sends a message using the mail() function. Everything works fine except the line breaks at the end of each line don't seem to work. As you can see I am using " \r\n " hoping that this will give me a line break but it doesn't I have added <br>
to get the break, but I would rather not use that in case someone doesn't have an HTML email client.
<?php
$to = '[email protected]'; // Was a valid e-Mail address, see comment below
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
$content = 'Name: '.$name."\r\n";
$content .= 'Email: '.$email."\r\n";
$content .= 'Subject: '.$subject."\r\n";
$content .= 'Message: '.$message."\r\n";
$headers = 'MIME-Version: 1.0' ."\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' ."\r\n";
// Additional headers
$headers .= 'To: iVEC Help <[email protected]>'. "\r\n";
$headers .= 'From: '.$name.' <'.$email.'>' . "\r\n";
mail( $to, $subject, $content, $headers);
?>
<p> You sent it ... good work buddy </p>
<p> <?php '$name' ?> </p>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不是这个领域的专家,但我的猜测是您将内容类型设置为 text/html,这意味着 html 渲染(这意味着换行符被转换为空格)。 如果您没有使用任何 HTML 元素(看起来没有),请尝试将内容类型设置为 text/plain。
I'm not an expert in this area, but my guess would be that you're setting the content type to text/html, which implies html-rendering (which means line breaks are translated to a space). If you're not using any HTML-elements (which appear not to), try setting the content type to text/plain.
如前所述,将其设置为 text/plain 或添加 HTML 换行符:
$content = '名称:'.$name."
\r\n";
As mentioned before, either set it to text/plain or add a HTML break for line breaks:
$content = 'Name: '.$name."</br>\r\n";
您将其作为 HTML 发送 - 将内容类型更改为
text/plain
它应该可以工作。You're sending it as HTML - change the content type to
text/plain
and it should work.问题是您在标题中说邮件是 HTML 电子邮件:
如果您想确保与 HTML 和文本客户端的兼容性,请考虑使用 Multipart 内容类型。 这可以通过多种不同的方式来实现,但一种简单的方法是使用可以为您完成此操作的库。 例如,您可以使用 PEAR:Mail_Mime
The problem is that you say, in your headers, that the mail is an HTML email:
If you want to ensure compability with both HTML and text clients, consider using the Multipart content type. This can be achieved in many different ways, but one simple way is to use a library that can do this for you. For example, you can use PEAR:Mail_Mime