Email Code

  Home arrow Email Code arrow EmailCode
EMAIL CODE

EmailCode
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2003-10-15

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    replaces mailto tags and automates javascript production for email addresses to protect against spam. You pass variables including the recipient name, the anchored text, and option subject, css class, and tag title information to the object and it spits out something the spam bots cannot handle. Makes for cleaner front end code.

    By : flyingchair

    <?php

    // class EmailCode
    // Version 1.01, Oct 15, 2003
    // Generates JavaScript based email tags which are
    // spam bot proof by passing simple variables to the class in an array
    // which then returns a Javascript package to the HTML.

    // by Phil Ingram, phil at flyingchair dot net
    // Please let me know if you use it or you have any problems.

    // this class generates email code using javascript to avoid spamming.
    // It could probably be written better by someone who has being doing
    // PHP longer than me but it works and has been tested with every
    // permutation I can think of.

    // You provide:
    // The email address to send to
    // The text between the anchors
    // An optional subject field
    // An optional body field
    // An optional link title
    // An optional CSS class name

    // To call this code:

    // 1. include path to this file
    // include ("path/EmailCode.php")
    //
    // 2. Then create new instance:
    // $variable = new EmailCode (array(
    // 'email_address' => 'you@somewhere.com',
    // 'email_text' => 'The text between the <A> tags',
    // 'email_subject' => 'The optional subject',
    // 'email_body' => 'The optional pre-inserted body copy',
    // 'email_title' => 'for mouseover',
    // 'email_class' => 'name of CSS class'
    // ));
    //
    // Email_address and email_text are mandatory as they are the recipient and the text that appears between the anchor tags.
    // The rest are optional and can be deleted or inserted in any order. Whatever, make sure they are followed by commas except
    // for the last one else you will get an error. The email_address is checked for syntax (x@y.z).
    //
    // 3. At the exact place you wish to place the link (where you would begin the <a> tag)
    // echo $variable->GetEmailCode ();
    //
    // Remember to wrap the tags in PHP tags if you are dipping between HTML and PHP.


    class EmailCode
    {
    var $error = false; //true if missing vital information
    var $error_code = "ERROR: "; //errors are appended to this string
    var $email_address = ""; //the email address you are sending to
    var $email_text = ""; //the text between the anchor tags
    var $email_code = ""; //the code returned to the page calling the object
    var $email_subject = false; //text for subject line
    var $email_body = false; //text for body of email
    var $email_title = false; //text for mouseover on link
    var $email_class = false; //for people using CSS in the anchor tags.
    var $email_comps = array(); //email address will be broken up into bits for spider baffling
    var $first_var = false; //used so it knows whether it should place a ? or a & before an optional variable


    function EmailCode ($email_info)
    {
    //is the address we are sending to set?
    if (!isset($email_info['email_address']))
    {
    $this->error = true;
    $this->error_code .="No email address";
    }
    //if it is - is it in email format?
    elseif (!eregi("^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,3}$", $email_info['email_address']))
    {
    $this->error = true;
    $this->error_code .="Email address is incorrectly formatted";
    }

    //is the text between the anchor tags set?
    if (!isset($email_info['email_text']))
    {
    if ($this->error == true)
    {
    $this->error_code .= " & ";
    }
    $this->error = true;
    $this->error_code .="There is no text to go between the anchor tags";
    }

    if ($this->error == false)
    {
    //the basics are met so let's build the code
    $this->email_address = $email_info['email_address'];
    $this->email_text = $email_info['email_text'];

    //concert email address to two components
    $this->email_comps = explode ("@", $this->email_address);


    //now for the optional stuff
    if (isset($email_info['email_subject'])) $this->email_subject = $email_info['email_subject'];
    if (isset($email_info['email_body'])) $this->email_body = $email_info['email_body'];
    if (isset($email_info['email_title'])) $this->email_title = $email_info['email_title'];
    if (isset($email_info['email_class'])) $this->email_class = $email_info['email_class'];

    //now we have our variables let's construct the code
    //I do it line by line to ensure neatness of appearance in the produced HTML.
    $this->email_code = "<SCRIPT TYPE = \"text/javascript\"> \n";
    $this->email_code .= "<!-- \n";
    $this->email_code .= "// Advanced PHP generated JavaScript email protection by Phil Ingram \n";
    $this->email_code .= "// Web address: http://www.flyingchair.net \n";
    $this->email_code .= "// Feel free to use this script but link back or credit to me please \n\n";
    $this->email_code .= " var email1 = '".$this->email_comps[0]."' \n";
    $this->email_code .= " var email2 = '".$this->email_comps[1]."' \n";
    $this->email_code .= " document.write('<a ') \n";

    //is there a link title?
    if ($this->email_title)
    {
    $this->email_code .= " document.write('title=\"".$this->email_title."\" ') \n";
    }

    //is there a CSS class?
    if ($this->email_class)
    {
    $this->email_code .= " document.write('class=\"".$this->email_class."\" ') \n";
    }

    $this->email_code .= " document.write('href=\"') \n";
    $this->email_code .= " document.write('mai') \n";
    $this->email_code .= " document.write('lto:') \n";
    $this->email_code .= " document.write(email1+'&#64;'+email2) \n";

    //is there a subject?
    if ($this->email_subject)
    {
    $this->email_code .= " document.write('";
    if (!$this->first_var)
    {
    $this->first_var = true;
    $this->email_code .="?";
    }
    else $this->email_code .= "&";

    $this->email_code .= "subject=".$this->email_subject."') \n";
    }

    //is there a body?
    if ($this->email_body)
    {
    $this->email_code .= " document.write('";
    if (!$this->first_var)
    {
    $this->first_var = true;
    $this->email_code .="?";
    }
    else $this->email_code .= "&";

    $this->email_code .= "body=".$this->email_body."') \n";
    }

    $this->email_code .= " document.write('\">') \n";
    $this->email_code .= " document.write('".$this->email_text."') \n";
    $this->email_code .= " document.write('</a>') \n";
    $this->email_code .= "// --> \n";
    $this->email_code .= "</script> \n";
    }
    else
    {
    //the basics are not met so we set the returned code (if called) to the error message.
    $this->email_code = "<strong>".$this->error_code."</strong>";
    }
    }

    function GetEmailCode ()
    {
    return $this->email_code;
    }
    }
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Email Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    EMAIL CODE ARTICLES

    - Basic Ajax contact form
    - Random validation image
    - tinySendMail
    - SaferMail 0.7
    - Smtp Auth Email Script
    - Search Mime Email Structure
    - PHP Text / HTML Email with Attachments 2.1
    - Simple way to send mail wih one attached Doc...
    - Generic POP3 Class
    - PHP Text / HTML Email with Unlimited Attachm...
    - Another SendMail
    - email with attachment
    - Get Email Addresses from Strings
    - EmailCode
    - Email Validation - Gone Wild

    Developer Shed Affiliates

     



    © 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap