Hi Forum users,
I have recently tried to put together a php contact form but when it sends it doesn't have the visitors email address. I receive the email with the subject and message addressed to myself from myself. I would also like to add the visitors name on the contact from, but have also failed at attempting that. Adding input type="text" name="Visitor" to the form and
$visitor = Trim(stripslashes($_POST['visitor']));
to the php form data and adding the $vistor in the create and send didn't appear to work.
Could someone please be of assistance?
<?php
require_once "Mail.php";
//Store form data in local variables
$EmailFrom = Trim(stripslashes($_POST['EmailFrom']));
$EmailTo = "myemail@gmail.com";
$Subject = Trim(stripslashes($_POST['Subject']));
$Message = Trim(stripslashes($_POST['Message']));
// Require fields
$validationOK=true;
if (Trim($EmailFrom)=="") $validationOK=false;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
exit;
}
$validationOK=true;
if (Trim($Subject)=="") $validationOK=false;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
exit;
}
$validationOK=true;
if (Trim($Message)=="") $validationOK=false;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
}
//smtp variables
$host = "smtp.gmail.com";
$port = "587";
$username = "myemail@gmail.com";
$password = "xxxxx";
//reCaptcha validation
require_once('recaptchalib.php');
$privatekey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=captcha.html\">";
exit;
}
//create and send
$headers = array ('From' => $EmailFrom,
'To' => $EmailTo,
'Subject' => "Web contact - $Subject");
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'port' => $port,
'username' => $username,
'password' => $password));
$mail = $smtp->send($EmailTo, $headers, $Message);
//show form submission result
if (PEAR::isError($mail)) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
exit;
} else {
print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.html\">";
}
?>
Form
<div id="eform">
<center><form method="POST" action="contact.php">
Fields marked (*) are required.
<p>Email Address: *<br>
<input type="text" name="EmailFrom"></p>
<p>Subject: <br>
<input type="text" name="Subject"></p>
<p>Message:<br>
<textarea name="Message" rows="10" cols="30"></textarea>
<p><script type="text/javascript"
src="http://api.recaptcha.net/challenge?k=6Lc4eAEAAAAAALC7mtowaUZ2z0sVF1pX_txsraLK">
</script></p>
<noscript>
<iframe src="http://api.recaptcha.net/noscript?k=6Lc4eAEAAAAAALC7mtowaUZ2z0sVF1pX_txsraLK"
height="300" width="500" frameborder="0"></iframe><br>
<textarea name="recaptcha_challenge_field" rows="3" cols="40">
</textarea>
<input type="hidden" name="recaptcha_response_field"
value="manual_challenge">
</noscript>
<p><input type="submit" name="submit" value="Submit"></p>
</form></center>
</div>