I've created a fairly simple method for obfuscating email addresses as an anti-spam measure. It should work for any browser that supports JavaScript and a fairly modern Document Object Model (DOM). A composer window in the user's default email application will be opened.

First up is the javascript function that assembles the mailto: url and 'submits' it:

<script language="javascript">
  // Action taken when an email 'link' is clicked
  function local_emclick(parm1, parm2)
  {
    test_4 = "lto:";
    d37 = "@";
    t2 = "mai";
    parm = t2+test_4+parm1+d37+parm2;
    self.location = parm;
  }
</script>

You should customize everything (but the script tags and function and self.location) to prevent spammers from learning to recognize the pattern. Even the order of the parameters should be customized.

Next up is the PHP function that eases using this method:

function local_emlink($one, $two, $three, $four, $five="")
  {
    if ($five!="")
    {
      $smso = "<b>"; $rmso = "</b>";
    }
    else
    {
      $smso = ""; $rmso = "";
    }
    print("$smso<a class=\"$two\" href=\"about:\" onclick='local_emclick(\"$one\", \"$three\"); return false;'>$four</a>$rmso");
  }

An example of its use is:

<p style="text-align:center; margin:2pt 18pt; text-indent:0;
         background-color: #CC9966; font-size:7.5pt; font-weight:bold;
         text-decoration:underline">
  Sample Company<br>
  012 Rhode St.<br>
  Notown, MI 00000<br>
  (123)555-7890<br>
  Fax: (123)555-7891<br>
  <?php local_emlink("info", "header", "sample.com", "Contact"); ?>
</p>

If you don't have access to PHP, then you can create the link directly in HTML. This is the same HTML generated by the PHP, except for whitespace::

<a class="header"
   href="about:"
   onclick='local_emclick("info", "sample.com");
            return false;'>Contact</a>

Recommended Answers

All 2 Replies

good post, but you should provide demo for these

good post, but you should provide demo for these

Quite right. Do note that if you copy/paste the examples below, you may receive an error. Daniweb's VB seems to insert "<b></b>" between 'about' and ':' in the HTML example.

Minimal PHP example:

<html><body>

<?php

  function local_emlink($one, $two, $three, $four, $five="")
  {
    if ($five!="")
    {
      $smso = "<b>"; $rmso = "</b>";
    }
    else
    {
      $smso = ""; $rmso = "";
    }
    print("$smso<a class=\"$two\" href=\"about:\" onclick='local_emclick(\"$one\", \"$three\"); return false;'>$four</a>$rmso");
  }
?>

<script language="javascript">
  // Action taken when an email 'link' is clicked
  function local_emclick(parm1, parm2)
  {
    test_4 = "lto:";
    d37 = "@";
    t2 = "mai";
    parm = t2+test_4+parm1+d37+parm2;
    self.location = parm;
  }
</script>

<p style="text-align:center; margin:2pt 18pt; text-indent:0;
         background-color: #CC9966; font-size:12pt; font-weight:bold;
         text-decoration:underline">
  Example Company<br>
  012 Rhode St.<br>
  Notown, MI 00000<br>
  (123)555-7890<br>
  Fax: (123)555-7891<br>
  <?php local_emlink("info", "header", "example.com", "Contact"); ?>
</p>

</body></html>

Minimal HTML example (produce by the above PHP):

<html><body>


<script language="javascript">
  // Action taken when an email 'link' is clicked
  function local_emclick(parm1, parm2)
  {
    test_4 = "lto:";
    d37 = "@";
    t2 = "mai";
    parm = t2+test_4+parm1+d37+parm2;
    self.location = parm;
  }
</script>

<p style="text-align:center; margin:2pt 18pt; text-indent:0;
         background-color: #CC9966; font-size:12pt; font-weight:bold;
         text-decoration:underline">
  Example Company<br>
  012 Rhode St.<br>
  Notown, MI 00000<br>
  (123)555-7890<br>
  Fax: (123)555-7891<br>
  <a class="header" href="about:" onclick='local_emclick("info", "example.com"); return false;'>Contact</a></p>

</body></html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.