(VTL Script to) Parse email ID stored in database and use it as URL query parameter in an email template? | Community
Skip to main content
New Participant
May 12, 2023
Solved

(VTL Script to) Parse email ID stored in database and use it as URL query parameter in an email template?

  • May 12, 2023
  • 1 reply
  • 2071 views

Hello there!

 

I've got a {{lead.Email}} in my database. We send out emails to our leads with an links, and the href for the anchor tags contain the email address as a URL query parameter like so-

... <a href="https://abc.domain.com/route?email={{lead.Email}}">My Cart</a> ...

 

We've observed a problem with this- some leads enter their email with a + sign, and this sign remains intact in the URL (it should ideally be parsed into a %2B in the URL). How can I do this?

 

I've tried writing VTL scripts for this-

 

Script (my.parsedEmail):

#set($email = ${lead.Email}) #if($email=="") #set($email = "null") #end $email.replace("+","%2B"))

 

Email:

... <a href="https://abc.domain.com/route?email={{my.parsedEmail}}">My Cart</a> ...

 

This approach resulted in the entire VTL script getting dumped into the URL.

https://abc.domain.com/route?email=&mkt_tok=long_string#set($email%20=%20${lead.Email})#if($email==%22%22)null#end#if($email!=%22%22)$email.replace(%22+%22,%22%2B%22))#end

 

Then, I learned that we're supposed to get the entire <a> tag out of the script in one piece, with the https:// protocol in the tag and the URL in a variable.

 

This is my latest script-

## Generate the right email address (replace + with %2B) #set($email = ${lead.Email}) #if($email=="") #set($email = "null") #end #set($email=$email.replace("+","%2B")) ## define button styles #set($styles="some_inline_button_styles") ## generate the link #set($url="abc.domain.com/route?referral=marketo&amp;email=$email") ## The line that gets injected into the email <a rel="noopener" style="$styles" target="_blank" href="https://$url">Complete My Application</a>

 

The link generated was the most ridiculous one yet-

 

https://%24url/?mkt_tok=long_string

 

I've been stuck for a couple of days, how do I get this to work?

Thanks in advance!

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SanfordWhiteman

A native {{lead.token}} is not URL-safe and any such link needs to be emitted from Velocity.

 

But don’t roll your own URL-encoder, there’s one built into EscapeTool.

#set( $email = $lead.Email ) #if( $email.isEmpty() ) #set( $email = "null" ) #end ## define button styles #set( $styles="some_inline_button_styles" ) ## generate the link #set( $urlNoProto ="abc.example.com/route?referral=marketo&amp;email=${esc.url($email)}" ) ## The line that gets injected into the email <a rel="noopener" style="$styles" target="_blank" href="https://${urlNoProto}">Complete My Application</a>

 

Also remember not to use another company’s actual domain. In demo code use example.com.

1 reply

SanfordWhiteman
SanfordWhitemanAccepted solution
New Participant
May 12, 2023

A native {{lead.token}} is not URL-safe and any such link needs to be emitted from Velocity.

 

But don’t roll your own URL-encoder, there’s one built into EscapeTool.

#set( $email = $lead.Email ) #if( $email.isEmpty() ) #set( $email = "null" ) #end ## define button styles #set( $styles="some_inline_button_styles" ) ## generate the link #set( $urlNoProto ="abc.example.com/route?referral=marketo&amp;email=${esc.url($email)}" ) ## The line that gets injected into the email <a rel="noopener" style="$styles" target="_blank" href="https://${urlNoProto}">Complete My Application</a>

 

Also remember not to use another company’s actual domain. In demo code use example.com.

New Participant
May 13, 2023

This works- thank you!