0

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.

Synchro
  • 35,538
  • 15
  • 81
  • 104
be MrZulf
  • 342
  • 2
  • 7
  • 2
    If you're telling it to use the content-type HTML, then the client will 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 `
    `, or change the content-type to `text/plain` to make the client output it as plain text.
    – M. Eriksson Jan 31 '23 at 11:05
  • Does this answer your question? [What is the difference between single-quoted and double-quoted strings in PHP?](https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – DarkBee Jan 31 '23 at 11:06
  • Sorry @DarkBee but it is not the correct answer – be MrZulf Jan 31 '23 at 11:07
  • Thank you @M.Eriksson your comment works for me you can post this in my answer – be MrZulf Jan 31 '23 at 11:08
  • Isn't it? [demo](https://ideone.com/AMVb8N) - using `'\r\n'` will return the literal string `\r\n` and not a carriage return new line – DarkBee Jan 31 '23 at 11:09
  • @DarkBee - If it were, the OP should still see `\r\n` in the output, which they don't seem to do. – M. Eriksson Jan 31 '23 at 11:12
  • Then OP's code does not match the real problem – DarkBee Jan 31 '23 at 11:16
  • @DarkBee - How so? They have line breaks in the text, but are rendering it as HTML (which the content-type for the email says), which would give them exactly what they say they get. There are most likely line breaks in the text, if you check the source, but they won't show in HTML. – M. Eriksson Jan 31 '23 at 11:17
  • Not when using single quotes though right? [demo](https://www.darkbee.be/stack/newline.php) – DarkBee Jan 31 '23 at 11:22
  • @DarkBee the above code is just to mention that it is above string but I said it is coming from Backend it won't be a issue for single or double quote – be MrZulf Jan 31 '23 at 11:25
  • @beMrZulf - I think the confusion comes from "coming from the backend" when your PHP code actually _is_ your backend. So referring to something else as the "backend" gets a bit confusing. Maybe you meant it comes from some database? API? Read from a file, or something? – M. Eriksson Jan 31 '23 at 11:26
  • @M.Eriksson yeah it from a json file and Identically it was stored with /r/n so that it could add new lines. Thank you guys for the help. – be MrZulf Jan 31 '23 at 11:29
  • @beMrZulf It is for people having a similar problem and reading this question – DarkBee Jan 31 '23 at 11:46
  • @DarkBee sure. Thank you for your co operation – be MrZulf Jan 31 '23 at 11:48

4 Answers4

3

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";
M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
1

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.

Liel Dahan
  • 97
  • 1
  • 9
-1

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.

Raxi
  • 2,452
  • 1
  • 6
  • 10
-1

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