Question

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 = 'user@example.com'; // 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 <help@ivec.com>'. "\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>
Was it helpful?

Solution

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

OTHER TIPS

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

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.

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top