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 th
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
easiest way for this
$newline = '
';
use the $newline variable anywhere you want (:
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);
use "<br>"
instead of \n
it will work 100 % my discovery
e.g
$subject="Mail from customer ".$_POST['Name'].`"<br>"`;
enjoy :)
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.
(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