10

I've got the PHP mail(): function working for a form I'm using on my site. I am now trying to format the $body attribute to look nicer and more organized when I receive the subsequent e-mail.

I've tried \n and I've tried
and both give me

Here's the snippet of the code that I'm working with (I think it's just a matter of syntax that I'm doing wrong):

if(!empty($brandname) && !empty($firstname) && !empty($lastname) && !empty($email) && !empty($goals) && !empty($bio)){
        $to = 'test@test.com';
        $subject = 'Submission Form';
        $body = 'Brand Name: '.$brandname.'<br />Name: '.$firstname.' '.$lastname.'<br />Email: '.$email.'<br />About the Company:'.$bio;
        $headers = 'From: '.$email;

It's just displayed the <br /> as text in the e-mail I receive. It was the same when I used \n (which I'm assuming is actually the right way to do it). What am I doing wrong? Thanks.

MillerMedia
  • 3,651
  • 17
  • 71
  • 150

6 Answers6

12

You must place \n in double quotes " not between single quotes ' If you want that this sequence would be interpreted as a new line (line feed 0x0A) character

Take a look at this php documentation:

http://php.net/manual/en/language.types.string.php

Munawir
  • 3,346
  • 9
  • 33
  • 51
Abraham Covelo
  • 895
  • 7
  • 7
5

(PHP 4, PHP 5)

$body = 'Brand Name: '.$brandname."\r\n".'Name: '.$firstname.' '.$lastname."\r\n".'Email: '.$email."\r\n".'About the Company:'.$bio;

Use "\r\n" for newline as above

Bembo
  • 1,859
  • 1
  • 13
  • 6
2

easiest way for this

$newline = '
';

use the $newline variable anywhere you want (:

  • 3
    Depending on where you type the enter key you are going to get different results as Mac, Window, and Linux handle newlines with different combinations of `\r` and `\n`. – Bailey Parker Jan 08 '12 at 23:02
1

use "<br>" instead of \n it will work 100 % my discovery

e.g

$subject="Mail from customer ".$_POST['Name'].`"<br>"`;

enjoy :)

kleopatra
  • 51,061
  • 28
  • 99
  • 211
1

From the manual page of the mail() function, we learn that in order to send HTML emails, extra headers need to be said. This is concisely demonstrated in Example #4.

However, for just line breaks, I wouldn't advise using HTML. For that, simply insert "\n" in the string, making sure to use double quotes, as mentioned by Abraham.

MichD
  • 1,140
  • 8
  • 16
0

I couldn't add a comment to Mohammad Saqib's answer, but I wanted to say that you shouldn't use <br> as a replacement for \r\n because this may cause the email to be caught by 'line too long' spam filters. New lines are important to keep the email more human and allow successful delivery!

The method I recommend is:

$output[] = "line 1";
$output[] = "line 2";
$EmailBody = join("\r\n",$output);

Also you must make sure you 'sanitize' headers to avoid header injection from user input, here is a function that is double sure to stop injection:

function sanitize(&$data){
   return str_ireplace(array("\r", "\n", "%0a", "%0d"), '', stripslashes($data));
}


$headers = 'From: '.sanitize($email);

If you wanted HTML tags to show in the email, change the type using the header:

$headers = 'Content-type: text/html;\r\nFrom: '.sanitize($email);

If you did display HTML, you should clean the user input to avoid anything unexpected... i.e. HTML injection. Use this function anywhere you display user input in HTML:

function htmlclean($str){
    return htmlentities($str, ENT_QUOTES);
}

e.g. $body = "Brand Name: " . htmlclean($brandname);

Sidupac
  • 651
  • 7
  • 11