# Encrypt email with kirbytags

**URL:** <https://forum.getkirby.com/t/encrypt-email-with-kirbytags/1662>\
**Category:** Questions\
**Tags:** v2\
**Created:** [July 22, 2015, 12:43pm UTC](https://forum.getkirby.com/t/encrypt-email-with-kirbytags/1662 "2015-07-22T12:43:35Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpbagnis](https://avatars.discourse-cdn.com/v4/letter/j/5fc32e/32.png) [@jpbagnis](https://forum.getkirby.com/u/jpbagnis)\
**Post date:** [July 22, 2015, 12:43pm UTC](https://forum.getkirby.com/t/encrypt-email-with-kirbytags/1662/1 "2015-07-22T12:43:35Z")

</div>

Is there anyway to encrypt emails to fight Spam bots using the email Kirbytag ?  
ex : (email: [aa@a.com](mailto:aa@a.com))

---

<div class="post-metadata">

**Author:** ![flokosiol](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/flokosiol/32/468_2.png) [@flokosiol](https://forum.getkirby.com/u/flokosiol)\
**Post date:** [July 22, 2015, 3:17pm UTC](https://forum.getkirby.com/t/encrypt-email-with-kirbytags/1662/2 "2015-07-22T15:17:36Z")

</div>

I don’t know if there’s a ready to use solution, but you could [create your own Kirbytag](http://getkirby.com/docs/advanced/kirbytext) and implement the encryption of your choice.

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [July 22, 2015, 3:22pm UTC](https://forum.getkirby.com/t/encrypt-email-with-kirbytags/1662/3 "2015-07-22T15:22:49Z")

</div>

The `email` tag actually already encrypts email addresses using HTML entities. The DOM view of your developer tools probably don’t show them like this, but the source code does. 😄

---

<div class="post-metadata">

**Author:** ![jpbagnis](https://avatars.discourse-cdn.com/v4/letter/j/5fc32e/32.png) [@jpbagnis](https://forum.getkirby.com/u/jpbagnis)\
**Post date:** [July 22, 2015, 5:40pm UTC](https://forum.getkirby.com/t/encrypt-email-with-kirbytags/1662/4 "2015-07-22T17:40:54Z")

</div>

Thks Lukas! 😀 I read about this encrypt ability but was confuse by my developer tool.

I guess I wrote this for nothing :

```
    <?php

function munge($address)
{
    $address = strtolower($address);
    $coded = "";
    $unmixedkey = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.@";
    $inprogresskey = $unmixedkey;
    $mixedkey="";
    $unshuffled = strlen($unmixedkey);
    for ($i = 0; $i <= strlen($unmixedkey); $i++)
    {
	$ranpos = rand(0,$unshuffled-1);
	$nextchar = $inprogresskey{$ranpos};
	$mixedkey .= $nextchar;
	$before = substr($inprogresskey,0,$ranpos);
	$after = substr($inprogresskey,$ranpos+1,$unshuffled-($ranpos+1));
	$inprogresskey = $before.''.$after;
	$unshuffled -= 1;
    }
    $cipher = $mixedkey;
    $shift = strlen($address);
    $txt = "<script type=\"text/javascript\" language=\"javascript\">\n" .
           "<!-"."-\n" .
           "// Email obfuscator script 2.1 by Tim Williams, University of Arizona\n".
           "// Random encryption key feature by Andrew Moulden, Site Engineering Ltd\n".
           "// PHP version coded by Ross Killen, Celtic Productions Ltd\n".
           "// This code is freeware provided these six comment lines remain intact\n".
           "// A wizard to generate this code is at http://www.jottings.com/obfuscator/\n".
           "// The PHP code may be obtained from http://www.celticproductions.net/\n\n";

    for ($j=0; $j<strlen($address); $j++)
    {
	if (strpos($cipher,$address{$j}) == -1 )
	{
		$chr = $address{$j};
		$coded .= $address{$j};
	}
	else
	{
		$chr = (strpos($cipher,$address{$j}) + $shift) % strlen($cipher);
		$coded .= $cipher{$chr};
	}
    }

    $txt .= "\ncoded = \"" . $coded . "\"\n" .
	" key = \"".$cipher."\"\n".
	" shift=coded.length\n".
	" link=\"\"\n".
	" for (i=0; i<coded.length; i++) {\n" .
	" if (key.indexOf(coded.charAt(i))==-1) {\n" .
	" ltr = coded.charAt(i)\n" .
	" link += (ltr)\n" .
	" }\n" .
	" else { \n".
	" ltr = (key.indexOf(coded.charAt(i))-
shift+key.length) % key.length\n".
	" link += (key.charAt(ltr))\n".
	" }\n".
	" }\n".
	"document.write(\"<a href='mailto:\"+link+\"'>\"+link+\"</a>\")\n" .
	"\n".
        "//-"."->\n" .
        "<" . "/script>";
  //<noscript>N/A" . "<"."/noscript>"; removed because it make the wysiwyg plug-in to misbehave
    return $txt;
}

kirbytext::$tags['email'] = array(
  'html' => function($tag) {

    $email = $tag->attr('email');
    $encrypted = munge($email);
    
    return $encrypted;

  }
);
```

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [July 22, 2015, 7:58pm UTC](https://forum.getkirby.com/t/encrypt-email-with-kirbytags/1662/5 "2015-07-22T19:58:52Z")

</div>

No, not really. The code you used relies on JavaScript, which is probably more reliable in terms of spam bot-unreadablility, while the Kirby implementation simply uses HTML entities (which is probably not as reliable but does not require any JavaScript).  
Thanks a lot for posting this code, it might help other people who are not satisfied with the Kirby implementation. 😄

---

<div class="post-metadata">

**Author:** ![sebastianberns](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/sebastianberns/32/1418_2.png) [@sebastianberns](https://forum.getkirby.com/u/sebastianberns)\
**Post date:** [December 21, 2015, 10:46am UTC](https://forum.getkirby.com/t/encrypt-email-with-kirbytags/1662/6 "2015-12-21T10:46:31Z")

</div>

Hey jpbagnis,  
Hello Lukas

Thanks for the code!  
There’s a problem, though, which messes up the output.  
The link’s closing tag gets dropped:

```
document.write('<a href="mailto:'+ link +'">'+ link +'')

```

Seems to me some kind of collision with kirbytext.  
But I can’t figure out where to fix this.  
Any idea?

---

<div class="post-metadata">

**Author:** ![daizumi](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/daizumi/32/11799_2.png) [@daizumi](https://forum.getkirby.com/u/daizumi)\
**Post date:** [August 30, 2017, 10:29am UTC](https://forum.getkirby.com/t/encrypt-email-with-kirbytags/1662/7 "2017-08-30T10:29:16Z")

</div>

> [@lukasbestle](#):
>
> The email tag actually already encrypts email addresses using HTML entities. The DOM view of your developer tools probably don’t show them like this, but the source code does. 😄

However, that only works if you type in the kirbytag manually. If you use the Email icon dialog, it won’t encrypt your emails.

The reason is the dialog window outputs a different syntax. If you fill in both the email and text field, it will output:

```
(email: aa@a.com text: aa@a.com)

```

And the text in that example won’t be encrypted in the source code - only te email.

If you, in turn, only fill in the email field (which you would expect to solve the problem), then it will output:

```
<aa@a.com>

```

Which will also NOT encrypt your email in the source code.

And it gets worse: I tried to add an info field to explain how emails need to be manually filled in, but my kirbytag example gets rendered as an email link. How do I escape kirbytag syntax?

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [August 30, 2017, 7:52pm UTC](https://forum.getkirby.com/t/encrypt-email-with-kirbytags/1662/8 "2017-08-30T19:52:38Z")

</div>

> [@daizumi](#):
>
> And the text in that example won’t be encrypted in the source code - only te email.

That’s indeed true, however difficult to solve. We can either _always_ encode (not encrypt!) the text or _never_ encode it. Trying to be smart and detect if the text is an email address isn’t reliable enough for this.

> [@daizumi](#):
>
> If you, in turn, only fill in the email field (which you would expect to solve the problem), then it will output:
> 
> \<aa@a.com\>
> 
> Which will also NOT encrypt your email in the source code.

Yes, the bracket syntax is part of Markdown, not of Kirbytext. We should probably not use that in the Panel textarea field and instead use `(email: aa@a.com)`. I have added it as an [idea on GitHub](https://github.com/getkirby/panel/issues/1096).

> [@daizumi](#):
>
> And it gets worse: I tried to add an info field to explain how emails need to be manually filled in, but my kirbytag example gets rendered as an email link. How do I escape kirbytag syntax?

Unfortunately you can’t currently escape Kirbytags properly.  
You could escape them like this: `(\email: aa@a.com)`, but the backslash (or any other character) would be displayed.  
Or you could use the `help` attribute on the `textarea` field as that doesn’t support Kirbytext.

We are working on implementing escapable Kirbytags for Kirby 3. 🙂

---

<div class="post-metadata">

**Author:** ![daizumi](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/daizumi/32/11799_2.png) [@daizumi](https://forum.getkirby.com/u/daizumi)\
**Post date:** [August 31, 2017, 8:58am UTC](https://forum.getkirby.com/t/encrypt-email-with-kirbytags/1662/9 "2017-08-31T08:58:45Z")

</div>

> [@lukasbestle](#):
>
> We are working on implementing escapable Kirbytags for Kirby 3. 🙂

Looking so much forward to Kirby 3!

In the meantime, I managed to avoid Kirbytags from being rendered inside info fields with a zero-width non-joiner character instead of a backslash:

```
(&zwnj;email: name@domain.tld)

```

renders as

```
(email: name@domain.tld)

```

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [August 31, 2017, 10:45am UTC](https://forum.getkirby.com/t/encrypt-email-with-kirbytags/1662/10 "2017-08-31T10:45:03Z")

</div>

Works, but please note that you can’t copy-paste it from the info field to a textarea field – the Kirbytag won’t be rendered there as well.

---

<div class="post-metadata">

**Author:** ![daizumi](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/daizumi/32/11799_2.png) [@daizumi](https://forum.getkirby.com/u/daizumi)\
**Post date:** [August 31, 2017, 11:29am UTC](https://forum.getkirby.com/t/encrypt-email-with-kirbytags/1662/11 "2017-08-31T11:29:02Z")

</div>

That’s true and kind of renders my solution useless. 😕

OK, then I’m sticking with the `help` attribute. Thanks for the tip.

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:32pm UTC](https://forum.getkirby.com/t/encrypt-email-with-kirbytags/1662/12 "2025-02-06T17:32:14Z")

</div>


