# Email obfuscate

**URL:** <https://forum.getkirby.com/t/email-obfuscate/4475>\
**Category:** Plugins\
**Created:** [June 21, 2016, 7:57am UTC](https://forum.getkirby.com/t/email-obfuscate/4475 "2016-06-21T07:57:36Z")\
**Posts on this page:** 1\
**Showing post:** 20

<div class="post-metadata">

**Author:** ![seehat](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/seehat/32/6343_2.png) [@seehat](https://forum.getkirby.com/u/seehat)\
**Post date:** [January 14, 2021, 12:48pm UTC](https://forum.getkirby.com/t/email-obfuscate/4475/20 "2021-01-14T12:48:30Z")

</div>

I wrote a small plugin to automatically encode emails in kirbytext and to provide a fieldMethod to apply the same filter to other fields, e.g. block-textfields.

This also works if a email gets wrapped in an A-Tag, because only the emails found are converted to encoded characters.

I imagine this is useful, in order that no rawtext email gets output on the website.

Maybe it’s also useful for someone here:

```
<?php

class Encode {
  public static function email ($value) {
    return Str::encode($value);
  }

  public static function emailsInText ($text) {
    $text = preg_replace_callback('/[A-Z0-9\._%+-]+(@)[A-Z0-9\.-]+(\.)[a-z]{2,6}/i', function($match) {
      return Encode::email($match[0]);
    }, $text);
    return $text;
  }
}

Kirby::plugin('site/encode', [
  'fieldMethods' => [
    'encodeEmail' => function ($field) {
        $field->value = Encode::email($field->value);
        return $field;
    },
    'encodeEmails' => function ($field) {
      $field->value = Encode::emailsInText($field->value);
      return $field;
  },
  ],
  'hooks' => [
    'kirbytext:after' => function ($text) { return Encode::emailsInText($text); }
  ],
]);
```

---

_[View the full topic](https://forum.getkirby.com/t/email-obfuscate/4475)._
