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+'@'+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
developerWorks - FREE Tools! |
Join this Rational Talks to You teleconference, featuring Paul Boustany and Mark Krasovich, to speak to the experts about becoming a Rational ClearCase power user. Get a chance to ask your questions and learn tips and tricks for using Rational ClearCase in Agile development FREE! Go There Now!
|
|
|
|
CakePHP is a stable production-ready, rapid-development aid for building Web sites in PHP. This "Cook up Web sites fast with CakePHP" series shows you how to build an online product catalog using CakePHP. FREE! Go There Now!
|
|
|
|
Download the IBM WebSphere Portal V6.1 beta code and learn more about the rich features and enhancements in IBM WebSphere Portal V6.1. WebSphere Portal provides a composite application or business mashup framework and the advanced tooling needed to build flexible, SOA-based solutions, and scalability to meet the needs of any size organization. FREE! Go There Now!
|
|
|
|
Discover how IBM Rational AppScan Standard Edition can help you detext vulnerabilities in your web applications in the Web Application Security eKit. IBM Rational AppScan is a leading suite of automated web application security solutions that scan and test for common Web application vulnerabilities. The new Web Application Security eKit provides you with valuable resources, including white papers, demos, and additional information on the benefits of testing your Web applications. FREE! Go There Now!
|
|
|
|
Download a free trial version of IBM Rational Software Analyzer Developer Edition V7.0 to identify bug defects earlier in the software development cycle. Rational Software Analyzer is an extensible software development solution that reduces the expense of bug-fixes by enabling static analysis code reviews and bug identification very early in the development cycle. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of WebSphere Extended Deployment Compute Grid, which lets you schedule, execute, and monitor batch jobs. Because online transaction processing and batch jobs execute simultaneously on the same server resources, you can avoid costly duplication of resources. Compute Grid supports job types of Java transactional batch, compute-intensive and a new type called "native execution", which enables non-Java workloads to run on distributed end points. FREE! Go There Now!
|
|
|
|
Join us for this web seminar to learn how you can defend your web applications from attack. Learn about the 3 most common web application attacks, including how they occur and what can be done to prevent them. We’ll also discuss manual versus automated approaches for scanning and identifying web application vulnerabilities and how IBM Rational AppScan, an automated vulnerability scanner, can help you automate more of what you are doing manually today. FREE! Go There Now!
|
|
|
|
Discover how Rational tools and best practices for testing can make your job easier. The new Rational Testing eKits provide you with valuable resources – including demos, webcasts, tutorials, and articles – that help you address your specific testing needs across the software lifecycle. Five new eKits are available covering the topics of Requirements and Test Management, Functional Testing, Performance Testing, Code Quality and Embedded Systems, and SOA and Web Services Testing. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Method Composer V7.2 which helps you deliver customized yet consistent process guidance to your project teams and IT organization, and includes the latest version of IBM Rational Unified Process (RUP), which has provided process guidance to teams since 1996. FREE! Go There Now!
|
|
|
|
Whether you are creating new applications or modifying existing ones, managing integration of new components with traditional z/OS elements is a critical part of building and deploying modern applications. Listen to this webcast to see how IBM can help you optimize your development process using an IDE like Rational Developer for System z that integrates with management tools, such as ClearCase to manage your application development on mainframes. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |