TalkPHP
 
 
Account Login
Latest Articles
» How to keep your forms from double posting data
» cURL Basics
» Securing your PHP applications Part 1
» The way the function rolls
» Database Abstraction with Zend_Db - Part 2
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Display Modes
Old 02-20-2008, 03:46 PM   #1 (permalink)
The Contributor
 
Join Date: Sep 2007
Posts: 89
Thanks: 1
Sam Granger is on a distinguished road
Default Email form - validates email by checking a/mx records of domain.

This might be handy for you guys, you can use it in whatever way you want, however, do not remove copyright notice in comments in php. And do now claim as own work!

Now, to the code. Enjoy!!

The form.html - you can rename this to whatever you want:
HTML Code:
<html>
<head>
	<title>Form</title>
</head>

<body>
<form action="form.php" method="post">
	<p><label>Name: <input type="text" name="name_field" /></label></p>
	<p><label>Email: <input type="text" name="email_field" /></label></p>
	<p><label>Message: <textarea name="message_field"></textarea></label></p>
	<p><input type="submit" value="Send" /></p>
</form>
</body>
</html>
The form.php:
PHP Code:
<?php

// include config.php & email.php
require_once('config.php');
require_once(
'email.php');

/*
 @className: Validation
 @classPurpose: Validate User Input
 @classDate: 25.11.2007
 @classCopyright: DutchBrit.com
 @classExtras: none
*/
class Validation {
    
    const
        
VALID_NAME '#^[\w\s]{3,}$#i'// NAME RegExp pattern
        
VALID_EMAIL '^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$'// EMAIL RegExp pattern
        
    
private
        
$name_field ''// The name of the field used in your html document (name="xxx")
        
$email_field ''// the same as above
        
$message_field ''// the same as above
        
    
private $config null// Config Class Instance
    
private $errors false// Errors Flag
    
private $email null,
        
$thanks_email null// Email Class Instance
        
    
public function __construct($name$email$message)
    {
        
$this->name_field $name;//$_POST[$name];
        
$this->email_field $email;//$_POST[$email];
        
$this->message_field $message;//$_POST[$message];
        
        
$this->config Config::getInstance();
    }
    
    
/*
     @purpose: Validate Name
    */
    
private function validate_name()
    {
        
//... check if 'name' field is empty (quick mode) and throw error
        
if( !isset($this->name_field{0}) )
        {
            
$this->throwError($this->config->invalid_name);
        }
        
// check if the name contain invalid characters and length
        
elseif( !preg_match(self::VALID_NAME$this->name_field) )
        {
            
$this->throwError($this->config->invalid_name);
        }
    }
    
    
/*
     @purpose: Validate email address
    */
    
private function validate_email()
    {
        
//... check if 'email' field is empty
        
if( !isset($this->email_field{0}) )
        {
            
$this->throwError($this->config->invalid_email_address);
        }
        
// check for invalid characters
        
elseif( !eregi(self::VALID_EMAIL$this->email_field) )
        {
            
$this->throwError($this->config->invalid_email_address);
        }
        
// check for A or MX record...
        
else
        {
            
// split the email into name @ domain
            
$domain split('@'$this->email_field);
            
            
// if not A or MX record were found, at least try to reach the domain
            
if( !@fsockopen($domain[1], 80$errno$errstr30) )
            {
                
// validate A or MX record. NOTE: checkdnsrr() is not available on Windows!
                
if( !(checkdnsrr($domain[1],"MX") || checkdnsrr($domain[1], "A")) )
                {
                    
$this->throwError($this->config->invalid_email_record);
                }
            }
        }
    }
    
    
/*
     @purpose: Validate Message
    */
    
private function validate_message()
    {
        
//... check if 'message' field is empty
        
if( !isset($this->message_field{0}) )
        {
            
$this->throwError($this->config->invalid_message);
        }
        
// strip all tags but tags in $allowed_tags
        
else
        {
            
$this->message_field strip_tags($this->message_field$this->config->allowed_tags);
        }
    }
    
    
/*
     @purpose: Throw/Display Error Message
    */
    
private function throwError($errstr$errno 0)
    {
        throw new 
Exception($errstr$errno);
        
$this->errors true;
    }
    
    
/*
     @purpose: Run validation and send email
    */
    
public function validate()
    {
        echo 
'validating...';
        
$this->validate_name();
        
$this->validate_email();
        
$this->validate_message();
        
        if( !
$this->errors )
        {
            
// Initialize Instance of Email class.
            
$this->email = new Email($this->email_field$this->name_field$this->message_field,
                
$this->config->to_email$this->config->to_subject);
            
            
//... send email.
            
if( $this->email->send() )
            {
                print(
'Success!');
            }
            else
            {
                
$this->throwError($this->config->send_email_failed);
            }
            
            
            
// Initialize new Instance of Email class to send 'Thank you!' message.
            
$this->thanks_email = new Email($this->config->to_email$this->config->to_email,
                
$this->config->thank_you_msg$this->email_field$this->config->thank_you_subject);
            
$this->thanks_email->send();
        }
    }
};

