It’s important to personalize your emails. Now you can hyper-personalize your outreach emails with conditional content using simple If/Then statements in the Subject and Body of your message. Do you have to be a programmer to do this? No! We make inserting conditional content fairly easy. Use one of our copy/paste recipes and modify to your tastes. Let’s get started.
How do you use conditional content?
Conditional content blocks can be placed in the Subject and Message inside a set of double curly brackets, like so:
{{...code...}}
You can mix regular merge tags, which use a single set of curly brackets, with conditional code blocks, so:
Hi {FirstName}, {{...code...}}
In the Gmail Compose box’s Subject and Message areas, just type a single left curly bracket to see the regular merge tag menu and type two left curly brackets to see the conditional content menu. The conditional content menu gives you sample shorthand If/Then statements for each variable in your campaign. Of course you’ll want to customize the statement from there.
What statements can you use?
Let’s say you have a column in your Google Sheet called Industry that identifies the industry that each of your email prospects works in.
Our conditional content feature supports a bunch of statements, but the most useful will be the If/Then statement. Here’s an example.
Basic If/Then statement:
{{If Industry="Recruiting" Then}} {FirstName}, hope the recruiting business is going well and you're placing lots of candidates! {{End If}} {{If Industry="Sales" Then}} {FirstName}, hope your sales are high and you're closing deals left and right! {{End If}} {{If Industry="Other" Then}} {FirstName}, hope things are going well for you. {{End If}}
Basic If/Then/Else:
A better way to do the above is to use an ELSE block.
{{If Industry="Recruiting" Then}} {FirstName}, hope the recruiting business is going well and you're placing lots of candidates! {{Else If Industry="Other" Then}} {FirstName}, hope your sales are high and you're closing deals left and right! {{Else}} {FirstName}, hope things are going well for you. {{End If}}
In the above examples, I’m outputting text to my email based on the Industry column of my spreadsheet. Additionally, I’m using a regular “FirstName” merge tag in each of the code blocks.
Using comparative operators
If your Google Sheet has columns with numerical data, then you can use those to set content based on conditions. For example if your CRM system has a field called DaysSinceLastContact and you export from your CRM system to a Google Sheet, you might do this:
{{If CInt(DaysSinceLastContact) > 30 Then}} It's been a long while...{DaysSinceLastContact} days since we talked! {{Else}} Hope I'm not annoying you with all my outreach! {{End If}}
In the above example, it’s important to surround DaysSinceLastContact with the CInt command, which converts the column to an integer type, so that it can be properly compared to the integer 30. Without CInt, it would be compared as a string, and would yield different results.
Let’s say you have a column called LastPurchaseDate where you track the last time a customer purchased from you. You might want to vary some text based on how long ago your customer last purchased.
{{If CDate(LastPurchaseDate) > CDate("1/1/20") Then}} Thanks for supporting us with a purchase during this year of the pandemic. You last purchased on {LastPurchaseDate}. {{Else}} It's been a while since we've heard from you. Can we help you with your next purchase? {{End If}}
Note the use of the CDate operator. That makes sure that both values are treated as dates and compared as dates. If you’re a programmer, just like with integers you know that comparing dates as dates is important, as opposed to comparing dates as strings.
Shorthand for If/Then statements:
There’s a shorthand you can use for If/Then statements that goes like this:
{{ IfThenElse(condition, true result, false result) }}
Here’s an example:
{{ IfThenElse(DaysSinceLastContact > 30, "It's been a while...", "Hope I'm not annoying you...") }}
In this example, if DaysSinceLastContact is greater than 30, then “It’s been a while…” is used, otherwise “Hope I’m not annoying you…” is used.
Using boolean logic
You can combine conditions with AND, OR, and parentheses.
Example using OR:
{{If Industry="Recruiting" OR Industry="HeadHunting" Then}} {FirstName}, hope your business is going well and you're placing lots of candidates! {{End If}}
Example using AND:
{{If CDate(LastPurchaseDate) >= CDate("1/1/20") AND CDate(LastPurchaseDate) <= CDate("12/31/20") Then}} Thanks for supporting us with a purchase during 2020, the year of the pandemic. You last purchased on {LastPurchaseDate}. {{Else}} It's been a while since we've heard from you. Can we help you with your next purchase? {{End If}}
Example combining conditions using parentheses:
{{If (Industry="Recruiting" OR Industry="HeadHunting") AND CDate(LastPurchase) >= CDate("1/1/20") Then}} {FirstName}, hope your people business is going well and you're placing lots of candidates! Also thanks for your business in 2020. {{Else}} It's been a while! Can I help you with anything in your business? {{End If}}
Randomize text using the “spin” command:
You can also randomize text, based on a set of variations. Let’s say you want to provide a random greeting for each of your email recipients:
{{spin}} Hey there! {{variation}} Hello there! {{variation}} Hi there friend! {{end spin}}
Three Easy and Practical Examples
Example One: Getting rid of the annoying space in greetings
One of the personalization dilemmas every seasoned email marketer experiences at some point in their careers is dealing with the extra “space” in the greeting when you don’t know a prospect’s first name. Let’s say your email list contains first names for half of your email addresses only. You want your email to begin:
Hey {FirstName},
for those people where you know the first name, but just:
Hey,
for those whose first names you don’t know.
If you compose your campaign with “Hey {FirstName},” then for the recipients that don’t have first names will see:
Hey ,
and now you see the problem. There’s that extra space before the comma. Conditional content solves this problem easily:
{{ IfThenElse(FirstName != "", "Hey {FirstName},", "Hey,") }}
What this code block does says is, if the first name isn’t blank, then output “Hey {FirstName},”, and if it is blank then use “Hey,”. Problem solved, no extra annoying space.
Example Two: Greet people in their own language
If you know the country of each of your email list recipients, it’s easy to output the greeting in their primary language. For example, if you have a Country column in your Google Sheet, you can do this:
{{If Country="USA" Then}} Good day! {{Else If Country="France" Then}} Bonjour! {{Else if Country="Germany" Then}} Guten Tag! {{Else If Country="India" Then}} Namaste! {{Else}} Hi! {{End If}}
But what if you don’t know the country for each prospect? You can still greet people in their primary language by looking for a country designation within the email address. For example, we know that [email protected] is a United Kingdom address, and we know that [email protected] is a Russian address. So with the magic of conditional content, we can do this:
{{If Right(EmailAddress, 3) = ".fr" Then}} Bonjour! {{Else If Right(EmailAddress, 3) = ".de" Then}} Guten Tag! {{Else If Right(EmailAddress, 3) = ".in" Then}} Namaste! {{Else}} Hi! {{End If}}
Here I’m using the Right command to examine the three rightmost characters of the EmailAddress value to determine if it’s a country-specific address. You can also parse string values with the Left command the the Contains command. A flawed version of the above sample looks like this:
{{If Contains(EmailAddress, ".fr) Then}} Bonjour! {{Else If Contains(EmailAddress, ".de) Then}} Guten Tag! {{Else If Contains(EmailAddress, ".in) Then}} Namaste! {{Else}} Hi! {{End If}}
Why is this version flawed? Because it’s not enough to check whether an email address simply contains “.fr” because the address [email protected] contains “.fr” but is not a French address. It’s important to look at the rightmost characters, not just check whether the email address contains the characters.
Example 3: A SaaS platform emailing both free and paid users
As the creator of GMass, I send out an email announcement to all of my users whenever there’s a hot new feature I’ve just launched, like this one, ahem ahem. Now I can vary my opening line based on whether the user is a freemium user or a paid subscriber.
Let’s say I have a column in my spreadsheet called AccountType that is either “free” or “paid”.
Hey {FirstName}, {{If AccountType="free" Then}} As a freemium user, we're happy to add awesome new features to our platform all the time. We do hope that you'll find value to your life with our SaaS platform and encourage you to subscribe. {{Else}} Thank you for being a paying subscriber and supporting our SaaS platform. We use the money you pay us to build awesome new feautres like this one. {{End If}}
I used logic similar to this when I announced this feature to the entire GMass user base. See the exact email template I used.
Gmail might mangle your code, but that’s okay!
When you write your script in the Gmail Compose, if you look at the HTML that’s created behind the scenes, you’ll notice that elements of your code are turned into HTML entities. For example, if in my Compose window I have:
{{ If CDate(LastPurchase) > CDate("1/1/20") Then}} Thanks for your purchase this year! {{ End If }}
If I look at the HTML behind the Compose, I’ll see this:
Notice how the “greater than” sign has turned into the HTML entity > instead of just the > symbol. GMass knows to look for those and treat them as their encoded equivalent, so you don’t have to worry that those aren’t actual double quotes. This is the benefit of building this feature from scratch with our own custom compiler.
Testing your script before you send
The easiest way to test your script to make sure your syntax is correct and your logic is correct is to:
- Set your campaign to Create Drafts
- Enter a few addresses that will have varying conditions in the “Send Test” box
- Click “Send Test Email”
By doing this, DRAFTS will be created for each address entered. You can use addresses from your actual list that represent different conditions and you can avoid having to use your own addresses to test what the final email will look like, since no actual emails will be sent when testing this way. Remember to set your campaign back to “Send emails” when you’re done testing.
Another way is to create a separate Worksheet in your Google Sheet containing just sample rows using test addresses that you own and then sending a test to the addresses in that Worksheet prior to sending to your full list.
Avoid spaces in variables
In our document on using Google Sheets, in the section on formatting we recommend that column headings contain only alphanumeric characters and no spaces. That means using column headings like “FirstName” rather than “First Name” or “First-Name’. This is to ensure the highest accuracy with personalization as possible. Spaces and non-alphanumeric characters introduce edge cases which can cause unexpected results. For example, if you use “First Name”, and then when you use the merge tag {First Name}, the two words end up getting split onto two different lines, that will break personalization.
Sometimes, however, avoiding a space or non-alphanumeric character isn’t possible and for those situations, you should wrap your variable name in the VAR command. GMass intelligently deciphers first names from email addresses and puts those first names in a variable called auto-first so you can have “Dear {auto-first}” in your email. But to use that variable in conditional logic requires the use of the VAR command because of the hyphen in the variable name.
This will NOT work.
{{ If auto-first != "" Then}} I believe your first name is {auto-first} {{ Else }} I know your email address is {EmailAddress}, but I couldn't figure out your first name. {{ End If }}
{{ If var("auto-first") != "" Then}} I believe your first name is {auto-first} {{ Else }} I know your email address is {EmailAddress}, but I couldn't figure out your first name. {{ End If }}
Error handling
It’s easy to make a mistake when writing conditional code. When you go to send your campaign, if an individual email that’s part of the campaign encounters an error, that email recipient will be skipped and the campaign will continue sending. If you have a syntax error in your scripting, then all emails in the campaign will fail. When 10 consecutive emails error out, then the whole campaign will stop.
You are notified in all cases of any errors in your conditional scripting.
Here are the most common errors.
1. Forgetting the closing {{End If}}
{{If Industry="Recruiting" Then}} Hope the recruiting business is going well and you're placing lots of candidates! [There is no "End If" block] Let's set up a call to catch up. When is good for you?
2. Forgetting to begin/end all code blocks with double braces
{{If Industry="Recruiting" Then}} Hope the recruiting business is going well and you're placing lots of candidates! {{End If} [The ending block has only one curly bracket instead of two.] Let's set up a call to catch up. When is good for you?
3. Forgetting the keyword “Then”
{{If Industry="Recruiting"}} [The word "Then" is missing] Hope the recruiting business is going well and you're placing lots of candidates! {{End If} Let's set up a call to catch up. When is good for you?
The philosophy behind this feature
In building our conditional content feature, we wrote our own compiler and mimicked an old deprecated Microsoft language called VBScript for the commands and syntax. VBScript used to be one of the easiest languages for beginning programmers to learn, and while it isn’t used widely today, its syntax and logic make it the perfect email scripting language given its ease of use.
We also analyzed the conditional content feature of a few other email platforms like Mailchimp, ActiveCampaign, and Lemlist, and found weaknesses in each, so we decided to build something better. I hope you think we succeeded in that mission.
Compatibility with other systems
We’ve specifically chosen the syntax of one curly bracket for mail merge variable substitution and two curly braces for conditional logic to avoid confusion for users who want to personalize with simple mail merge variables and not be bothered with seeing code show up when designing their personalization schemes in email campaigns. This presents a challenge, however, because many other email service providers use the double curly braces syntax for regular old mail merge variable substitution. In Mixmax for example, if you want to personalize with a variable called “firstname”, you would write:
{{firstname}}
Many users copy/paste their email templates from other systems into GMass, and include variables surrounded by double curly braces. To prevent “breaking” these templates, GMass will attempt to determine if the syntax inside double curly braces is truly conditional logic or is just a mail merge variable and will treat each scenario appropriately.
Other personalization options
Conditional content is just one of the many, and the most sophisticated, ways of personalizing your email campaigns. For regular old merge tag substitution personalization, see our guide to personalization. Earlier, I mentioned how you can solve the annoying “space” issue when you don’t have someone’s first name, but we also provide a way of auto detecting first names. For our full set of personalization documentation see the personalization category page on this blog.
Email marketing. Cold email. Mail merge. Avoid the spam folder. Easy to learn and use. All inside Gmail.
TRY GMASS FOR FREE
Download Chrome extension - 30 second install!
No credit card required
Good to have this feature. One thing that bothers me though is that every statement also creates a new line in the email, even when there is no text output.
Yes, that is the case, but you can bunch up the lines of code to prevent that. See how I do that in my recent campaign. Look at the part where I give the subscribe link:
https://www.dropbox.com/s/0h6g8yo4h6wjidi/Conditional-Logic-Announcement-Template-web.png?dl=0
Well yeah, that’s what I did but if you have a lot of logic it becomes pretty confusing. Would be much better if they didn’t automatically create a new line!
To give you a better idea of why I don’t like the way it works now. This is the code I ended up with. All in one line. Not very nice to look at and even worse to work with.
{{If Product2 != “” Then}}{Product2}English:{TextProduct2INT}German:{TextProduct2GER}Price: {PriceProduct2}Weight: {WeightProduct2}{WebsiteProduct2}–{{ End If }}{{If Product3 != “” Then}}{Product3}English:{TextProduct3INT}German:{TextProduct3GER}Price: {PriceProduct3}Weight: {WeightProduct3}{WebsiteProduct3}–{{ End If }}{{If Product4 != “” Then}}{Product4}English:{TextProduct4INT}German:{TextProduct4GER}Price: {PriceProduct4}Weight: {WeightProduct4}{WebsiteProduct4}–{{ End If }}{{If Product5 != “” Then}}{Product5}English:{TextProduct5INT}German:{TextProduct5GER}Price: {PriceProduct5}Weight: {WeightProduct5}{WebsiteProduct5}–{{ End If }}{{If Product6 != “” Then}}{Product6}English:{TextProduct6INT}German:{TextProduct6GER}Price: {PriceProduct6}Weight: {WeightProduct6}{WebsiteProduct6}–{{ End If }}
You actually don’t have to do it that way. You can have each {{…}} statement on its own line, and that WON’T create an extra line of space when the message is rendered. If you are getting an extra line of space by doing that, I want to see the campaign. Contact support (gmass.co/g/support) with the info and mention that you’ve been discussing the issue with me personally here.
that is Great for this function
Is there any way to analyse if include anywords in subject or address and then to do someelse based on the information?
such as:
if subject: “want to buy lamp.”
How we can know if it is include “lamp”, just want client want to buy which product, and send the related product information to them
Thanks.
John
While the Subject itself isn’t a variable that you can use for a condition, if “lamp” came from a merge variable, then you can use that in your condition.
New to mass mailing.Using Gmess is a breeze. These instructions pretty clear. Need to find out what a google sheet is and buy a good CRM..I am in real Estate…Thank you Ayay,,,I have received an email that the letter pops up right away, you dont have to open an attachment…is that something we can do with codes???
which function code do your gmass can support, if all the windows function code can support?
or can you show us which code can support?
such as DATE() Time() also working?
Best Regards
John
There ARE other functions you can use, but I haven’t added them to the documentation yet. I will soon. What are you looking for?
Your examples all use {{Else} i.e. single closing curly bracket – this will throw an error should be {{Else}} I think.
You’re right. Sorry about that. I just fixed it in the two places where I had the typo.
Ajay, woud it be possible to add a condition to show a list of Names & Emails. My goal is to share the names & emails of the indviduals who are a part of the same “Team” as the recipient of the email.
Thank you
Using the code from your Example 1 above, I still get the “Hello ,” when the column is missing.
Could it be connected with Gsheet cell being empty but must be NULL or something?
Many thanks, really love the tool in general!
I have 3 different subject lines. How can I send a mail with any of the random subject lines?
Thanks.
Can GMass write to a linked Google Sheets spread sheet? That is, in the reference about to DaysSinceLastContact, can GMass create and write to the spreadsheet the date that the email was sent to a given row and, likewise, write a column for DaysSinceLastContact?