How to format the body of an email message?

I developed a simple php contact form that works fine, but I can’t format the message body according to my requirement, my code is below, I receive mail in single line format,

where I want every information about a new line like this | "Name: Sayed Sherat Ahmed
Contact No: 03453594552
Email: abc@abc.com
Address: R-47, Sector 9, Northern City.
Requirement: Hello, how are you? "

<?php $to = " sheery_1@hotmail.com "; $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."; ?> 
+6
source share
3 answers

You need a new line ( \n ), not an HTML line break ( <br> ), since your email is not marked as an HTML body (and emails with HTML bodies should really have multi-part MIME bodies with both plain text and HTML versions, since "HTML only" is a good flag for spam detectors).

+13
source

If your email is sent as text, then the tags will not do anything in this case. Try using a new line character, for example:

$ MESSAGE_BODY = "Name:". $ _ POST ["name"]. "\ n";

This should solve your problem.

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

$_Post["name"] has the value "jhon".
he prints: jhon<br>

+5
source

Because you are using the html tag in your email content.

Set content- type text/html in your email header

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

OR

you can get rid of this type of problem using phpmailer

+5
source

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


All Articles