0

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
 }
?>

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Change this line:

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

to

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

Or something to that effect

Comments

0

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";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.