Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
%%[
/* pull the field to be masked from a data extension */
set @email = AttributeValue("email")
/* find the position of the first occurrence of an @ sign in the string */
set @at = indexOf(@email, "@")
/* extract the username portion of the email address */
set @username = Substring(@email,1, Subtract(@at,1))
/* check the length of the username */
set @lenUsername = Length(@username)
/* count the number of characters to mask in the username.
it also controls how many characters will not be masked.
currently set to 2, change to more if needed */
set @lenSubtract = subtract(@lenusername,2)
/* extract the string to be masked in the username */
set @usernameSubstring = Substring(@username,2,@lenSubtract)
/* prepare a string of asterisks with the same length as extracted part of username */
for @i = 1 to @lenSubtract do
set @usernameAsterisks = concat(@usernameAsterisks,"*")
next @i
/* extract the domain portion of the email address */
set @domain = Substring(@email,add(@at,1))
/* check the length of the domain */
set @lenDomain = Length(@domain)
/* calculate the number of characters equal to half of the domain length */
set @halfDomain = FormatNumber(Divide(@lendomain,2),"F0")
/* extract the string to be masked in the domain */
set @domainSubstring = Substring(@domain,2,@halfdomain)
/* prepare a string of asterisks with the same length as extracted part of domain */
for @i = 1 to @halfdomain do
set @domainAsterisks = concat(@domainAsterisks,"*")
next @i
/* replace content of the original email string with asterisks */
set @maskedEmail = concat(replace(@username, @usernameSubstring, @usernameAsterisks),"@",replace(@domain, @domainSubstring, @domainAsterisks))
]%%
%%=v(@maskedEmail)=%%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment