View Single Post
Old 02-20-2008, 04:46 PM   #1 (permalink)
Sam Granger
The Acquainted
 
Join Date: Sep 2007
Posts: 126
Thanks: 4
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
The Following User Says Thank You to Sam Granger For This Useful Post:
Sirupsen (05-22-2009)