...

View Full Version : Contact Form - Simple question



wtfmancmon
01-14-2011, 10:40 PM
I bought this website template, it's form looks really nice, it came with a contact.php file but i don't know where to fill in the "email address" where the form will send to.

here is the contact.php code below:



<?php

if(!$_POST) exit;

$email = $_POST['email'];


//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
$error.="Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;
else{
$values = array ('name','email','message');
$required = array('name','email','message');

$your_email = "james@example.com";
$email_subject = "New Message: ".$_POST['subject'];
$email_content = "new message:\n";

foreach($values as $key => $value){
if(in_array($value,$required)){
if ($key != 'subject' && $key != 'company') {
if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
}
$email_content .= $value.': '.$_POST[$value]."\n";
}
}

if(@mail($your_email,$email_subject,$email_content)) {
echo 'Message sent!';
} else {
echo 'ERROR!';
}
}
?>


Any suggestions?

olidenia
01-14-2011, 11:10 PM
Here you go, I have edited it a bit:

The form is designed to send to whatever email is submited in the form.

EDIT: Tested and working, I have left the example form above just in case, it's on top of the script and calls thesame page, you can moove it to whatever page and then call contact.php or you can also remove it to use an existing form.


<!-- Start of FORM -->
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Name: <input type="text" name="name" value=""><br />
Email: <input type="text" name="email" value=""><br />
Message: <textarea name="message" rows="" cols=""></textarea><br />
<input type="submit" name="submit" value="submit">
</form>
<!-- End of FORM -->

<?php

if(!$_POST) exit;

$email = $_POST['email'];


//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
$error.="Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;
else{
$values = array ('name','email','message');
$required = array('name','email','message');

$your_email = "youremail@example.com";
$email_subject = "New Message: ".$_POST['subject'];
$email_content = "new message:\n";

foreach($values as $key => $value){
if(in_array($value,$required)){
if ($key != 'subject' && $key != 'company') {
if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
}
$email_content .= $value.': '.$_POST[$value]."\n";
}
}

if(@mail($your_email,$email_subject,$email_content)) {
echo 'Message sent!';
} else {
echo 'ERROR!';
}
}
?>

gnomeontherun
01-15-2011, 06:55 PM
Just a note, it would be easy to abuse this script. All I would have to do is send a POST request to it with the same fields and it could be abused to send spam.

You should consider captcha or some kind of protection against this.

Also, you need to filter your request variables. Doing $_POST['value'] doesn't check to see if someone is being malicious, and could allow someone to hack into the system. Example:


$email = filter_input(INPUT_GET, 'email', FILTER_SANITIZE_EMAIL);

Here is a reference for it too: http://www.w3schools.com/php/php_ref_filter.asp

wtfmancmon
01-17-2011, 07:03 PM
wow thanks for the detailed help guys!

unfortunately i don't think that i communicated my request properly. i meant to say that i want to know how to have the form be Sent to a specific address, like if im the administrator of the site at gggg.com and i want all emails to go to admin@gggg.com, where do i put in the php the admin@gggg.com

sorry im new to PHP.

kyle

knightcon
01-17-2011, 09:13 PM
Put it in the $your_email variable.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum