php 在php中使用mail()发送带有换行符的电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3786564/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:06:08  来源:igfitidea点击:

Send email with line breaks using mail() in php

php

提问by Prady

I am trying to send email using mail() in php. I need the message to be formatted or at least allow line breaks.

我正在尝试在 php 中使用 mail() 发送电子邮件。我需要格式化消息或至少允许换行。

$mail = mail(WEBMASTER_EMAIL, $subject, $message,
 "From: ".$name." <".$email.">/r/n"
 ."Reply-To: ".$email."/r/n"    
 ."X-Mailer: PHP/" . phpversion());

Do i need to provide "< br/>" tags in the $message or /r/n. Tried both but they came in as
or /r/n and not line breaks

我需要在 $message 或 /r/n.xml 中提供“< br/>”标签吗?两者都试过了,但它们以
或 /r/n 的形式出现,而不是换行符

Thanks Prady

谢谢普拉迪

回答by Gal

It's \r\n, as in backslash not forward slash.

它是 \r\n,如反斜杠而不是正斜杠。

Also you can try it like this:

你也可以这样尝试:

$message = "

Hi!

This is one line.

And this is another.



Bye!
";

回答by In81Vad0

you should set the content type of the mail (read: tell php you're sending a html e-mail) http://php.net/manual/en/function.mail.php

您应该设置邮件的内容类型(阅读:告诉 php 您正在发送 html 电子邮件) http://php.net/manual/en/function.mail.php

it's all explained in Example #5 Sending HTML email.

这在示例 #5 发送 HTML 电子邮件中进行了解释。

回答by sathia

because it's \r\n

因为它是\r\n

回答by Aura Binary

To format message body you should use \n, as follow:

要格式化消息正文,您应该使用 \n,如下所示:

$message = "Hi!
\nThis is one line.
\nAnd this is another.
\nBye!";

This is formatting your message body.

这是格式化您的邮件正文。

回答by Devendra Rajput

It's the solution I got after waisting too many time and It's working perfectly.

这是我在浪费太多时间后得到的解决方案,并且运行良好。

echo "line 1".PHP_EOL."line 2".PHP_EOL;

If you're planning to show the result in a browser then you have to use "<br>".

如果您打算在浏览器中显示结果,则必须使用"<br>".