tags:

views:

33

answers:

3

Hi there, I'm clueless when it comes to PHP and have a script that emails the contents of a form. The trouble is it only sends me the comment when I want it to also send the name and email address that is captured.

Anyone know how I can adjust this script to do this?

A million thanks in advance!

<?php
 error_reporting(E_NOTICE);
 function valid_email($str)
 {
  return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
 }
 if($_POST['name']!='' && $_POST['email']!='' && valid_email($_POST['email'])==TRUE && strlen($_POST['comment'])>1)
 {
  $to = "[email protected]";
  $headers =  'From: '.$_POST['email'].''. "\r\n" .
    'Reply-To: '.$_POST['email'].'' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
  $subject = "Contact Form";
  $message = htmlspecialchars($_POST['comment']);
  if(mail($to, $subject, $message, $headers))
  {
   echo 1; //SUCCESS
  }
  else {
   echo 2; //FAILURE - server failure
  }
 }
 else {
  echo 3; //FAILURE - not valid email
 }
?>
+1  A: 

You could do

$extra_fields = 'Name: '.$_POST['name'].'<br>Email: '.$_POST['email'].'<br><br>Message:<br>';
$message = $extra_fields.$_POST['comment'];

Not exactly clean, but you get the point. Just concatenate the data in with the $message.

Kevin
Brilliant - thanks for that!
Chris
+1  A: 

Change this line:

  $message = htmlspecialchars($_POST['comment']);

to

  $message = htmlspecialchars($_POST['name'] .  $_POST['email'] . "\r\n" .  $_POST['comment']);

Or something to that effect

Cetra
A: 

The problem is your $message = ... line, its only including the $_POST['comment']) variable. You need to add the $_POST['name'] and $_POST['email'] like something below:

$message = '';
$message .= htmlspecialchars($_POST['name']) . "\n";
$message .= htmlspecialchars($_POST['email']) . "\n";
$message .= htmlspecialchars($_POST['comment']) . "\n";
ocdcoder