Email Validations in Forms using Salesforce Marketing Cloud API and AMPscirpt
Updated: Aug 4, 2020
Forms are one of the biggest sources to generate the lead. But imagine a form without having a proper email address? You will try to send an email from Salesforce marketing cloud to an in-valid email address in form. The email will definitely get bounced and it will damage your IP reputation. Due to this, your inbox placement will be reduced and mails will go to junk folders.
You can use the below email validation steps, if the form is hosted in cloud pages.
How to validate Form email addresses in real time using SFMC API ?
There are many tools / App out in online where you can pass the email address with a publicly accessible URL along with a key provided by the app vendors and it will return you True or False based on email address validation.
We are going to use the same process but using Oauth2 to connect SFMC and not by publicly accessible URL. We will be leveraging the REST API provided by SFMC to validate the email.
Before you connect to the marketing cloud you would need to create an installed package for API Integration. This post assumes that you have completed this step.
Step 1 : Get access token via Ampscript
It will be easy to use for you to get access tokens via SSJS. But, am a fan of ampscript, so let's try using it to retrieve the access token using ampscript:
You can use the below code to get the access token. @apitoken variable will hold your access token. [view code snippet]
Note: It's not recommended to store the Client ID and Client secret in a cloud page. So, please store it in a data extension and pull it to the cloud page using Lookup functions.
Just change it to your client_id, Client_secret, MID (Account ID), Post URL for authentication from the below code.
This will return a success response with access token for the next REST API call.
We are using the HTTPPost2 function in ampscript to do a POST call on SFMC to get the authentication token. It has 8 parameters and additional parameters are used if you have any additional headers.
Step 2: Use access token to validate email
Below is the full code which includes the basic form (First Name, last Name & Email Address) and the 2nd Post call to validate the email address. [view code snippet]
Break up the Validators in SFMC REST API:
If you come across the email validation API, there are different validators which have been used. These validators are basically used to evaluate the email which is present in the form. Below screenshots and wordings will give you an idea of how the validator works.
Syntax Validator: This will validate the syntax if the email address has a '@' symbol or if it has '.' symbol. It is as same as the IsEmailAddress function in ampscript.
MX Validator: If you want to send email to anyone you need to create a 'MX' record for the domain. Without that you can't send an email. This validator will check if the MX record has been created for that particular domain. For example, 'firstname.lastname@example.org' will return an invalid email address on MX validator because there is no MX record for that particular domain.
You can remove the validators at any point of time. It is not necessary to keep all the 3 validators in the API call to validate. For example, if you are a B2B company you might have some role based emails to sign up the form. In that case you can remove the List detective validator.
I have used the below email address to test . Please look at the screenshots for all the validators and how the API returns response.
You can trigger a real time verification email using Triggered sends for those email addresses are valid. Feel free to look at my previous post on send triggered emails via ampscript.
Before you get into code, it's better to test your REST API in Postman and if it works then start the coding!
If you are hosting the form on a website or any other site apart from the cloud page you can still leverage the Marketing Cloud API to validate email if the website supports REST and if you have access to marketing cloud.
As per salesforce recommendation you can make 2500 REST API calls / minute. The above process is consuming 2 API calls / forms. So, if you think if more than 1250 customers/minute will be filling up this form then you shouldn't consider this approach.
Please feel free to leave your thoughts / queries on comments.
Happy Learning ! Share the Salesforce !