try {
    
$form = new Validation($_POST['name'], $_POST['email'], $_POST['message']);
    
$form->validate();
} catch( 
Exception $e ) {
    echo 
$e->getMessage();
}

?>
The email.php:
PHP Code:
<?php

/*
 @className: Config
 @classPurpose: Setting up the configuration
 @classDate: 25.11.2007
 @classCopyright: DutchBrit.com
 @classExtras: Singleton (Allow Only One instance of this class)
*/
class Email {
    
    private
        
$email_to ''// email to send to
        
$email_subject ''// subject
        
$email_message ''// message
        
$email_from ''// from email address
        
$email_name ''// from email name
        
    
private $headers = array();
    
    public function 
__construct($from_email$from_name$email_message$to_email$email_subject)
    {
        
// set up all necessary settings
        
$this->email_from $from_email;
        
$this->email_name $from_name;
        
$this->email_message $email_message;
        
$this->email_subject $email_subject;
        
$this->email_to $to_email;
        
        
// set up headers
        
$this->headers[] = "MIME-Version: 1.0\r\n";
        
$this->headers[] = "Content-type: text/html; charset=utf-8\r\n";
        
$this->headers[] = "From: ".$this->email_from." <".$this->email_name.">\r\n";
        
$this->headers[] = "Reply-To: ".$this->email_from."\r\n";
        
$this->headers[] = "X-Mailer: PHP/"  .phpversion();
    }
    
    public function 
send()
    {
        
//...sending...
        
$headers_string implode($this->headers'');
        return 
mail($this->email_to$this->email_subject$this->email_message$headers_string);
    }
};

?>
The config.php:
PHP Code:
<?php

/*
 @className: Config
 @classPurpose: Setting up the configuration
 @classDate: 25.11.2007
 @classCopyright: DutchBrit.com
 @classExtras: Singleton (Allow Only One instance of this class)
*/
class Config {
    
    static private 
$instance null;
    
    public
        
$to_email ='sam.granger@gmail.com'// email address where the email will be send
        
$to_subject 'OOP Form Test'// default email subject
        
$thank_you_msg 'Thank you!'// thank you message
        
$invalid_email_record 'Your email do not have a valid A or MX record.'// invalid A, MX record, fake email.
        
$invalid_email_address 'Your email contacin invalid characters.'// invalid email address (incorrect characters)
        
$invalid_name 'Your name is invalid.'// invalid name (incorrect characters)
        
$invalid_message 'Your message is invalid.'// invalid message (incorrect characters)
        
$send_email_failed 'We were unable to send your message.'// Failed to send...email
        
$send_email_success 'Your message were send'// Email was send successful message
        
$allowed_tags '<a>,<p>'// allowed tags, separated by comma
 
    
private function __construct()
    {
    }
 
    static public function 
getInstance()
    {
        if(
self::$instance == null)
        {
            
self::$instance = new Config();
        }
 
        return 
self::$instance;
    }
};

?>
Sam Granger is offline  
Reply With Quote
Old 03-10-2008, 12:13 PM   #2 (permalink)
The Visitor
 
ninham's Avatar
 
Join Date: Mar 2008
Location: Suffolk UK
Posts: 3
Thanks: 0
ninham is on a distinguished road
Default

Hi Sam

I cant get this to work :(

I get this error
Quote:
Parse error: syntax error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home2/stowtown/public_html/contact/form.php on line 16
__________________
www.codem8.com Your Coding M8
Send a message via MSN to ninham
ninham is offline  
Reply With Quote
Old 03-10-2008, 12:32 PM   #3 (permalink)
The Contributor
 
Join Date: Sep 2007
Posts: 89
Thanks: 1
Sam Granger is on a distinguished road
Default

I'll have a look when I get home. What PHP version are you running btw?
Sam Granger is offline  
Reply With Quote
Old 03-10-2008, 12:37 PM   #4 (permalink)
The Visitor
 
ninham's Avatar
 
Join Date: Mar 2008
Location: Suffolk UK
Posts: 3
Thanks: 0
ninham is on a distinguished road
Default

I'm running PHP Version: 4.4.8 (Zend: 1.3.0)

My host has not upgraded to php5 yet
__________________
www.codem8.com Your Coding M8
Send a message via MSN to ninham
ninham is offline  
Reply With Quote
Old 03-30-2008, 05:58 PM   #5 (permalink)
The Contributor
 
Join Date: Sep 2007
Posts: 89
Thanks: 1
Sam Granger is on a distinguished road
Default

Was only made for php5, sorry. Could make a php4 version for you though?

Need to have a look over this code again.
Sam Granger is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:59 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0