# Building an email that stays editable

If you haven't read the `assistant` guide in this conversation yet, read it first. It covers how to work with the person.

Emails on this site are edited in a drag-and-drop block editor. To make an email the site owner can open and keep editing there, give `create-email-template` (or `update-email-template`) the body as `blocks`: a JSON array of block specs. The markup, block CSS and plain text are written for you, the same way the editor writes them. Plain HTML (`content` with `editor: "html"`) also sends fine, but it opens in a code editor instead, and the person can't edit it with blocks. Always use blocks, unless the person asks for an HTML email outright or gives you HTML they want kept as it is.

## Before you write

1. Look at one or two existing block emails so the new one looks like it belongs to this site. Find them with `list-email-templates` (without `expand`) and read one with `get-email-template` and `view: "specs"`. Reuse their structure, fonts and colors and swap in new copy.
2. Call `list-replacement-codes` for the merge tags this site supports. Use them for greetings, sign-offs and compliance links instead of static text.
3. Call `list-sender-profiles` if the email should come from someone other than the default sender.
4. Call `get-setup-status` and look at `languages`. If `site` lists more than one, write the email in the `default` language, then ask whether it should be translated into the site's other languages. Don't translate it without being asked to. See Variations below.

{{email_design}}

## Block specs

A spec is a block's `type` and only the settings that differ from the defaults. Settings you leave out get this site's block defaults. Give an `id` (letters, digits and hyphens, like `intro` or `cta`) to blocks you may want to change later; otherwise one is made for you.

```json
[
  { "type": "image", "src": "https://example.com/logo.png", "alt": "Acme", "width": 140 },
  { "type": "heading", "id": "title", "tag": "h1", "content": "The October roundup", "fontStyle": { "fontSize": 36 } },
  { "type": "text", "id": "intro", "content": "<p>Hi {first::there},</p><p>Here's what's new this month.</p>" },
  { "type": "button", "id": "cta", "text": "Read the blog", "link": "https://example.com/blog" },
  { "type": "columns", "columns": [
    [ { "type": "image", "src": "https://example.com/a.png", "alt": "Feature A", "width": 280 },
      { "type": "text", "content": "<p><strong>Feature A</strong> does this.</p>" } ],
    [ { "type": "image", "src": "https://example.com/b.png", "alt": "Feature B", "width": 280 },
      { "type": "text", "content": "<p><strong>Feature B</strong> does that.</p>" } ]
  ] },
  { "type": "social" },
  { "type": "footer", "alignment": "center" }
]
```

Colors are hex (`#1e73be`), or `palette:N` for this site's palette colors. Specs with a mistake (an unknown type, a taken id, a bad layout) are refused with what's wrong, and nothing is saved.

### Every block

- `advancedStyle`: the block's box. `padding` (a number for every side, `"10px 20px"`, or `{top, right, bottom, left}`; 5 by default), `backgroundColor`, `borderStyle` (`solid`, `dashed`...), `borderColor`, `borderWidth`, `borderRadius`.
- `hide_on_mobile`, `hide_on_desktop`, `hide_in_plain`, `hide_in_browser`: true to hide it there.
- `css`: custom CSS for the block, where `selector` stands for the block, like `selector p { letter-spacing: 1px; }`.

Font settings (`p`, `fontStyle`, `style`...) are objects of `fontSize` (pixels), `fontFamily`, `fontWeight`, `lineHeight`, `fontStyle` (`normal`, `italic`), `textTransform` and `color`. `fontFamily` is one of the editor's fonts ({{email_fonts}}), each written with fallbacks for mail apps that don't have it, or a font uploaded on this site (see "This site's design"); others become Arial. `use` takes the name of one of the site's global fonts instead.

### Types

