Sending Transactional Emails with the Marketo REST API: Part 2, Custom Content

July 24, 2015 | by

Last time, we took a look at triggering transactional emails from outside of Marketo.  This week we’ll be looking at how to pass dynamic content to our emails via the Request Campaign API call.  Request Campaign not only allows the triggering of emails externally, but you can  also replace the content of My Tokens within an email.  My tokens are reusable content that can be customized at the program or marketing folder level.  These can also just exist as placeholders to be replaced through your request campaign call.  For instructions on configuring the smart campaign, see part one.

Building your Email

In order to customize our content, first we will need to configure a program and an email in Marketo.  To generate our custom content, we will need to create tokens inside of the program, and then place them into the email that we’re going to be sending.  For simplicity’s sake, we’ll be using just one token in this example, but you can replace any number of tokens in an email, in the From Email, From Name, Reply-to or any piece of content in the email.  So let’s create one token Rich Text for replacement and call it “bodyReplacement”.  Rich Text allows us to replace any content in the token with arbitrary HTML that we want to input. create a new token

Tokens can’t be saved while empty, so go ahead and insert some placeholder text here.  Now we need to insert our token into the email:

add token to email

This token will now be accessible for replacement through a Request Campaign call.  This token can be as simple as a single line of text which needs to be replaced on a per-email basis, or can include almost the entire layout of the email.

The Code

In previous posts, we’ve looked at authenticating and retrieving lead records, and then sending an email to those leads.  We’ll be expanding on the code from last week to pass in customized tokens to our request campaign call.

If the code looks familiar, that’s because it only has two additional lines from the main method in the previous post.  This time we’re creating the content of our token in the bodyReplacement variable and then using the addToken method to add it to the request.  addToken takes a key and a value and then creates a JsonObject representation and adds it to the internal tokens array.  This is then serialized during the postData method and creates a body that looks like this:

Combined, our console output looks like this:

Wrapping Up

This method is extensible in a multitude of ways, changing content in emails within individual layout sections, or outside emails, allowing custom values to be passed into tasks or interesting moments.  Anywhere a my token can be used from within a program can be customized using this method.  Similar functionality is also available with the Schedule Campaign call which will allow you to process tokens across an entire batch campaign.  These can’t be customized on a per lead basis, but are very useful for customizing content across a wide set of leads.