skip to Main Content

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

My name is John. rn I am a boy. rn 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.0rn";
$header .= "Content-type:text/html;charset=UTF-8rn";
$msg = "Above Str";
mail(to@user, Subject, $msg, $header);

Can anyone help to do make this proper so.

4

Answers


  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.

    Login or Signup to reply.
  2. 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.0rn";
    $header .= "Content-type:text/html;charset=UTF-8rn";
    $msg = nl2br('Above Str');
    mail('to@user', 'Subject', $msg, $header);
    

    This will convert the rn line breaks in the string to HTML line breaks
    before sending the email.

    Login or Signup to reply.
  3. This

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

    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-8rn";
    
    Login or Signup to reply.
  4. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search