Zend - The PHP Company




Email

Add Code


Simple (e)mail (HTML)form data engine v1.04  

Type: application
Added by: oz014pen
Entered: 01/02/2002
Last modified: 02/12/2001
Rating: - (fewer than 3 votes)
Views: 14283
Simple mail form engine send data from HTML form to specific email addresses, with few features :
  • e-mail validation rutine,
  • working with POST & GET method
  • CC: & BCC: capability
  • capability to design post form HTML page (with or without printing data)


<?php
<script LANGUAGE="php">

###############################################################################
#                                                                             #
# smf.php - Simple (e)mail (HTML)form data engine                             #
#                                                                             #
# Description : Simple mail form engine send data from HTML form to specific  #
#               email addresses, with few features : e-mail validation        #
#               rutine, capability to design post form HTML page (with or     #
#               without data                                                  #
#                                                                             #
# Version : 1.04                                                              #
#                                                                             #
# Date : 30.1.2002                                                            #
#                                                                             #
# Author : Dubravko Penezic, Dubravko.Penezic@SRCE.hr                         #
#                                                                             #
# is_valid_email(): an e-mail validation utility routine                      # 
# Written by Michael A. Alderete <michael@aldosoft.com>                       #
#                                                                             #
###############################################################################

###############################################################################
#                                                                             #
# Reserved form var name :                                                    #
#                                                                             #
#   (email) $email - email address of recipients, more email address are      #
#                    devided with ,  exm. pero@SRCE.hr,pero@CARNet.hr         #
#                                                                             #
#   (next_url) $next_url - URL of post form HTML page                         #
#                          Puting [form_data] string in HTML code (IT MUST BE #
#                          ALONE in text line) of post form HTML page, script #
#                          will write tabel of all data submited from HTML    #
#                          form (URL must have http:// if page is on Internet)#
#                                                                             #
#   (cc) $cc - email addresses for CC:                                        #
#                                                                             #
#   (bcc) $bcc - email adresses for BCC:                                      #
#                                                                             #
#   (submit) $submit - script expect that name of submit button is submit     #
#                                                                             #
###############################################################################

# Internal configuration

# Sender name

  
$sender='Pero Peric';

# Sender email

  
$sender_email='Pero.Peric@SRCE.hr';

# Remout user information

  
$rh getenv("REMOTE_HOST"); 
  
$ra getenv("REMOTE_ADDR"); 
  
$ru getenv("REMOTE_USER"); 
  
$hua getenv("HTTP_USER_AGENT"); 
  
$rm getenv("REQUEST_METHOD");
  
$referer getenv("HTTP_REFERER");

# Standard disclaimer

  
$disc  "n";
  
$disc .= "Email You recived was create with smf.php script, called from :n";
  
$disc .= "$referern";
  
$disc .= "We know follow data about user who submit data :n";
  
$disc .= "  HOST  : $rhn";
  
$disc .= "  ADDR  : $ran";
  
$disc .= "  USER  : $run";
  
$disc .= "  AGENT : $huan";
  
$disc .= "n If you replay on this message, please use follow parameter for n";
  
$disc .= "recipient : $sender <$sender_email>n";
  
$disc .= "-----------------------------------------------------------------------n";

# Standard email header

  
$header  "Submitet data from HTML form on URL :n";
  
$header .= $referer."n";
  
$header .= "-----------------------------------------------------------------------n";
  
$header .= "n";

# Standar footer

  
$footer  "n";
  
$footer .= "-----------------------------------------------------------------------n";
  
$footer .= "n";

# is_valid_email - check if $address is valid (semantic) email address

  
function is_valid_email($address
  { 

    return (
eregi
            
'^[-!#$%&'*+\./0-9=?A-Z^_`{|}~]+'.      // the user name 
            '@'.                                      // the ubiquitous at-sign 
            '([-0-9A-Z]+.)+' .                       // host, sub-, and domain names 
            '([0-9A-Z]){2,4}$',                       // top-level domain (TLD) 
            trim(
$address))); 
  } 


# read_file(
$pname) - return array with all lines from $pname file/URL

  function read_file(
$pname)
  {
    return file (
$pname);
  }


# write_page(
$page) - write post form HTML page

  function write_page(
$page)
  {
    while (list (
$line_num$line) = each ($page)) 
    {
      print 
$line;
    }
  }


# insert_table(
$page$table) - Inserting tabel with submited data

  function insert_table(
$page,$table)
  {
    
$tmp=array();
    while (list (
$line_num$line) = each ($page)) 
    {
      if (eregi("[form_data]", 
$line))
      {
        array_push(
$tmp,$table);
      }
      else
      {
        array_push(
$tmp,$line);
      }
    }
    
    return 
$tmp;
  }


# create_table(
$data) - create tabel with submited data and return like array

  function create_table(
$data)
  {
    
$tmp = '<table BORDER=0 CELLPADDING=4>'."n";

    while (list(
$key$value) = each($data))
    {
      
$key=str_replace('_',' ',$key);
        
      if( ! eregi("submit",
$key)) 
      {
        
$tmp .= '<tr><td><b><font face="Arial, Helvetica, sans-serif" size="2">'.$key.'</font></b></td><td><font face="Arial, Helvetica, sans-serif" size="2">=</font></td><td><i><font face="Arial, Helvetica, sans-serif" size="2">'.$value.'</font></i><tr>'."n";
      }
    }

    
$tmp .= '</table>'."n";

    return 
$tmp;
  }


# send_data(
$data) - create email, with submited data and send it to recipient, return success code

  function send_data(
$data)
  {
    global 
$email$sender$sender_email$subject$header$footer$disc;

    //Build up some nice From/Reply Headers

    
$headers  = "From: $sender <$sender_email>n";
    
$headers .= "Reply-To: $sender <$sender_email>n";
    if( ! 
$cc=='')
    {
      
$headers .= "Cc: $ccn";
    }
    if( ! 
$bcc=='')
    {
      
$headers .= "Bcc: $bccn";
    }
    
$headers .= "X-Mailer: PHP/" . phpversion(). " smf.php v 1.0";
    
    // Build email body

    
$body = $header;
    while (list(
$key$value) = each($data))
    {
      
$key=str_replace('_',' ',$key);

      if( ! eregi("submit",
$key)) 
      {
        
$body .= $key . ' = ' . $value . "n";
      }
    }

    
$body .= $footer;
    
$body .= $disc;

    
$tmp = mail($email$subject.' - '.date("Ymdhis"), $body$headers);

    return 
$tmp;
  }

  if (
$HTTP_POST_VARS || $HTTP_GET_VARS)
  {
    if (getenv("REQUEST_METHOD") == "GET")
    {
      
$data = $HTTP_GET_VARS;
    } 
    else 
    {
      
$data = $HTTP_POST_VARS;
    }

    
$email_test=array();

    
$email_test=array_merge($email_test,split(",",$email));
    if(! 
$cc=='')
    {
      
$email_test=array_merge($email_test,split(",",$cc));
    }
    if(! 
$bcc=='')
    {
      
$email_test=array_merge($email_test,split(",",$bcc));
    }

    
$err=0;
    
$ems='';

    while (list (
$num$line) = each ($email_test)) 
    {
      if(! is_valid_email(
$line))
      {
        
$err=1;
        
$ems .= 'Invalid email address '.$line."<br>n";
      }
    }

    if(! 
$err)
    {
      if(! send_data(
$data))
      {
        
$err=1;
        
$ems .= 'Problem with sending email. Check sendmail or PHP configuration on HTML server<br>';
      }
    }

    if(
$err)
    {
      print '<html>
<head>
<title> Error Message from smf.php script</title>
</head>
<body>
  <font SIZE=3 COLOR="BF0000">
'.
$ems.'
  </font>
</body>
</html>';
    }
    else
    {
      if(
$next_url=='')
      {
print'<html>
<head>
<title> Submited data send from smf.php script</title>
</head>
<body>
  <font SIZE=3 COLOR="BF0000">
  Submited data send succesfuly !<br><br>
  <a HREF="'.
$referer.'">Return to form</a><br>
  </font>
</body>
</html>';
      }
      else
      {
        
$table=create_table($data);
        write_page(insert_table(read_file(
$next_url),$table));
        exit();
      }
    }
  }

</script>
?>


Usage Example


---------- HTML form side ---------------- 
<form name="form1" method="post" action="smf.php">
  <p>
    <input type="text" name="prvi drugi " size="40" maxlength="40">
  </p>
  <p> 
    <input type="text" name="email">
  </p>
  <p>
    <input type="submit" name="Submit" value="Submit">
  </p>
</form>

----------------- HTML post form -------

<html>
<head>
<title>Data</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<p>Submited data :</p>
[form_data]
</body>
</html>

----------------------------------------


Rate This Script





Search



This Category All Categories