SitePoint Sponsor

User Tag List

Results 1 to 1 of 1
  1. #1
    SitePoint Addict
    Join Date
    May 2006
    0 Post(s)
    0 Thread(s)

    Adding validation to existing form mailer

    Hi all,

    I have a form mailer script which I have used many times along with jquery validation. I have had recent spamming issues so I would like to add some server side validation to back the javascript up.

    This is the PHP (also attached)

    Code PHP:
    #                           Universal PHP mailer script                   
    #  Date: 05/05/05                                                             
    #  Written by: Some Guy                                                               
    #  Email: [email][/email]  
    #------------------------------- Set the intial variables below ------------------------------#
    $to = "";	// This is the recipients email address
    $from = "name@email.comk";          // this appears in the from field must be in email format
    $subject = "Contact from the website";          // This is the email subject header
    $forward = 1;  // 1= web forwarding on 0 = forwarding off
    $location = "thankyou.php";     // enter forwarding address here e.g. "thankyou.htm"
    $formMsg = "Someone has filled in the contact form on the website, the results are below.";   //This message appears in the top cell
    $colour1 = "#D6C6FC";                      // Insert a colour in here for the top cell of the table
    $colour2 = "#E1D6FD";                      // Insert a colour in here for the left cells
    $colour3 = "#E8DFFD";                      // Insert a colour for the right cells
    $textcolour = "#444444";                   //insert a value for text colour
    $textsize = "13";                          //text size in pixels
    $logoUrl = "";		// Absolute URL for logo image top left
    $logoAlt = "Company name";		//alt tag on logo
    //------------ Do not Edit below this line unless you know what you are doing ----------------#
    $date = date ("l, F jS, Y"); 
    $time = date ("h:i A"); 
    $msg = "<html><head><style type='text/css'>td {font-family: Arial, Helvetica, sans-serif;font-size: $textsize px; color: $textcolour;text-decoration: none;}</style></head><body><div align=center><table border=0 cellpadding=10 cellspacing=1 width=500>";
    $msg .= "<tr><td colspan=2 width=400 bgcolor='white' align=left valign=top><img src=\"$logoUrl\" alt=\"$logoAlt\"></td></tr>";
    $msg .= "<tr><td colspan=2 width=400 bgcolor='$colour1'><b>$formMsg</b> <br>This form was submitted on $date at $time.<br></td></tr>"; 
    if ($_SERVER['REQUEST_METHOD'] == "POST") {
    	foreach ($_POST as $key => $value) { 
    		$msg .= "<tr><td width=150 bgcolor='$colour2' align=left valign=top>".ucfirst ($key) ."</td><td width=250 bgcolor='$colour3' align=left valign=top>". $value . "</td></tr>"; 
    else {
    	foreach ($_GET as $key => $value) { 
    		$msg .= "<tr><td width=350 bgcolor='$colour2' align=left valign=top>".ucfirst ($key) ."</td><td width=250 bgcolor='$colour3' align=left valign=top>". $value . "</td></tr>"; 
    $msg .= "</table></div></body></html>";
    $headers = "From: $from\r\n";
    // specify MIME-Version 1.0
    $headers .= "MIME-Version: 1.0\r\n";
    //unique boundary
    $boundary = uniqid("KCMAIL");
    //tell e-mail client this email contains alternative versions
    $headers .= "Content-Type: multipart/alternative".
    	"; boundary = $boundary\r\n\r\n";
    //mesage to non mime type browsers
    $headers .= "This is a MIMe encoded message. Please update your email browser.\r\n\r\n";
    //plain text version of message
    $headers .= "--$boundary\r\n".
    	"Content-Type: text/plain; charset=ISO-8859-1\r\n" .
    	"Content-Transfer-Encoding: base64\r\n\r\n";
    $headers .= chunk_split(base64_encode("This is the plain version"));
    //Html version
    $headers .= "--$boundary\r\n" .
    	"Content-Type: text/html; charset=ISO-8859-1\r\n" . 
    	"Content-Transfer-Encoding: base64\r\n\r\n";
    $headers .= chunk_split(base64_encode($msg));
    mail($to, "$subject", "", $headers); 
    if ($forward == 1) { 
        header ("Location:$location"); 
    else { 
        echo "Thank you for submitting our form. We will get back to you as soon as possible."; 

    This is the HTML

    Code HTML4Strict:
    <form id="enquiry" name="enquiry" action="mailer.php" method="post" class="bodyform">
              <h2>Get in touch</h2>
              <p>If you've got any questions or feedback, just drop us an email and we'll get back to you as soon as we can.</p>
    			<p>All fields are required</p>
                  <label for="name">Your name:</label>
                  <input type="text" name="name" value="" id="name" class="required">
                  <label for="email">Your email:</label>
                  <input type="text" name="email" value="" id="email" class="required">
                  <label for="message">Your message:</label>
                  <textarea id="message" name="message" rows="8" cols="40" class="required"></textarea>
              <p class="submit">
                <input type="submit" value="Submit &raquo;">

    Any help would be appreciated.

    Attached Files Attached Files


Posting Permissions

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