Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2010
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Contact Form - Simple question

    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:

    Code:
    <?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?

  • #2
    Regular Coder olidenia's Avatar
    Join Date
    Oct 2009
    Location
    Sitting In Front Of A Screen
    Posts
    110
    Thanks
    16
    Thanked 4 Times in 4 Posts
    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.

    PHP Code:
    <!-- 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!';
        }
    }
    ?>
    Last edited by olidenia; 01-14-2011 at 11:52 PM.
    It's easy once you know how...

  • #3
    Senior Coder gnomeontherun's Avatar
    Join Date
    Sep 2007
    Location
    Houston
    Posts
    2,846
    Thanks
    10
    Thanked 238 Times in 229 Posts
    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:

    Code:
    $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
    jeremy - gnomeontherun
    Educated questions often get educated answers, and simple questions often get simple answers.

  • #4
    New Coder
    Join Date
    Jul 2010
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    Regular Coder
    Join Date
    Apr 2007
    Location
    Griffith. Australia
    Posts
    138
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Put it in the $your_email variable.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •