Location: PHPKode > scripts > YACF - Yet Another Contact Form > yacf-yet-another-contact-form/yacf.php
<?php
# -------------------------------------------------------------------
#
#                 -=* Yet Another Contact Form! *=-
#
# License and copyright:
#
# This program is designed and copyrighted by
# Aaron Colman on April 11, 2004 under the terms 
# of the GPL provided under gpl.txt
#
# For more information visit http://www.ibasics.biz
# or write me at hide@address.com
#
# I will be updating this software as time goes
# on, if you want the newest version... that's 
# where you need to go.
#
# No warranty of any kind is implied or expressed.
# Neither fitness for a purpose nor warranty of 
# merchantability. You alone are liable for your
# own actions with this script. If something goes 
# wrong, it's your fault and you get to deal with 
# the consequences. Not I! If you don't agree with
# these terms then don't use this script.
#
# Last updated: July 11th, 2007
# -------------------------------------------------------------------

# Figure out the newline character
if (strtoupper(substr(PHP_OS,0,3)=='WIN')) {
   $newline = "\r\n";
}
elseif (strtoupper(substr(PHP_OS,0,3)=='MAC')) {
   $newline = "\r";
}
else {
   $newline = "\n";
}

# -------------------------------------------------------------------

# Your site's URL and name
$your_site = '';
$site_name = '';

# The URL to return them to after they've sent a message.
$return_url = '';

# Set these if you want to use them below.
$default_addy = "";
$default_name = "";

# Anti-Spam URL limit - Too many URLs is a sign of form spam.
# Set this and any messages w/ more URLs will be considered
# spam and not sent.
$url_limit   = 2;

# Security issues can arise of slashes aren't added. But
# Other problems can also come up if they are. 
# Add slashes, yes/no?  Slash the message, yes/no?
$add_slashes = TRUE;
$slash_msg   = FALSE;

# Template files to load. The opening template, the close template.
# The template displayed before the mail form is printed and
# The template displayed after the mail form is printed.
$open_template       = "page.open.php";
$close_template      = "page.close.php";
$mail_head_template  = "header.mailertemplate.php";
$mail_foot_template  = "";

# ------ These are the options for the subject line. ------
# Start with 1, go to as many as you want. Don't use 0.
#
# The index number (first number) is the option number.
# Subj is the subject line of the mail sent to you.
# Desc is the description that they see
# Addy is the address it goes to
# Name is the name of the person it goes to.
# Conf is whether or not we send them a confirmation email.

$subject_option[1]['subj'] = "FORM - Get in touch!";
$subject_option[1]['desc'] = "Contact me!";
$subject_option[1]['addy'] = $default_addy;
$subject_option[1]['name'] = $default_name;
$subject_option[1]['conf'] = TRUE;

$subject_option[2]['subj'] = "FORM - Link Exchange.";
$subject_option[2]['desc'] = "I'd like to exchange links.";
$subject_option[2]['addy'] = $default_addy;
$subject_option[2]['name'] = $default_name;
$subject_option[2]['conf'] = TRUE;

$subject_option[3]['subj'] = "FORM - Site problem.";
$subject_option[3]['desc'] = "There's a problem with your site.";
$subject_option[3]['addy'] = $default_addy;
$subject_option[3]['name'] = $default_name;
$subject_option[3]['conf'] = TRUE;

# -------------------------------------------------------------------
# Customize this at will
$confirm_msg = "Hello!".$newline."Thank you for your recent email.".$newline.$newline."We will respond to it as quickly as we can.$newline                                Sincerely, $newline                                $site_name.".$newline.$newline."Note: If this message is in error then please accept".$newline."our apologies. This is not due to any list, it is an".$newline."auto-response from our contact form.";
$confirm_sub = 'Thank you! Your Message has been received.';
# -------------------------------------------------------------------

function my_addslashes($st) {
    if (get_magic_quotes_gpc())
        return $st;
    else
        return addslashes($st);
}

function countURLs($msg) {
    preg_match_all("/http(s?)\:\/\/\w+/i", $msg, $matches);
    return count($matches[0]);
}

function isEmail($address = "") {
     if ($address == "") {
          return FALSE;
     }
     if (preg_match("/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z0-9.-]+$/i",$address)) {
          return TRUE;
     }
     else { return FALSE; }
}

