在 PHP 邮件函数的 $body 中创建一个新行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8782119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating A New Line In $body Of PHP Mail Function
提问by MillerMedia
I've got the PHP mail(): function working for a form I'm using on my site. I am now trying to format the $body attribute to look nicer and more organized when I receive the subsequent e-mail.
我有 PHP mail(): 函数为我在我的网站上使用的表单工作。我现在正在尝试格式化 $body 属性,以便在收到后续电子邮件时看起来更好、更有条理。
I've tried \n and I've tried
and both give me
我试过\n,我试过
,都给我
Here's the snippet of the code that I'm working with (I think it's just a matter of syntax that I'm doing wrong):
这是我正在使用的代码片段(我认为这只是我做错的语法问题):
if(!empty($brandname) && !empty($firstname) && !empty($lastname) && !empty($email) && !empty($goals) && !empty($bio)){
$to = '[email protected]';
$subject = 'Submission Form';
$body = 'Brand Name: '.$brandname.'<br />Name: '.$firstname.' '.$lastname.'<br />Email: '.$email.'<br />About the Company:'.$bio;
$headers = 'From: '.$email;
It's just displayed the <br />
as text in the e-mail I receive. It was the same when I used \n (which I'm assuming is actually the right way to do it). What am I doing wrong? Thanks.
它只是<br />
在我收到的电子邮件中显示为文本。当我使用 \n 时也是如此(我认为这实际上是正确的做法)。我究竟做错了什么?谢谢。
采纳答案by Abraham Covelo
You must place \n
in double quotes "
not between single quotes '
If you want that this sequence would be interpreted as a new line (line feed 0x0A) character
您必须放在\n
双引号中"
而不是单引号之间'
如果您希望此序列被解释为换行符(换行符 0x0A)
Take a look at this php documentation:
看看这个php文档:
回答by Ana El Bembo
(PHP 4, PHP 5)
(PHP 4, PHP 5)
$body = 'Brand Name: '.$brandname."\r\n".'Name: '.$firstname.' '.$lastname."\r\n".'Email: '.$email."\r\n".'About the Company:'.$bio;
Use "\r\n"
for newline as above
使用"\r\n"
了换行符如上
回答by MichD
From the manual page of the mail() function, we learn that in order to send HTML emails, extra headers need to be said. This is concisely demonstrated in Example #4.
从mail() 函数的手册页中,我们了解到为了发送 HTML 电子邮件,需要说明额外的标题。这在示例 #4 中得到了简洁的演示。
However, for just line breaks, I wouldn't advise using HTML. For that, simply insert "\n" in the string, making sure to use double quotes, as mentioned by Abraham.
但是,对于换行符,我不建议使用 HTML。为此,只需在字符串中插入“\n”,确保使用双引号,如亚伯拉罕所述。
回答by Mohammad Saqib
use "<br>"
instead of \n
it will work 100 % my discovery
使用 "<br>"
而不是\n
它会 100 % 我的发现
e.g
例如
$subject="Mail from customer ".$_POST['Name'].`"<br>"`;
enjoy :)
请享用 :)
回答by Mohammad Saqib
easiest way for this
最简单的方法
$newline = '
';
use the $newline variable anywhere you want (:
在任何地方使用 $newline 变量(:
回答by Sidupac
I couldn't add a comment to Mohammad Saqib's answer, but I wanted to say that you shouldn't use <br>
as a replacement for \r\n
because this may cause the email to be caught by 'line too long' spam filters. New lines are important to keep the email more human and allow successful delivery!
我无法对 Mohammad Saqib 的回答添加评论,但我想说您不应该将其<br>
用作替代品,\r\n
因为这可能会导致电子邮件被“行太长”垃圾邮件过滤器捕获。新行对于保持电子邮件更人性化并成功交付很重要!
The method I recommend is:
我推荐的方法是:
$output[] = "line 1";
$output[] = "line 2";
$EmailBody = join("\r\n",$output);
Also you must make sure you 'sanitize' headers to avoid header injection from user input, here is a function that is double sure to stop injection:
此外,您必须确保“清理”标头以避免用户输入的标头注入,这是一个双重确保停止注入的功能:
function sanitize(&$data){
return str_ireplace(array("\r", "\n", "%0a", "%0d"), '', stripslashes($data));
}
$headers = 'From: '.sanitize($email);
If you wanted HTML tags to show in the email, change the type using the header:
如果您希望在电子邮件中显示 HTML 标签,请使用标题更改类型:
$headers = 'Content-type: text/html;\r\nFrom: '.sanitize($email);
If you did display HTML, you should clean the user input to avoid anything unexpected... i.e. HTML injection. Use this function anywhere you display user input in HTML:
如果你确实显示了 HTML,你应该清理用户输入以避免任何意外......即 HTML 注入。在您以 HTML 格式显示用户输入的任何位置使用此函数:
function htmlclean($str){
return htmlentities($str, ENT_QUOTES);
}
e.g. $body = "Brand Name: " . htmlclean($brandname);
例如 $body = "品牌名称:"。htmlclean($brandname);