Getting AI to Write Email HTML That Doesn't Break

Enforcing constraints through prompts and validating generated output before use

Prompt DesignHTML GenerationValidationLLMTemplate
5 min read

Introduction

For the record, getting AI to write HTML isn't hard at all. Ask it to "make newsletter HTML" and something plausible comes back right away. The problem is what's inside. Left unchecked, AI will cheerfully use approaches that are correct for web development but break in email — div layouts and external CSS.

AI is good at "ordinary HTML," but the special rules of "email-safe HTML" aren't honored unless you explicitly teach them. This article covers the design for making AI honor email constraints and validating the output before use. Reading Why Email HTML Is Special first will make the constraints themselves clearer.

Enforce the constraints through prompts

Spell out the rules and hand them over

The first step to having AI write email-safe HTML is listing the constraints you want honored, concretely, in the prompt. A vague instruction like "make HTML that doesn't break in email" isn't enough. Lay out each item explicitly: lay out with tables, make all styles inline, no div-based columns.

The checklist from the previous article becomes the raw material for the prompt as-is. Because the rules are clear, the instructions can be written clearly too.

Specify "what not to do" strongly

What's interesting is that "don't ever do this" tends to work better than "do this." Since AI drifts toward modern approaches when it relaxes, we emphasize prohibitions: no div layouts, no external CSS, no <style> tags, no JavaScript.

Making the prohibitions crisp reduces accidents where AI uses the latest technique "with good intentions."

Fix the output format

It's also important to generate only the body and keep AI's hands off the header and footer. Limit the output scope to "the body table block only" and instruct it not to add extraneous explanations or markdown. That way the output lines up in a form you can slot straight into the template.

Validate the output before using it

Prompts alone can't fully guarantee it

No matter how carefully you instruct the constraints, AI behaves probabilistically, so it occasionally breaks the rules. So rather than trusting that "it's fine because I asked in the prompt," we always insert a step that mechanically validates the output. Prevent with the prompt, catch with validation — a two-layer defense.

What the validation checks

Validation automatically checks whether the constraints are honored. Are any prohibited tags mixed in? Are styles properly inline? Are all images absolute URLs? Is the width within the expected range? These are items you can judge mechanically. If a problem turns up here, you can catch it before sending.

Generation and validation flow
Generate with a constraint-laden prompt

Generate the HTML body with a prompt that spells out the prohibitions.

Run automatic validation

Check for prohibited tags, inline styling, absolute URLs, width, and more.

Regenerate or fix on failure

On a violation, regenerate with the issue noted, or correct it in the system.

Compose into the template

Slot the validated body into the per-brand template.

Recovery when there's a violation

When validation returns a failure, rather than just stopping, we have AI redo it with the violation attached. Returning concrete feedback like "a div was used, change it to a table" makes the next generation more likely to improve. For simple issues, the system sometimes corrects them mechanically.

Combine with templates

AI builds only the body

In this system, we don't have AI build the whole thing — what it generates is limited to the body. The header and footer have fixed designs per brand, so we prepare them as templates and compose by sandwiching in the validated body. The narrower AI's reach, the lower the risk of breakage.

Automatic per-brand templates

There are two brands, each with a different header and footer design. Specify a brand and the corresponding template is chosen automatically and composed with the body. The producer just picks a brand, and the system assembles it into the correct form.

Factual data isn't touched here

Note that data requiring accuracy — like prices and image URLs — is not handled in this generation-and-validation step. Those are fetched from the product master and inserted by the system, precisely so AI doesn't invent them. That mechanism is covered in Product Data Integration and a Review-First Workflow.

Summary

The trick to getting AI to write unbreakable email HTML is to not "ask and be done," but to design prevention and detection as separate things.

  • Don't ask vaguely; spell out the constraints as prohibitions and hand them to the prompt.
  • Don't trust the prompt alone — mechanically validate the output and catch it before sending.
  • On a violation, provide recovery that regenerates or corrects with the issue attached.
  • AI builds only the body; headers and footers are composed via per-brand templates.

The email HTML constraints themselves are in Why Email HTML Is Special, and product data insertion and the production flow are explored in Product Data Integration and a Review-First Workflow.