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 us for this on demand webcast to learn about developing complex systems more quickly and efficiently. We'll cover market drivers for developing, governing and reusing systems software assets and how you can develop system software assets with Rational Asset Manager. 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!
|
|
|
|
Visit IBM developerWorks to download a free trial version of WebSphere Business Modeler Advanced V6.1.1, IBM’s premier business process modeling and analysis tool for business users that offers process modeling, simulation, and analysis capabilities. IBM WebSphere Business Modeler helps you visualize, understand, and document business processes for continuous improvement. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download IBM DB2 Express-C 9.5, a no-charge version of DB2 Express 9 database server. DB2 Express-C offers the same core data server base features as other DB2 Express editions and provides a solid base to build and deploy applications developed using C/C++, Java, .NET, PHP, and other programming languages. FREE! Go There Now!
|
|
|
|
As systems increase in complexity, communication between systems and software teams becomes more and more difficult. Now, there’s a way to improve product quality and communication.<br />Read the “Model Driven Systems Development” white paper to see how. Also included in this kit are more educational white papers, customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems.<br /> FREE! Go There Now!
|
|
|
|
Portfolio Management is about effectively managing portfolio value by aligning portfolio investments with business goals. This complimentary e-kit provides a collection of materials that can help you understand how IBM Rational enables and automates best practices for improved governance and clear visibility into portfolio and project performance across the entire IT project lifecycle. 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!
|
|
|
|
Visit IBM developerWorks to try the IBM SOA Sandbox for connectivity. The SOA Sandbox for connectivity provides a trial environment with the tooling and components to help you explore how to effectively connect your infrastructure and integrate all of the people, processes and information in your company. Use the hosted sandbox to explore SOA techniques that streamline connecting existing IT assets together, as well as learn how to connect them to new business logic. FREE! Go There Now!
|
|
|
|
Join this webcast to learn how IBM Rational's Functional Testing solution enables you to implement automation your way, at your pace, with your existing staff. In this webcast, you’ll learn how you can eliminate redundancy of manual test scripts, reduce errors, and increase test coverage through test automation. After this presentation you will understand how IBM Rational Functional Testing solution can streamline your manual testing and make test automation easily attainable. FREE! Go There Now!
|
|
|
|
With IBM Rational Systems Development Solution, you can deliver products faster with higher quality. Within this kit, Read the “Model Driven Systems Development” white paper to see how to improve product quality and communication. Then check out the rest of the e-Kit to learn more about important topics that can affect the success of any software project through customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems. From start to finish, at every stage in your projects, Rational Systems Development Solution can help your company reach its full potential. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |