hmrc / emailaddress

Micro-library for validating and obfuscating email addresses

Version Matrix


Join the chat at Build Status Download

Scala micro-library for typing, validating and obfuscating email addresses

Address Typing & Validation

The EmailAddress class will only accept valid addresses:

scala> import

scala> EmailAddress("")
res0: =

scala> EmailAddress("not_a_meaningful_address")
java.lang.IllegalArgumentException: requirement failed: 'not_a_meaningful_address' is not a valid email address

You can also use EmailAddress.isValid(...):

scala> EmailAddress.isValid("")
res2: Boolean = true

scala> EmailAddress.isValid("not_a_meaningful_address")
res3: Boolean = false

Accessing the domain and mailbox

You can access the mailbox and domain of a given address:

scala> EmailAddress("").domain
res0: =

scala> EmailAddress("").mailbox
res1: = example

These compare equal as you might expect:

scala> EmailAddress("").domain == EmailAddress("").domain
res2: Boolean = true

scala> EmailAddress("").domain == EmailAddress("").domain
res3: Boolean = false


Addresses are obfuscated by starring out all of their mailbox part, apart from the first and last letters:

scala> ObfuscatedEmailAddress("")
res4: = e*****

Unless there are only two letters:

scala> ObfuscatedEmailAddress("")
res7: = **```

You can also create them directly from an EmailAddress:

scala> EmailAddress("").obfuscated
res6: = e*****

Converting back to String

All classes toString and implicitly convert to Strings nicely:

scala> val someString: String = EmailAddress("")
someString: String =

scala> val someString = EmailAddress("").toString
someString: String =

scala> val someString: String = ObfuscatedEmailAddress("")
someString: String = e*****

scala> val someString = ObfuscatedEmailAddress("").toString
someString: String = e*****

scala> EmailAddress("").domain.toString
res4: String =

scala> val s: String = EmailAddress("").domain
s: String =

scala> EmailAddress("").mailbox.toString
res5: String = example

scala> val s: String = EmailAddress("").mailbox
s: String = example


Include the following dependency in your SBT build

resolvers += Resolver.bintrayRepo("hmrc", "releases")

libraryDependencies += "" %% "emailaddress" % "<INSERT VERSION>"


This code is open source software licensed under the Apache 2.0 License.