PDA

View Full Version : PHP Help Formatting PHP Email (And other little various issues)



sosADAMsos
July 15th, 2008, 03:55 PM
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."\r\n";
$to = "adam@XXXXX.com";

$body = "From: $name_field\n E-Mail: $email_field\n Comments: $comments_field";

mail($to, $subject, $body, $from);

}
?>

That's sending me this email (to which I've made revisions):
http://www.fwhittier.com/upload/form1.gif

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!

sekasi
July 15th, 2008, 04:31 PM
"From: $name_field\n E-Mail: $email_field\n Comments: $comments_field";


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



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

sosADAMsos
July 15th, 2008, 05:25 PM
Why is that? Is it more secure?

sosADAMsos
July 15th, 2008, 05:51 PM
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"] . "\r\n";
$to = "adam@XXXXX.com";

$message = "Contact Message From XXXXX" . "\r\n\n\n";
$message .= "Name:\n" . $_POST["name"] . "\r\n\n";
$message .= "Email:\n" . $_POST["email"] ."\r\n\n";
$message .= "Comments:\n". $_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 "\n" and a "\r\n"?

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