Email-formatting

This is how you best format email addresses

Usually you can find email addresses as attribute of a lot of entities, e.g. "user", "owner", or "contact" in various third party systems. This depends on the system, but the output often looks something like this:

{
  "first_name": "Oliver",
  "last_name": "Doe",
  "email": "oliver.doe@gmail.com",
  ...
}

However, sometimes the email might be a free-form text field in the third party system, so some customers add a comment or even just use non-valid emails. Here are two examples:

  1. "email": "oliver.doe@gmail.com (brother)"

  2. "email": "oliver.doe@go"

In theses cases we have to make sure that the email is either fixable or recognized as a non-valid email. One can use the following Jinja code to do so:

 "email": {% if owner.email != None %}{% set mail_list = regex_search(owner.email, "([A-Za-z0-9]+[.-_])*[A-Za-z0-9]+@[A-Za-z0-9-]+(\.[A-Z|a-z]{2,})+" ) %}{% if probe(mail_list, 0) %}"{{ mail_list.0 }}"{% else %}""{% endif %}{% else %}""{% endif %}

This returns the email in a correct format, or if it's not possible, an empty String "". For our examples that would mean:

  1. "email": "oliver.doe@gmail.com"

  2. "email": ""

Last updated