How to Set Rules for a Mail Merge in Gmail and Google Sheets
A rule tells Mail Merge to change an email based on the values in a row of your sheet. For example, a guest with a confirmed place gets an arrival time, and a guest on the waiting list gets their position on the list. Write your template and add your merge fields first. Then add the rules.
Maya sends the same email before every workshop. Only the date and the teacher change, and neither is in her sheet. A variable holds each value at the top of the message, so Maya types the date once.
Go to Extensions > Mail Merge with Attachments > Open Mail Merge, open your campaign and move to the Template step.
Click at the start of the Message box. Type one {% assign %} tag for each value, with a name on the left and the value in straight quotes:
{% assign day = "Saturday, March 20" %}
{% assign teacher = "Elizabeth" %}
Click where the value should appear and type the name between {? and ?}. Repeat for every place that needs the value.
Your place at the glazing workshop on {? day ?} is confirmed.
Please arrive by 9:30 am. {? teacher ?} starts at 10 am.
Click Send Test Email and send the email to yourself. Every {? day ?} should read Saturday, March 20.
Before the next workshop, Maya changes the two lines at the top. Every sentence that uses {? day ?} or {? teacher ?} changes with them.
Mail Merge doesn’t ask for the value when the campaign runs. Change the {% assign %} tag before you send.
A variable works only in the box where you type the tag. To use the date in the Subject too, type the tag at the start of the subject:
Subject
{% assign day = "Saturday, March 20" %}Glazing workshop on {? day ?}
A waiting list needs its own count. Sofia is in row 4 of the sheet, but she is first on the waiting list. A formula column counts only the rows that say Waitlist.
Type Waitlist # in the first empty cell of row 1. In Maya’s sheet, that cell is G1, and Status is column E.
Select G2, then drag the small blue dot at its bottom-right corner down to the last row. Sofia’s cell shows 1 and Aiko’s cell shows 2. The other cells stay empty.
Write the new column in your template as a merge field:
You are number {{Waitlist #}} on the waiting list.
Sofia reads You are number 1 on the waiting list. Aiko reads You are number 2 on the waiting list.
Mail Merge sends one email for each row. To show several items in one email, keep the items in one cell of that row, with a comma between them. A loop then prints one line for each item.
Angus made three pieces in an earlier class, so his Pieces cell says Mug, Bowl, Vase.
Click where the list should go in the Message box.
Type an {% assign %} tag that cuts the cell into a list at each comma:
{% assign pieces = {{Pieces}} | split: ", " %}
Type the loop. Mail Merge repeats the line between {% for %} and {% endfor %} once for each item:
You will glaze:
{% for piece in pieces %}
{? forloop.index ?}. {? piece ?}
{% endfor %}
Click Send Test Email and check that every item is on its own line.
Angus reads:
You will glaze:
1. Mug
2. Bowl
3. Vase
Daniel has one piece, so his list has one line. {? forloop.index ?} prints the number of each line.
Your items may be in separate rows on another tab, with one row for each piece. A formula can collect each person’s items into one cell.
Say Maya has a Pieces tab with the guest’s email address in column A and the piece in column B. In the Pieces column of her mailing list, she enters this formula in row 2 and fills it down:
FILTER finds every piece whose email address matches B2. TEXTJOIN joins the pieces with commas. IFERROR leaves the cell empty for a guest with no pieces.
When a whole group shares the same list, keep the list in its own tab and add the tab to the email as a table. Maya’s Workshop Plan spreadsheet has a Morning tab and an Afternoon tab. Each tab has that session’s timetable in cells A1 to B5.
Rules work in the message, the subject and the sender name. Mail Merge doesn’t read tags in Send to, CC, BCC or Reply-to.
A tag can’t stop an email. A condition changes the text, but the row still gets the email. Only a hidden row, a filtered row or a SKIP status leaves a row out.
Type tags as plain text with straight quotes. A tag that is partly bold or coloured stops working.
The row number changes when you sort the sheet or insert a row. Sort first, and send the booking numbers once.
Google Sheets doesn’t apply a filter again when a cell changes. If a guest cancels after you set the filter, click the filter icon and click OK again.
An empty list prints no lines. Sofia has no pieces, so a loop over her Pieces cell prints nothing.
A tag has a mistake, so Mail Merge didn’t send the email for that row. The usual cause is an {% if %} with no {% endif %}, or a {% for %} with no {% endfor %}. Fix the template and run the campaign again. See the list of error messages.
Check that the value in quotes matches the cell, with the same capital letters and no extra space. For > or <, the cell must hold a plain number with no comma, currency sign or unit.
Scriptlets ReferenceEvery tag and filter that works in Mail Merge, with an example for each one.