简体   繁体   English

如何在PHP中使用段落发送消息

[英]How to send a message in php with paragraphs

I'm using the php code below to send a message 我正在使用下面的php代码发送消息

<?php
$newline = $_GET['message'];

$newline = str_replace("[N]","\n","$newline");
$newline = str_replace("[n]","\n","$newline");

mail($_GET['to'],$_GET['subject'],$newline,"From: ".$_GET['from']);

header( 'Location: http://my_site.com/POMPC/report.html' ) ;
?>

sadly, when I send the message, it appears but everything appears in a straignt line, messing up the message. 可悲的是,当我发送消息时,它出现了,但是所有内容都显示在一条清晰的线中,使消息混乱了。

Example: 例:

Hello,
This is a message. 
Reply.

Appears as 出现为

Hello,This is a message. 您好,这是一条消息。 Reply. 回复。

Everything is in a straignt line and messes up stuff. 一切都在一条直线上,把东西弄乱了。 How do I mantain the formatting. 如何维护格式。 The message I'm sending is from my desktop application and sends user defined data to me. 我发送的消息来自我的桌面应用程序,并向我发送用户定义的数据。

$_GET['message']获得的内容中使用<p> </p>标记;

Use nl2br($_GET['message']); 使用nl2br($_GET['message']);

Also, why are you using GET method? 另外,为什么要使用GET方法? POST is a better choice POST是更好的选择

You can create your message like this 您可以这样创建您的消息

$content=" <h3> Hello </h3>, <p>This is a message. <br/> Long string <br/>";

$content . = " <br/> Thanks <br/> Your Name";

Before everything else, make sure that your message contains correct line breaks where you expect them. 在进行其他所有操作之前,请确保您的消息在您期望的位置包含正确的换行符。 You can put this debugging code after the str_replace calls: 您可以将以下调试代码放在str_replace调用之后:

echo "<pre>$newline</pre>";

and see whether your text is broken up into lines. 并查看您的文字是否分成几行。 If not, then the issue is in your input. 如果不是,则问题出在您的输入中。

Next, it looks like your mail reader parses the message as HTML, hence everything is mangled together. 接下来,看起来您的邮件阅读器将邮件解析为HTML,因此所有内容都被合并在一起。 You are not specifying content-type explicitly, therefore it's up to each individual mail reader to decide how to interpret the message. 您没有明确指定内容类型,因此,由每个邮件阅读器来决定如何解释消息。 There are two solutions to your issue: 您的问题有两种解决方案:

(1) Declare the message as HTML and use <br/> to break up the lines: (1)将消息声明为HTML并使用<br/>拆分行:

<?php
$newline = $_GET['message'];

$newline = str_replace("[N]","\n","$newline");
$newline = str_replace("[n]","\n","$newline");
$newline = nl2br($newline);

$headers = "Content-type: text/html\r\nFrom: ".$_GET['from'];
mail($_GET['to'],$_GET['subject'],$newline,$headers);

header( 'Location: http://my_site.com/POMPC/report.html' ) ;
?>

(2) Declare the message as pure text and leave the line breaks as \\n : (2)将消息声明为纯文本,并将换行符保留为\\n

<?php
$newline = $_GET['message'];

$newline = str_replace("[N]","\n","$newline");
$newline = str_replace("[n]","\n","$newline");

$headers = "Content-type: text/html\r\nFrom: ".$_GET['from'];
mail($_GET['to'],$_GET['subject'],$newline,$headers);

header( 'Location: http://my_site.com/POMPC/report.html' ) ;
?>

Either of the two should produce the results your want. 两者都应产生您想要的结果。

If you want HTML paragraphs: 如果要HTML段落:

Using just the mail() function you would have to compose the HTML source like this: 仅使用mail()函数,您就必须像这样编写HTML源代码:

Do not write anything in the 3rd parameter $message , just leave it blank '' . 不要在第3个参数$message写入任何内容,只需将其留空'' And compose rest as such: 并这样构成休息:

$additional_headers = <<<MAIL
Content-Type: multipart/alternative; boundary=00151747835895c6f804c48dc14b

--00151747835895c6f804c48dc14b
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

    Pararaph 1
    Pararaph 2

--00151747835895c6f804c48dc14b
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

    <p>Pararaph 1</p>
    <p>Pararaph 2</p>

--00151747835895c6f804c48dc14b--
MAIL;

$additional_headers = str_replace("\n", "\r\n", $additional_headers);

Make sure the text in the plain/text area is separated by one or more \\r\\n to get a visual feel similar to the html paragraphs. 确保纯文本/文本区域中的文本被一个或多个\\r\\n分隔,以获得与html段落相似的视觉效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM