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! |
Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, for an overview of Rational’s new software offerings and resources to help modernize and accelerate software innovation on i on Power Systems – while ensuring past application investments are protected and continue to grow. Learn how these solutions are helping customers extend their core i5/OS solutions toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time. FREE! Go There Now!
|
|
|
|
Achieving true agility is a never-ending effort. We will showcase how you can become agile incrementally, a few practices at the time.Which practices should any agile team strive to adopt? What additional practices should you consider based on your needs to scale? Adopting practices are however made much easier with the right tool support. What about if your tools adapt to your practices? We will take a look at how the Jazz technology can be leveraged to make your process change the behavior of your tools. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of Lotus Quickr 8.0, which enables collaboration by transforming the way everyday business content such as documents, rich media, photos, and video can be shared. Lotus Quickr makes it faster and easier to share content of all types (not just documents) within virtual teams. It is designed to make it easier to collaborate across organizational boundaries, while continuing to work within the context of familiar desktop applications. FREE! Go There Now!
|
|
|
|
Join this webcast to see how IBM Data Studio Developer and pureQuery can take the pain out of Java data access. uApplications developed using both Java and SQL have become a common requirement. Database connectivity using Java Database Connectivity (JDBC) to create an application is a multi-step tedious process, and tooling that covers both SQL and Java has been unavailable, until now. IBM Data Studio introduces the pureQuery platform: a high-performance, Java data access platform focused on simplifying the tasks of developing, managing, and optimizing database applications and services. FREE! Go There Now!
|
|
|
|
Manage, govern, and share services across your organization by using WebSphere Service Registry and Repository. Follow the hands-on exercises to learn how to navigate the Web interface to publish, find, reuse, and update services. FREE! Go There Now!
|
|
|
|
IBM Enterprise Modernization solutions help organizations evolve core IT systems towards modern architectures and technologies—reducing the burden of maintenance and freeing up resources to develop new business requirements and capabilities. With the IBM Enterprise Modernization Sandbox for System z you can evaluate IBM Enterprise Modernization solutions focused on five key areas: Assets, Architectures, Skills, Processes and Infrastructures, and Investment. Each solution is based upon real customer experiences and offers a proven path to get you started with your modernization projects. FREE! Go There Now!
|
|
|
|
Ken Krugler, co-founder of code search company Krugle, and Laura Merling, vice president of Marketing and Business Development for Krugle, join to talk about the ins and outs of code search and what it means as a new feature for developerWorks users. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on December 11 at 1:00 pm ET to get tips on building your own plugins with Rational Method Composer. Get your questions answered! FREE! Go There Now!
|
|
|
|
In this webcast, IBM Rational will discuss the importance of Web application security and will share techniques and best practices to introduce application security testing into current QA processes including: understanding common security vulnerabilities and techniques to integrate security testing with defect tracking and remediation systems in an effort to safeguard sensitive online information. FREE! Go There Now!
|
|
|
|
WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |