Ankur
Ankur

Reputation: 51108

PHP line breaks don't seem to work

I have the following code that sends a message using the mail() function. Everything works fine except the line breaks at the end of each line don't seem to work. As you can see I am using " \r\n " hoping that this will give me a line break but it doesn't I have added <br> to get the break, but I would rather not use that in case someone doesn't have an HTML email client.

<?php
  $to = '[email protected]'; // Was a valid e-Mail address, see comment below
  $name = $_REQUEST['name'] ;
  $email = $_REQUEST['email'] ;
  $subject = $_REQUEST['subject'] ;
  $message = $_REQUEST['message'] ;

  $content = 'Name: '.$name."\r\n";
  $content .= 'Email: '.$email."\r\n";
  $content .= 'Subject: '.$subject."\r\n";
  $content .= 'Message: '.$message."\r\n";

  $headers  = 'MIME-Version: 1.0' ."\r\n";
  $headers .= 'Content-type: text/html; charset=iso-8859-1' ."\r\n";

  // Additional headers
  $headers .= 'To: iVEC Help <[email protected]>'. "\r\n";
  $headers .= 'From: '.$name.' <'.$email.'>' . "\r\n";

  mail( $to, $subject, $content, $headers);

?>

<p> You sent it ... good work buddy </p>
<p> <?php   '$name' ?> </p>

Upvotes: 2

Views: 5492

Answers (4)

FrankS
FrankS

Reputation: 2422

As mentioned before, either set it to text/plain or add a HTML break for line breaks:

$content = 'Name: '.$name."</br>\r\n";

Upvotes: 0

PatrikAkerstrand
PatrikAkerstrand

Reputation: 45731

The problem is that you say, in your headers, that the mail is an HTML email:

$headers .= 'Content-type: text/html; charset=iso-8859-1' ."\r\n";

If you want to ensure compability with both HTML and text clients, consider using the Multipart content type. This can be achieved in many different ways, but one simple way is to use a library that can do this for you. For example, you can use PEAR:Mail_Mime

Upvotes: 4

falstro
falstro

Reputation: 35687

I'm not an expert in this area, but my guess would be that you're setting the content type to text/html, which implies html-rendering (which means line breaks are translated to a space). If you're not using any HTML-elements (which appear not to), try setting the content type to text/plain.

Upvotes: 2

Greg
Greg

Reputation: 321834

You're sending it as HTML - change the content type to text/plain and it should work.

Upvotes: 7

Related Questions