Issue with AMPScript domain function in SMFC
Sometimes the unexpected currents of salesforce marketing cloud will take you to the issues you never experienced before. Recently we discovered that the function domain is not working within condition used in the email studio.
Wanted to add a restriction on our transactional email service to send emails only when the domain is whitelisted. Simple, right? We thought so too.
I knew there was a function in AMPScript that directly extracts the email domain from a given string.
Domain() function problem when sending email from salesforce marketing cloud
We used domain() function that does what it says. Extracts domain from any valid email string.
%%[ IF NOT EMPTY(Lookup("domain_whitelist","domain","domain", Domain(AttributeValue("EmailAddress")))) THEN]%%
//send email
%%[ELSE]%%
Everything worked in the preview of the email in the content builder (email studio) — no questions asked. “QA” passed with five stars, all good to go. Ready for testing by the other team that like to send API calls.
Now a bit later we’re hearing back from the team that the template stopped working.
We assumed the issue was caused by the newly added AMPScript section and our domain feature.
So, we rebuilt the domain extraction logic using basic AMPScript functions. After clearing the cache, emails started flowing in again.
%%[
SET @email = LowerCase(Trim(AttributeValue("EmailAddress")))
SET @atIndex = IndexOf(@email, "@")
SET @domain = Substring(@email, Add(@atIndex, 1), Length(@email))
IF NOT EMPTY(Lookup("domain_whitelist","domain","domain", @domain)) THEN]%%
//send email
%%[ELSE]%%
⛔ Do not use Domain() nested within another function!
I was not able to sleep and tried the domain() function work and found out that it has issues only when used inline. When you save results of the function to new variable and use the variable in the condition all works as expected
%%[
SET @email = LowerCase(Trim(AttributeValue("EmailAddress")))
SET @domain = Domain(@email)
IF NOT EMPTY(Lookup("domain_whitelist", "domain", "domain", @domain)) THEN
]%%
// send email
%%[ ELSE ]%%
💡 Possible explanation for why Domain() fails when nested within another function
We encounter similar issue with nested indexOf function within one line AMPScript code call. It seems like AMPScript sometimes fails to evaluate nested function calls reliably when used inline inside another function like Lookup(). Especially when functions like Domain() depend on dynamic content (AttributeValue()), the engine might not process them in the expected order — resulting in empty or null values.
By assigning Domain(@email) to @domain, we’re forcing the evaluation before the Lookup() is run — which stabilizes the logic.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus gravida dignissim volutpat. Vestibulum ante ipsum primis in faucibus orci lu
🔒 This content is for Premium Subsribers only.
Please log in to preview content. Log in or Register
You must log in and have a Premium Subscriber account to preview the content.
When upgrading, please use the same email address as your WordPress account so we can correctly link your Premium membership.
Please allow us a little time to process and upgrade your account after the purchase. If you need faster access or encounter any issues, feel free to contact us at info@martechnotes.com or through any other available channel.
To join the Discord community, please also provide your Discord username after subscribing, or reach out to us directly for access.
You can subscribe even before creating a WordPress account — your subscription will be linked to the email address used during checkout.
Premium Subscriber
19,99 € / Year
- Free e-book with all revisions - 101 Adobe Campaign Classic (SFMC 101 in progress)
- All Premium Subscriber Benefits - Exclusive blog content, weekly insights, and Discord community access
- Lock in Your Price for a Full Year - Avoid future price increases
- Limited Seats at This Price - Lock in early before it goes up







