How to send a message in php with paragraphs

你说的曾经没有我的故事 提交于 2019-12-14 04:28:13

问题


I'm using the php code below to send a message

<?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.


回答1:


Use <p> </p> tags in the content you get in$_GET['message'];




回答2:


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

Also, why are you using GET method? POST is a better choice




回答3:


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";



回答4:


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:

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. 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:

<?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:

<?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.




回答5:


If you want HTML paragraphs:

Using just the mail() function you would have to compose the HTML source like this:

Do not write anything in the 3rd parameter $message, just leave it blank ''. 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.



来源:https://stackoverflow.com/questions/11648615/how-to-send-a-message-in-php-with-paragraphs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!