Extract “Firstname B. Lastname” from “firstname.b.lastname@example.com” using Velocity (with caveats)

Let me start by noting this technique is for addresses @ your email domain, not for lead email addresses. If you forgot to require First Name and Last Name on a lead gen form, complete their profile responsibly. That’s not what this is for!

Sometimes, due to technical gaps, you only have the email address of a sales owner or other internal user, but you need a friendly name for the From: header or signature block.

If your company used 100% predictable logic to generate the address, you can (fairly) safely synthesize a friendly name while waiting for CRM to do a proper field mapping.

That logic is:

Given a display name where (1) all characters are valid in an email address and (2) all words are in title case, convert spaces to dots and append the domain.

For example, the name Sandy-Jo B. O'Shelley is converted to the email address sandy-jo.b.o'shelley@example.com. If an address was generated that way, this Velocity code —

#set( $emailField = $lead.YourEmailField )
## break down left-hand-side@right-hand-side
#set( $lhs = $emailField.substring(0,$emailField.lastIndexOf("@")) )
## split the lhs on the dot
#set( $lhsParts = $lhs.split("\.") )
#set( $displayableParts = [] )
#foreach( $part in $lhsParts )
#set( $displayable = "" )
## split on word break, title case each word, re-join
#foreach( $word in $part.split("\b") )
#set( $displayable = $displayable + $display.capitalize($word.toLowerCase() ) )
#end
## assume single letter is initial
#if( $displayable.length().equals(1) )
#set( $displayable = $displayable + "." )
#end
#set( $void = $displayableParts.add($displayable) )
#end
${display.list( $displayableParts, " " )}

— will extract the original name Sandy-Jo B. O'Shelley.

Caveats

  • Repeating the warning above, this isn’t for lead email addresses. Please don’t use it for that purpose.
  • Some mailservers don’t support — or perhaps are believed to not support — ASCII characters like the quote/apostrophe ' in mailbox names, even though that’s a perfectly valid character in the SMTP standard. In those cases the original O’Shelley would map to oshelley in the email address. You cannot possibly know that happened, so the result will be Oshelley.
  • You can’t know if accents were removed while transforming the result into an ASCII-only value. Leon and Léon will both map to leon.
  • You can’t know if someone’s name should use mixed case, as de la Cruz and De La Cruz will both map to de.la.cruz. Same for McCardle mapping to mccardle.
  • All non-Latin-1 characters and/or anglicized names can’t be recovered. That’s why it’s noted above that all characters must be valid in an email address!
  • In some contexts, a single letter isn’t an “initial” in the western style. So subramanian.m@example.com should be rendered as Subramanian M. The flipside is some two-letter names are properly rendered as initials, like Ma.. You can’t know what’s right just by looking at the email address.
  • There’s no substitute for knowing the person’s actual display name!