Home > Blockchain >  How do I add line break in PHP mail function?
How do I add line break in PHP mail function?

Time:01-31

I am having one string which is of near about 100 lines which is similar to this

My name is John. \r\n I am a boy. \r\n I am so in so

Here the above string is coming from backed so when I send mail using PHP mail function it should output as below.

My name is John.
I am a boy.
I am so in so

But unfortunately it give this output to me.

My name is John. I am a boy. I am so in so

The method I am using is similar to this.

$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type:text/html;charset=UTF-8\r\n";
$msg = "Above Str";
mail(to@user, Subject, $msg, $header);

Can anyone help to do make this proper so.

CodePudding user response:

This

$header .= "Content-type:text/html;charset=UTF-8\r\n"; 

is telling the client to render it as HTML, where line breaks aren't rendered.

If you want it as HTML, you can try:

$msg = nl2br($theString); 

to convert line breaks to HTML <br />.

If you want the clients to render it as text, not HTML, change the content type to text/plain:

$header .= "Content-type:text/plain;charset=UTF-8\r\n";

CodePudding user response:

You can try using PHP's nl2br() function to convert the line breaks in the string to HTML line breaks before sending the email:

$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type:text/html;charset=UTF-8\r\n";
$msg = nl2br('Above Str');
mail('to@user', 'Subject', $msg, $header);

This will convert the \r\n line breaks in the string to HTML line breaks
before sending the email.

CodePudding user response:

You specified Content-type: text/html, so the body of your e-mail message is a HTML document. As such, just like when building a regular webpage, newlines themselves have no visible effect.

Use either paragraphs (<p>...</p>) or <br> to get the result you're after.

CodePudding user response:

for this type of output, u need to just create a separate page and add all content that you want to send over the mail. then $msg = Load page; then call or load your that page here and your code will start working try it

  • Related