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 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 (which I'm assuming is actually the right way to do it). What am I doing wrong? Thanks.
You must place 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:
easiest way for this
$newline = '
';
use the $newline variable anywhere you want (:
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 " " in the string, making sure to use double quotes, as mentioned by Abraham.
use "<br>"
instead of it will work 100 % my discovery
e.g
$subject="Mail from customer ".$_POST['Name'].`"<br>"`;
enjoy :)
(PHP 4, PHP 5)
$body = 'Brand Name: '.$brandname."
".'Name: '.$firstname.' '.$lastname."
".'Email: '.$email."
".'About the Company:'.$bio;
Use " "
for newline as above
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 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("
",$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("", "
", "%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;
From: '.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);