Email Validations in Forms using Salesforce Marketing Cloud API and AMPscirpt
Updated: 5 days ago
Forms are one of the biggest source 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 address in real time using SFMC API ?
There are many tools / App out in online where you can pass the email address with 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 we connect to marketing cloud you would need to create a 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 token 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 full code]
Note: It's not recommended to store the Client ID and Client secret in cloud page. So, please store it in data extension and pull it to cloud page using Look up functions.
Just change it to your client_id, Client_secret, MID (Account ID), Post URL for authentication from the below code.
This will return success response with access token for the next REST API call.
We are using 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 full code]
Break up the Validators in SFMC REST API:
If you come across the email validation API, there are different validators which has 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 '@' 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 MX record has created for that particular domain. For example, 'email@example.com' 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 address to sign up the form. In that case you can remove the List detective validator.
I have used 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 address are valid. Feel free to look at my previous post on send triggered emails via ampscript.
Before you get into code, its better to test your REST API in Postman and if its works then start the coding!
If you are hosting the form in website or any other site apart from 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 / form. 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 !