Creating a new line in the $ body function of PHP Mail

I have a PHP mail () function: working for the form that I use on my site. Now I'm trying to format the $ body attribute to look more pleasant and more organized when I receive a follow-up email.

I tried \ n and I tried, and both gave me

Here is a piece of code I'm working with (I think this is just a syntax issue that I am 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; 

He simply displayed the text <br /> as text in the email I received. It was the same when I used \ n (which I assume is actually the right way to do this). What am I doing wrong? Thanks.

+6
source share
6 answers

You must put \ n in double quotes "not between single quotes". If you want this sequence to be interpreted as a newline character (line 0x0A)

Take a look at this php documentation:

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

+7
source

(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 the new line as above

+3
source

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

eg

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

enjoy :)

+2
source

the easiest way to do this

 $newline = ' '; 

use the $ newline variable anywhere (:

+1
source

On the man page for the mail () function, we learn that additional headers must be specified to send HTML emails. This is briefly demonstrated in Example # 4 .

However, for line breaks, I would not recommend using HTML. To do this, simply insert "\ n" in the string, making sure that you use double quotes, as mentioned by Abraham.

+1
source

I could not add a comment to Mohammad Sakib, but I wanted to say that you should not use <br> as a substitute for \r\n , because this can lead to the message being caught by the "too long line" spam -filters. New lines are important to keep emails more humane and ensure successful delivery!

My recommended method:

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

You should also make sure that you "sanitize" the headers to avoid typing the header from user input, here is a function that usually does not stop the injection:

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

If you want the HTML tags to appear in the message, change the type using the header:

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

If you were displaying HTML, you should clear the user input to avoid something unexpected ... i.e. HTML insertions. Use this function wherever you display user input in HTML:

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

eg. $ body = "Brand Name:". htmlclean ($ branded);

0
source

Source: https://habr.com/ru/post/905476/


All Articles