| type | settings |
|---|---|
| `text` | `content`: HTML with `p`, `h1`-`h3`, `ul`/`ol`/`li`, `a`, `strong`/`b`, `em`, `br`. Font settings for its tags: `p` (also lists), `a` (links, usually just `color`), `h1`, `h2`, `h3`. The fonts are added to each tag as inline styles, so don't write those yourself; a tag's own `style`, like `text-align`, is kept. |
| `heading` | `content` (inline HTML), `tag` (`h1`-`h6`, default `h2`), `alignment` (`left`, `center`, `right`), `fontStyle`. The size doesn't follow the tag: set `fontStyle.fontSize` for a big `h1`. |
| `button` | `text`, `link`, `align` (`left`, `center`, `right`, or `justify` for full width), `size` (`sm`, `md`, `lg`), `backgroundColor`, `style` (font settings, `color` is the text color), `borderStyle` (`{borderStyle, borderColor, borderWidth, borderRadius}`). |
| `image` | `src`, `alt`, `width` (pixels), `link`, `align`, `borderStyle` (as for a button; `{"borderRadius": 8}` for rounded corners). |
| `video` | A video's thumbnail, linked to the video, since emails can't play video. `url`: a YouTube, Vimeo, Wistia or Rumble link; the site makes the thumbnail, so leave `src` out. `playButton`: `{style, color, iconColor, size, opacity}`, `style` one of `circle` (default), `outline`, `pill`, `box`, `triangle` or `none`, `size` percent of the width (default 18), `opacity` percent of `color`. `progressBar`: `{style, color, progress}`, `style` one of `none` (default), `line`, `scrubber`, `player`, `progress` percent. `darken` (percent, darkens the thumbnail). `thumbnail`: an image URL to use instead of the video's thumbnail. `alt` (the video's title by default), `width`, `align`, `link` (the video by default), `borderStyle`. |
| `spacer` | `height` (pixels). |
| `divider` | `height` (line thickness), `width` (percent), `color`, `lineStyle` (`solid`, `dashed`, `dotted`, `double`, `ridge`, `groove`). |
| `columns` | `columns`: an array of columns, each an array of specs. `layout`: `one_column`, `two_columns` (50/50), `three_columns`, `four_columns`, `two_columns_left` (66/33), `two_columns_right` (33/66), `three_columns_center` (25/50/25), `three_columns_left` (50/25/25), `three_columns_right` (25/25/50), or `custom_columns` with `customColumns` widths in percent, like `[30, 70]`. `gap` (pixels between columns), `verticalAlign` (`top`, `middle`, `bottom`), `responsive` (stack on phones, default true), `columnStyle` (`{backgroundColor, padding, borderStyle...}` for every column that has blocks). |
| `menu` | `links`: `[["Blog", "https://..."], ["Shop", "https://..."]]`, `align`, `gap`, `separator` (text between links, like `"|"` or `"&bull;"`), `fontStyle`. |
| `social` | `use`: `"global"` (default, the site's own social accounts) or `"custom"` with `socials`: `[["facebook", "https://..."], ...]`. `theme`, `size` (pixels), `gap`, `align`. Icons: facebook, instagram, linkedin, pinterest, reddit, threads, tiktok, tumblr, twitch, twitter, vimeo, whatsapp, wordpress, youtube, github, truthsocial, odysee, discord, rumble, bluesky. Themes: `brand-circle`, `brand-boxed`, `brand-icons`, and `black-`, `dark-grey-`, `grey-`, `white-` with `circle`, `boxed` or `icons`. |
| `html` | `content`: raw HTML for anything the other blocks can't do. Scripts, forms, iframes and video are taken out. |
| `footer` | The business name, address, phone, privacy policy and terms links and the unsubscribe link, from the site's settings. `alignment`, `style` (font settings), `linkStyle` (`{color}`), and `tel`, `privacyPolicy`, `terms` (false to leave that link out). There's no unsubscribe line in a transactional email. |
| `global` | `templateId`: the ID of a global block (a saved reusable block) that exists on the site. It's put in when the email is sent. |

### Images

Where images come from, in this order:

1. Images you were given, or that the site's existing emails already use.
2. A relevant image from the site's media library. Search it with your connection's own media library tools if it has them, otherwise with `search-media`.
3. An image that's online and that you're allowed to use: add it to the media library with `upload-media`, giving its `url` so the site downloads it, and use the URL it returns. Never write base64 yourself, it won't be a valid image.
4. Otherwise a placeholder from placehold.co, the size of the image it stands in for, labelled with what belongs there: `https://placehold.co/600x300/png?text=Product+photo`. Keep `/png` in the URL, the default is SVG and many email clients don't show SVG images. Write spaces in the label as `+`. Full width is 600 wide, half of a two column row about 280, a third about 180.

Never make up an image URL or link to an image on someone else's site. When you use placeholders, say which images need replacing.

## Changing an email

Don't read or rewrite the whole email to change part of it. Use `edit-email`, which changes blocks by id:

1. `get-email-template` returns an outline: each block's `id`, `type`, the `parent` and `column` of blocks in columns, and a `summary` of its text, image or link. It also returns the email's `version`. With `view: "specs"` it returns the blocks as specs instead, and with `blocks` (ids) only those. `find` returns where some text or HTML is and which block it's in.
2. Call `edit-email` with `expected_version` and a list of `operations`, applied in order, all or nothing:

```json
{
  "email_id": 12,
  "expected_version": "…",
  "operations": [
    { "op": "replace_text", "find": "Sale ends Friday", "replace": "Sale ends Sunday" },
    { "op": "replace_text", "block": "cta", "find": "https://example.com/old", "replace": "https://example.com/new" },
    { "op": "replace", "block": "cta", "blocks": [ { "type": "button", "id": "cta", "text": "Shop now", "link": "https://example.com/shop", "size": "lg" } ] },
    { "op": "add", "at": { "after": "intro" }, "blocks": [ { "type": "spacer", "height": 20 } ] },
    { "op": "move", "block": "footer", "at": { "position": "end" } },
    { "op": "delete", "block": "old-banner" }
  ]
}
```

- `replace_text` is the cheapest change. It must match exactly once (give `block` to narrow it down, or `all: true`), and it matches the HTML as written, so text split by tags like `<strong>` won't match.
- To change a block's settings, get its spec (`view: "specs"`, `blocks: [id]`), change it, and `replace` the block with it, keeping its id.
- `add` and `replace` take new blocks as `blocks` (specs). `add` and `move` take `at`: `{"after": id}`, `{"before": id}`, `{"in": id, "column": 0}` for a column of a columns block, or `{"position": "start"}` / `{"position": "end"}`.
- `set_content` replaces the HTML inside a block's `<td>` as it is, without styling it. `set_props` changes settings in a block's comment that are read when the email is sent, like a global block's `templateId`.
- Use `dry_run: true` to check a change without saving it.

The result lists the blocks that changed and the new `version` for the next call. Use `update-email-template` for the subject, preview text, sender, status and other settings.

### Emails that aren't built with blocks

An email from the HTML editor has no blocks. `get-email-template` returns its visible text as `text`, a line for each paragraph, heading or cell, with links as `[text](href)` and images as `![alt](file)`, instead of the HTML. Change it with `edit-email` too:

```json
{
  "email_id": 37,
  "expected_version": "…",
  "operations": [
    { "op": "replace_text", "find": "ends Sunday", "replace": "ends Monday" },
    { "op": "replace_html", "find": "href=\"https://example.com/fall\"", "replace": "href=\"https://example.com/fall-sale\"" }
  ]
}
```

- `replace_text` only changes visible text, never tags, attributes or the CSS, so it's the safe way to change wording.
- `replace_html` replaces an exact piece of the HTML: a link, an image, a style, or a whole `<tr>…</tr>`. Copy it from `get-email-template` with `find`, which returns each place some text or HTML is with the HTML around it, exactly as saved. A piece that opens a tag has to close it.
- Both must match exactly once, unless `all: true`.

## Variations

A variation is another version of an email, like the email in another language, sent instead of it to the contacts in the variation's audience. Flows, broadcasts and notifications keep sending the original email: each contact gets the first ready variation whose audience they're in, or the original. Its opens and clicks count towards the original email's reports too.

To translate an email:

1. `get-setup-status` lists the site's languages under `languages`, with how many contacts speak each one. A contact's language is their `locale`, like `fr_FR`.
2. `create-email-variation` with the `email_id` and `audience: {"locales": ["fr_FR"]}`. It returns a draft copy of the email with the same block ids.
3. `get-email-strings` on the variation's `id` gives its text as strings by key: the subject, preview text, each paragraph, heading, list item, button and menu label, the text in HTML blocks, and image and video alt text. Inline tags are placeholders like `<1>bold</1>` and `<2/>`. The footer isn't in the strings: it's made in the variation's language when the email is sent.
4. Translate them all in one `set-email-strings` call: `{"email_id": 41, "strings": {"subject": "…", "text-cc143b": "Bonjour <1>{first}</1>,"}}`. Keep every placeholder exactly once, around the words it belongs to, and keep merge tags like `{first}` as they are. A string that's the same in both languages, like a name, is given its own text to mark it translated. The strings don't include where links go or which images are shown: when a link goes to a page that has a version in the variation's language, like `/fr/pricing/`, point it there with `edit-email`, and swap an image that has text in it. Ask for the translated page's address if you can't find it, don't guess it.
5. For a language written right to left, like Arabic, Hebrew, Persian or Urdu, set the variation's direction with `update-email-template` and `template_settings: {"direction": "rtl"}`. A copy starts with the original's direction.
6. Check it with `send-test-email` on the variation, then make it ready with `update-email-template` and `status: "ready"`. A draft variation is never sent.

The strings don't cover global blocks or dynamic blocks like posts and shortcodes, so `get-email-strings` and `set-email-strings` list those under `not_translated`: when you say a translation is done, name each one and its `note` as left to check by hand, rather than calling the email fully translated.

Keeping translations up to date: after the original email changes, `get-email-strings` with `needs_translation: true` on a variation lists only what to translate again (`status: "changed"`, with the new `source`), and `missing` lists text in blocks the email has that the variation doesn't; add those blocks to the variation with `edit-email`. `set-email-strings` says how many are `left_to_translate`.

- A language's regional varieties share a variation unless they have their own: a contact who speaks `fr_CA` gets the `fr_FR` variation when there's none for `fr_CA`, so one variation a language is enough. Add one for a region only when its wording should differ. The original email counts as the site's default language, so on an `en_US` site an `en_GB` contact gets the original.
- The audience takes the same filters as a segment in `search-contacts`, for a variation that isn't about language, like `{"tags_include": ["VIP"]}`. Everything given must match. A contact in more than one audience gets the one with the lowest `priority`.
- `get-email-template` on an email lists its `variations`; on a variation it gives `variation_of` and its `variation` settings. `update-email-variation` changes the audience, priority or language, and `delete-email-variation` removes one.
- `resolve-email-variation` says which version a contact would get, and why each variation was or wasn't chosen.
- A variation can't have variations of its own, and templates and global blocks can't have any.

### A/B tests

A test version is a variation with no audience: `create-email-variation` with `kind: "test"`. The contacts no audience version is for are split between the email (version A) and its ready test versions by `weight`, evenly unless you say otherwise, and each contact always gets the same one.

1. Create the test version, change what's being tested (often just the subject, with `update-email-template`), and make it ready.
2. `get-email-variation-report` shows each version's sent, opened and clicked, from when the test started, which one is ahead, and how confident that is. It suggests a winner once it's 95% sure; until then, keep the test running. Judge by clicks unless asked otherwise: some email apps open every email.
3. `declare-email-winner` ends it: everyone in the test gets the winner from then on. Only declare one the report suggests, or that the person you're working for picks. `winner_id: 0` starts the test over.

For a broadcast, `send-email-broadcast` with `ab_test: {"sample_percent": 20, "wait_hours": 4}` sends the test to a random sample, then picks the version that did best and broadcasts it to everyone else on its own. The wait starts once the last of the sample has gone out, so with batching or the contact's local time the winner goes out later than `wait_hours` after the start.

## Merge tags

Merge tags are resolved for each recipient when the email is sent, in the subject, preview text and content.

- `{first}`, `{last}`, `{full_name}`, `{email}`: the contact's details.
- `{code::fallback}` gives a fallback for an empty value: `Hi {first::there},`.
- `{code.argument}` passes an argument: `{meta.company_name}` for a custom field, `{date.F j, Y|+2 days}` for a date.
- `{business_name}`, `{business_address}`, `{business_phone}`, `{site_url}`: from the site's settings.
- `{owner_first_name}`, `{owner_email}`, `{owner_signature}`: the contact's owner, useful for personal sign-offs.
- `{unsubscribe_url}` and `{view_in_browser_url}` are bare URLs, so wrap them in a link: `<a href="{unsubscribe_url}">Unsubscribe</a>`.
- `{confirmation_link}` is a complete link already; `{confirmation_url}` is the bare URL for a button.

`list-replacement-codes` has every code on this site, including ones add-ons provide.

## Good practice

- Every marketing email needs an unsubscribe link and the business address. End it with a footer block.
- One clear call to action, usually a button. Keep the copy short.
- Write a subject line under about 50 characters and a `preview_text` that adds to it rather than repeating it.
- Don't make up facts, prices, dates, testimonials or links. Where something is missing, leave a clear placeholder like `[LINK TO OFFER]`, or a placehold.co image, and say where it is.
- Save as `status: "draft"` unless asked otherwise, then send a test with `send-test-email` and share the email's edit link. Both kinds of test work on a draft: a design test (the default) shows how it looks; `type: "simulated"` sends it the way a broadcast or flow would, so links, tracking, personalization and conditional blocks can be checked too. Send tests only to the person you're working for.
- Never send it to contacts (`send-email-template`, `send-email-broadcast`) unless the person you're working for asks you to, with that audience.

## The markup, for reference

You don't need to write it, but this is what the content looks like: a `<table>` with a `<tr>` per block, each block between comments that carry its type, id and the settings that aren't in its HTML, around a `<td id="b-{id}">` with the block's HTML. `get-email-template`'s `blocks` input returns a block's markup, and `edit-email` also takes markup as `html`, in this format:

```html
<table cellpadding="0" cellspacing="0" width="100%" role="presentation">
<tr><!--button:cta {"size":"md"}--><td id="b-cta" class="b-button" ...>...</td><!--/button:cta--></tr>
</table>
```
