hmrc / emailaddress   2.1.0

Apache License 2.0 GitHub

Micro-library for validating and obfuscating email addresses

Scala versions: 2.12 2.11 2.10


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("[email protected]")
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("[email protected]")
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("[email protected]").domain
res0: =

scala> EmailAddress("[email protected]").mailbox
res1: = example

These compare equal as you might expect:

scala> EmailAddress("[email protected]").domain == EmailAddress("[email protected]").domain
res2: Boolean = true

scala> EmailAddress("[email protected]").domain == EmailAddress("[email protected]").domain
res3: Boolean = false


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

scala> ObfuscatedEmailAddress("[email protected]")
res4: = e*****

Unless there are only two letters:

scala> ObfuscatedEmailAddress("[email protected]")
res7: = **```

You can also create them directly from an EmailAddress:

scala> EmailAddress("[email protected]").obfuscated
res6: = e*****

Converting back to String

All classes toString and implicitly convert to Strings nicely:

scala> val someString: String = EmailAddress("[email protected]")
someString: String =

scala> val someString = EmailAddress("[email protected]").toString
someString: String =

scala> val someString: String = ObfuscatedEmailAddress("[email protected]")
someString: String = e*****

scala> val someString = ObfuscatedEmailAddress("[email protected]").toString
someString: String = e*****

scala> EmailAddress("[email protected]").domain.toString
res4: String =

scala> val s: String = EmailAddress("[email protected]").domain
s: String =

scala> EmailAddress("[email protected]").mailbox.toString
res5: String = example

scala> val s: String = EmailAddress("[email protected]").mailbox
s: String = example


Include the following dependency in your SBT build before v4.0.0

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

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

Include one the following dependencies in your SBT build for v4.0.0 or after depending on whether you are using Play 2.8, Play 2.9 or Play 3.0

libraryDependencies += "" %% "emailaddress-play-28" % "<INSERT VERSION>"


libraryDependencies += "" %% "emailaddress-play-29" % "<INSERT VERSION>"


libraryDependencies += "" %% "emailaddress-play-30" % "<INSERT VERSION>"

Run the tests and sbt fmt before raising a PR


sbt fmt

Then run the tests and coverage report:

sbt clean coverage test coverageReport

If your build fails due to poor test coverage, DO NOT lower the test coverage threshold, instead inspect the generated report located here on your local repo: /target/scala-2.12/scoverage-report/index.html

Then run the integration tests:

sbt it:test


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