Location: PHPKode > scripts > Email Code > email-code/EmailCode.php
<?php

/*
// class EmailCode
// Version 1.02, Oct 20, 2004
// Generate JavaScript based email tags which are 
// spam bot proof but by passing simple variables to the class in an array
// which returns a Javascript package.
// 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 are free to use this code as you wish as often as you wish. Just leave the acknowledgement in the source code
// as a matter of courtesy.

// 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' => 'hide@address.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 (hide@address.com).
//
//	3. At the exact place you wish to place the link (where you would begin the <a> tag)
//	<?php 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;
	}
	
	function CheckError ()
	{
		return $this->error;
	}
}
?>
Return current item: Email Code