Home » Php » php – how to format message body of email?

php – how to format message body of email?

Posted by: admin July 12, 2020 Leave a comment

Questions:

i have developed a simple php contact form, that’s working fine, but i am unable to format the body of the message as per my requirement, my code is given below, i am getting mail in a single line format,

where i want every information on a new line like this
“Name: Syed Sheeraz Ahmed
Contact No: 03453594552
Email: [email protected]
Address: R-47, Sector 9, North city.
Requirement: hello how are you”

<?php
$to = "[email protected]";
$subject = "From Website Contact Form";
$name = $_REQUEST['name'] ;
$contact = $_REQUEST['contact'] ;
$address = $_REQUEST['address'] ;
$MESSAGE_BODY = "Name: ".$_POST["name"]."<br>"; 
$MESSAGE_BODY .= "Contact No: ".$_POST["contact"]."<br>"; 
$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>"; 
$MESSAGE_BODY .= "Address: ".$_POST["address"]."<br>"; 
$MESSAGE_BODY .= "Requirement: ".nl2br($_POST["message"])."<br>"; 
$message = $_REQUEST['message' + 'address' + 'contact'] ;
$from = $_REQUEST['email'] ;
$headers = "From:" . $from;
mail($to,$subject,$MESSAGE_BODY,$headers);
echo "Mail Sent.";
?> 
How to&Answers:

You want a new line (\n) not an HTML line break (<br>) since your email isn’t marked as having an HTML body (and emails that have HTML bodies should really have multipart MIME bodies with both plain text and HTML versions since “HTML only” is a nice flag for spam detectors).

Answer:

As you are using html tag in your email content.

Set the content- type text/html in the header of your mail

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

OR

you can save yourself from this type of problems using phpmailer

Answer:

If your email is sending as text then the
tags will do nothing. Try using a new line character for example:

$MESSAGE_BODY = “Name: “.$_POST[“name”].”\n”;

That should sort your problem.

in above example <br> is printed after the variable printed, why? how to remove <br>
for example:

$_Post["name"] has value “jhon”.
it prints: jhon<br>