How to obfuscate your email address from spam bots using PHP

Post Reply
Tony
Lieutenant
Lieutenant
Posts: 86
Joined: Tue Jul 21, 2009 4:11 pm

How to obfuscate your email address from spam bots using PHP

Post by Tony » Sun Nov 29, 2009 3:45 am

Want to have an email address on your web page but don't want the bots to harvest it for their spam lists? Try this technique.

Put this JavaScript in the <head> of your html page:

Code: Select all

<script type="text/javascript">
function contact(domain,user) { 
window.location.href = "mailto:" + user + "@" + domain;
}
</script>
Then use this as your email link:

Code: Select all

<a href="javascript:contact('example.com','name');">Contact Us</a>  
If you only use one domain name for all your email addresses on the page, then you can obfuscate further by hard coding the domain name in the JavaScript function and take it out of the link. See the example below:

Code: Select all

<script type="text/javascript">
function contact(user) { 
window.location.href = "mailto:" + user + "@example.com";
}
</script> 

<a href="javascript:contact('name');">Contact Us</a>
You can autocomplete the subject and even include text in the body of the new email. Simply include ?subject= which can be followed by &body= as shown in the following example. Note that use of some special characters can cause this not to work.

Code: Select all

window.location.href = "mailto:" + user + "@" + domain + "?subject=This is the subject line&body=This is some body text"; 
Post Reply

Return to “PHP & MySQL”