Help Formatting PHP Email (And other little various issues)

I’ve read many posts and done tons of searching/testing but just cant seem to figure it out:

I’m trying to create a simple php form for my website.

I’m making the first one simple and just asking for name, email, and comments.

The PHP I’m currently using is


<?php
if(isset($_POST['submit'])) {

    $subject = "Contact Form";
    $name_field = $_POST['name'];
    $email_field = $_POST['email'];
    $comments_field = $_POST['comments'];
    $from = 'From: '.$name_field."
";    
    $to = "adam@XXXXX.com";
    
    $body = "From: $name_field
 E-Mail: $email_field
 Comments: $comments_field";

    mail($to, $subject, $body, $from);
    
}
?> 

That’s sending me this email (to which I’ve made revisions):

  1. Why is the top text “email” and “comments” being indented?

  2. How can I add a message below? I’ve tried fooling around with $msg = “yada yada yada”, but I can’t seem to get it.

Thanks in advance!


"From: $name_field
 E-Mail: $email_field
 Comments: $comments_field"; 

While this is syntax-wise ‘correct’, I’d suggest doing this instead


"From: " . $name_field . "
 E-Mail:" . $email_field . "
 Comments:" . $comments_field; 

Why is that? Is it more secure?

Okay… I used an approach similar to yours and have something that will work for me:


<?php
if(isset($_POST['submit'])) {

    $subject = "CONACT MESSAGE FROM XXXXX";
    $from = 'From: ' . $_POST["name"] . "
";    
    $to = "adam@XXXXX.com";
    
	$message = "Contact Message From XXXXX" . "


";
    $message .= "Name:
" . $_POST["name"] . "

";
	$message .= "Email:
" . $_POST["email"] ."

";
	$message .= "Comments:
". $_POST["comments"];

    mail($to, $subject, $message, $from);
    
}
?>

But I have a few questions:

  1. Is this form secure / could it be taken advantage of? (I know it would get spam, but could it be hacked?)

  2. Whats the difference between just a “
    ” and a “
    ”?

  3. Is there anything I should do to better this form?