function print_mailform() {
    global $This_File, $var_replyto_name;
    global $var_replyto_email, $subject_option;
    global $var_subject_chosen, $var_message;

    $fcolor = "#CCCCCC";
    $bcolor = "#000066";
    print "<center>\n";
    print "<form method=POST action=\"$This_File\">\n";
    print "<strong>Your Name:</strong><br>\n";
    print "<input name=\"form_replyto_name\" size=40 maxlength=99 value=\"$var_replyto_name\" \n";
    print "       style=\"border-width: 2px; border-color: #000000; \n";
    print "       color: $fcolor; background-color : $bcolor;\">\n";
    print "<br><br>\n";
    print "<strong>Email Address:</strong><br>\n";
    print "<input name=\"form_replyto_email\" size=40 maxlength=99 value=\"$var_replyto_email\" \n";
    print "       style=\"border-width: 2px; border-color: #000000; \n";
    print "       color: $fcolor; background-color : $bcolor;\">\n";
    print "<br><br>\n";
    print "<strong>Subject: &nbsp;</strong>\n";
    print "<select name=\"form_subject\" \n";
    print "        style=\"border-width: 2px; border-color: #000000; \n";
    print "        color: $fcolor; background-color : $bcolor;\">\n";
    print "<option value=\"\">Choose One\n";
 
    $SubjArraySize = sizeof($subject_option);
    for ($i=1; $i<=$SubjArraySize; $i++) {
        if ($i == $var_subject_chosen) { $CHECKED = " SELECTED"; }
            else { $CHECKED = ""; }
        print "<option value=\"$i ".$subject_option[$i]['subj']."\"$CHECKED>".$subject_option[$i]['desc']."\n";
    }
 
    print "</select>\n";
    print "<br><br>\n";
    print "<strong>Message:</strong><br>\n";
    print "<textarea name=\"form_message\" rows=12 cols=45 \n";
    print "          style=\"border-width: 2px; border-color: #000000; \n";
    print "          color: $fcolor; background-color : $bcolor;\">$var_message</textarea>\n";
    print "<br><br>\n";
    print "<CENTER>\n";
    print "<table border=\"0\" width=\"300\"><tr>\n";
    print "<td width=\"50%\"><CENTER><input type=\"submit\" name=\"send\" value=\"Send\"></CENTER></td>\n";
    print "<td width=\"50%\"><CENTER><input type=\"reset\" value=\"Clear\"></CENTER></td>\n";
    print "</tr></table>\n";
    print "</CENTER>\n";
    print "<input type=\"hidden\" name=\"act\" value=\"send\">\n";
    print "</form>\n";
    print "</center>\n";
}

# -------------------------------------------------------

if (isset($_POST['act'])) { $action = strtolower(trim($_POST['act'])); }
   else { $action = ""; }

if (!(empty($open_template))) {
   $page_title = "Adaptive Business Design - Contact us.";
   $disable_rightside = TRUE;
   include($open_template); 
}

