0

I took the code below from a website but it only seems to send the Message and not the Name or Email. Can anyone tell me why?

<?php $action=$_REQUEST['action']; 
if ($action=="") { ?>

<form  action="" method="POST" enctype="multipart/form-data"> 
<input type="hidden" name="action" value="submit"> 
Your name:<br> 
<input name="name" type="text" value="" size="30"/><br> 
Your email:<br> 
<input name="email" type="text" value="" size="30"/><br> 
Your message:<br> 
<textarea name="message" rows="7" cols="30"></textarea><br> 
<input type="submit" value="Send email"/> 
</form> 

<?php 
}  
else { 
$message=$name=$_REQUEST['name']; 
$email=$_REQUEST['email']; 
$message=$_REQUEST['message']; 
if (($name=="")||($email=="")||($message=="")) 
    { 
    echo "All fields are required, please fill <a href=\"\">the form</a> again."; 
    } 
else{         
    $from="From: dG<my@email.com>\r\nReturn-path: $email"; 
    $subject="Message sent using your contact form"; 
    mail("my@email.com", $subject, $message, $from); 
    echo "Email sent!"; 
    } 
}   
?> 

I thought that I could change

mail("my@email.com", $subject, $message, $from);

to be more like

mail("my@email.com", $subject, $name, $email, $message, $from);

but that didn't work. As you can see, I don't know much about PHP, so please keep it as simple as you can.

Thanks.

Dan
  • 42
  • 4
  • 1
    Once you figure out how to make this work, you should read about [sanitizing email input for security reasons](http://stackoverflow.com/questions/1055460/how-to-sanitze-user-input-in-php-before-mailing) – skrilled May 06 '14 at 17:17
  • 1
    You need to add the `$name`, `$email` manually to your `$message`. Take a look at [the documentation for `mail()`](http://php.net/mail). – Amal Murali May 06 '14 at 17:17
  • this line seems pointless: ``. Also, why use `$_REQUEST`? Use `$_POST`. – TheLettuceMaster May 06 '14 at 17:18

1 Answers1

0

The mail() function accepts these arguments:

to, subject, message, headers, parameters

So, to include the contents of the $name and $email variables in the message, you'll probably want to add them to the message variable. Something like:

$message = $name . $email . $message;

Then, when you send the e-mail with:

mail("my@email.com", $subject, $message, $from);

the $message will include the $name and $email contents.

Once you get that working, you'll probably want to add some space between the name, email and message.

Here is what the complete else block could look like:

else{ $from="From: dG<my@email.com>\r\nReturn-path: $email"; $subject="Message sent using your contact form"; $message = $name . $email . $message; mail("my@email.com", $subject, $message, $from); echo "Email sent!"; }

Here is a link to the full documentation on the mail() function:

http://php.net/manual/en/function.mail.php

Dan
  • 42
  • 4
Leo Galleguillos
  • 2,429
  • 3
  • 25
  • 43
  • 1
    Thanks Leo. That worked a treat. I also looked at the link and added those spaces as you suggested: $message = $name . "\r\n" . $email . "\r\n" . $message; – Dan May 07 '14 at 13:35