if (empty($action)) {
    if (!(empty($mail_head_template))) { include($mail_head_template); }
    print_mailform();
    if (!(empty($mail_foot_template))) { include($mail_foot_template); }
}
elseif ($action == "send") {

    # Grab the data
    if (isset($_POST['form_message'])) { $var_message = htmlspecialchars(trim($_POST['form_message'])); }
       else { $var_message = ""; }
    if (isset($_POST['form_subject'])) { $var_subject = htmlspecialchars(trim($_POST['form_subject'])); }
       else { $var_subject = ""; }
    if (isset($_POST['form_replyto_email'])) { $var_replyto_email = htmlspecialchars(trim($_POST['form_replyto_email'])); }
       else { $var_replyto_email = ""; }
    if (isset($_POST['form_replyto_name'])) { $var_replyto_name = htmlspecialchars(trim($_POST['form_replyto_name'])); }
       else { $var_replyto_name = ""; }

    # Clean the data
    $var_message       = str_replace("|","",$var_message);
    $var_subject       = str_replace("|","",$var_subject);
    $var_replyto_email = str_replace("|","",$var_replyto_email);
    $var_replyto_email = str_replace(">","",$var_replyto_email);
    $var_replyto_email = str_replace("<","",$var_replyto_email);
    $var_replyto_email = str_replace('/',"",$var_replyto_email);
    $var_replyto_email = str_replace('..',"",$var_replyto_email);
    $var_replyto_name  = str_replace("|","",$var_replyto_name);
    $var_subject       = preg_replace("/\n|\r|\t/","",$var_subject);
    $var_replyto_email = preg_replace("/\n|\r|\t/","",$var_replyto_email);
    $var_replyto_name  = ucwords(preg_replace("/\n|\r|\t/","",$var_replyto_name));

    # Verify the chosen subject
    $var_subject_chosen_array = explode(' ',$var_subject);
    $var_subject_chosen = $var_subject_chosen_array[0];
    if (!(is_numeric($var_subject_chosen))) { 
         $var_subject = "";
    }
    else {
         if (isset($subject_option[$var_subject_chosen]['subj'])) {
            $var_subject = $subject_option[$var_subject_chosen]['subj'];
         }
         else {
            $var_subject = "";
         }
    }  

    # Add slashes if we should
    if ($add_slashes) {
        if ($slash_msg) { $var_message = my_addslashes($var_message); }
        $var_subject       = my_addslashes($var_subject);
        $var_replyto_email = my_addslashes($var_replyto_email);
        $var_replyto_name  = my_addslashes($var_replyto_name);
    }

    # Handle errors
    $skipflag = FALSE;
    print "<br>\n";
    if (empty($var_replyto_name))  { print "<font color=\"#660000\"><strong>* Required field -- \"Your Name\" is missing! Fix and re-submit.</strong></font><br>\n"; $skipflag = TRUE; }
    if (empty($var_message))       { print "<font color=\"#660000\"><strong>* Required field -- \"Your Message\" is missing! Fix and re-submit.</strong></font><br>\n"; $skipflag = TRUE; }
    if (empty($var_subject))       { print "<font color=\"#660000\"><strong>* Required field -- \"The Subject\" is missing! Fix and re-submit.</strong></font><br>\n"; $skipflag = TRUE; }
    if (empty($var_replyto_email)) { 
        print "<font color=\"#660000\"><strong>* Required field -- \"Your E-mail Address\" is missing! Fix and re-submit.</strong></font><br>\n";
        $skipflag = TRUE; 
    }
    else {
        if (!(isEmail($var_replyto_email))) {
            print "<font color=\"#660000\"><strong>* Required field -- \"Your E-mail Address\" is invalid! Fix and re-submit.</strong></font><br>\n";
            $skipflag = TRUE;
        }
    }
    if ((countURLs($var_message)) > $url_limit) {
        print "<font color=\"#660000\"><strong>*Anti-Spam: You have too many URLs in your message! Fix and re-submit.</strong></font><br>\n";
        $skipflag = TRUE;
    }

    if ($skipflag) { 
        print "<br>\n";
        print_mailform(); 
    }
    else {
       # Init the user info header
       $user_info_string  = "";
       $user_info_string .= "Sent from: ".$_SERVER['REMOTE_HOST']." [".$_SERVER['REMOTE_ADDR']."]$newline";
       $user_info_string .= "Coming from (referer): ".$_SERVER['HTTP_REFERER']."$newline";
       $user_info_string .= "Using (user agent): ".$_SERVER['HTTP_USER_AGENT']."$newline$newline";

       # Grab the data for this subject selection
       $send_subj = $subject_option[$var_subject_chosen]['subj'];
       $send_addy = $subject_option[$var_subject_chosen]['addy'];
       $send_name = $subject_option[$var_subject_chosen]['name'];
       $send_conf = $subject_option[$var_subject_chosen]['conf'];

       # Generate the headers
       $headers  = "Return-Path: <" . $var_replyto_email . ">$newline";
       $headers .= "From: $var_replyto_name <" . $var_replyto_email . ">$newline";
       $headers .= "Reply-To: $var_replyto_email$newline";
       $headers .= "Envelope-to: $send_addy$newline";
       $headers .= "Date: " . date('r') . "$newline";
       $headers .= "MIME-Version: 1.0$newline";
       $headers .= "Priority: normal$newline";
       $headers .= "X-Mailer: Yet Another Contact Form$newline";

       # Process and send the mail
       $var_message = preg_replace("/\r/","\n",$var_message);
       $var_message = preg_replace("/\n\n/","\n",$var_message);
       $var_message = preg_replace("/\n/",$newline,$var_message);
       $var_message = $user_info_string.$var_message;
       mail($send_addy, $send_subj, $var_message, $headers);

       # Send the confirmation?
       if ($send_conf) {
           $headers  = "Return-Path: <" . $send_addy . ">$newline";
           $headers .= "From: $send_name <" . $send_addy . ">$newline";
           $headers .= "Reply-To: $send_addy$newline";
           $headers .= "Envelope-to: $var_replyto_email$newline";
           $headers .= "Date: " . date('r') . "$newline";
           $headers .= "MIME-Version: 1.0$newline";
           $headers .= "Priority: normal$newline";
           $headers .= "X-Mailer: Yet Another Contact Form$newline";
           mail($var_replyto_email, $confirm_sub, $confirm_msg, $headers);
       }

       # And the final msg
       print "<center>\n";
       print "<font size=\"5\"><strong>Your message was sent.</strong></font>\n";
       print "</center>\n";
       print "<br><br>\n";
       print "Thank you $var_replyto_name,<br>\n";
       if ($send_conf) {
           print "In a minute you should receive a confirmation at $var_replyto_email.<br>\n";
       }
       print "<br>\n";
       print "<A href=\"$return_url\">Return to $site_name.</A><br>\n";
       print "<br>\n";
    }
}
else {
  print "<strong>ERROR! I did not understand your action!</strong><br>\n";
}

if (!(empty($close_template))) { include($close_template); }
?>
Return current item: YACF - Yet Another Contact Form