# MailPreflight Email QA & Deliverability API (v1.0.0)

> Catch broken merge tags, Gmail 102KB clipping, Outlook CSS bugs, and spam triggers before your agent hits send.

Deterministic pre-send QA for outbound HTML emails: catches Gmail 102KB clipping, Outlook Word-engine & Gmail CSS incompatibilities, unmerged ESP placeholders, phishing/shortener link triggers, and generates clean plain-text MIME fallbacks at the edge.

## Why Agents Use This API Instead of DIY
Replaces brittle regexes and heavyweight headless browsers with a <15ms deterministic HTML/CSS & deliverability parser covering Outlook Word VML/ghost-table quirks, 6 ESP placeholder syntaxes, phishing host-mismatch checks, and RFC 5322 plain-text conversion.

## Connection & Payment (x402 Default + Optional Bearer API Key)
- **MCP Server (Streamable HTTP):** `https://mail-preflight.apikeg.com/mcp` (Registry ID: `com.apikeg/mail-preflight` — `initialize`, `tools/list`, `resources/*`, and `prompts/*` are free)
- **OpenAPI 3.1 Spec:** [openapi.json](https://mail-preflight.apikeg.com/openapi.json)
- **Full Schema & Examples:** [llms-full.txt](https://mail-preflight.apikeg.com/llms-full.txt)
- **RFC 9727 API Catalog:** [.well-known/api-catalog](https://mail-preflight.apikeg.com/.well-known/api-catalog)
- **x402 Bazaar Manifest:** [.well-known/x402.json](https://mail-preflight.apikeg.com/.well-known/x402.json)
- **MCP Discovery Manifest:** [.well-known/mcp.json](https://mail-preflight.apikeg.com/.well-known/mcp.json)
- **A2A Agent Card:** [.well-known/agent-card.json](https://mail-preflight.apikeg.com/.well-known/agent-card.json)
- **Primary Payment (Zero Signup):** Cloudflare Monetization Gateway (`HTTP 402` / `x402` v2 with USDC on Base).
- **Optional Monthly API Key:** Pass `Authorization: Bearer ak_live_...` ($5 / $20 / $50 monthly per-product Stripe plans at [apikeg.com/account](https://apikeg.com/account)).
- **Free Preflight:** Append `?dry_run=true` (or header `X-Dry-Run: true`) to validate any request payload at `$0.00` cost.

## Tools & REST Endpoints
- [`audit_email_template`](https://mail-preflight.apikeg.com/tools/audit-email-template.md) (`POST https://mail-preflight.apikeg.com/v1/audit-email-template`) — $0.003 USD per email template preflight audit (x402 instant micropayment): Runs a full pre-send deliverability, merge-tag, link-hygiene, Gmail 102KB clipping, and cross-client CSS compatibility audit (Outlook Windows, Gmail, Apple Mail Dark Mode) on an HTML email.
- [`extract_plain_text_fallback`](https://mail-preflight.apikeg.com/tools/extract-plain-text-fallback.md) (`POST https://mail-preflight.apikeg.com/v1/extract-plain-text-fallback`) — $0.001 USD per HTML-to-plain-text MIME fallback conversion (x402 instant micropayment): Converts an HTML email into a clean, RFC 5322-wrapped text/plain MIME companion, preserving CTA links inline as 'Label (https://...)' and stripping invisible &#847; / &zwnj; preheader padding hacks.

## Instant MCP Configuration
```json
{
  "mcpServers": {
    "mail-preflight": {
      "type": "streamable-http",
      "url": "https://mail-preflight.apikeg.com/mcp"
    }
  }
}
```

## Common Agent Use Cases
- HTML email preflight checker API
- check Gmail 102KB email clipping MCP
- Outlook HTML email CSS compatibility validator
- detect unmerged email template variables before send
- convert HTML email to plain text MIME fallback API
- cold outreach spam trigger checker MCP

---

## Detailed Tool Specifications

### `audit_email_template` — Audit Outbound Email Template
- **REST Endpoint:** `POST https://mail-preflight.apikeg.com/v1/audit-email-template`
- **Tool Doc Page:** https://mail-preflight.apikeg.com/tools/audit-email-template
- **MCP Tool Name:** `audit_email_template`
- **Pricing:** $0.003 USD per email template preflight audit (x402 instant micropayment)
- **Side Effects:** readOnly=true, idempotent=true, destructive=false
- **Build vs. Buy ROI:** Audits 30+ email-client CSS quirks, 6 ESP merge syntaxes, and phishing/spam heuristics in <10ms for $0.003 without maintaining Can-I-Email rulesets.

#### Input JSON Schema
```json
{
  "type": "object",
  "properties": {
    "html": {
      "type": "string",
      "minLength": 1,
      "maxLength": 900000,
      "description": "Complete outbound HTML email payload to inspect for Gmail 102KB clipping, Outlook/Gmail/Dark Mode CSS support, link hygiene, and spam triggers."
    },
    "subject": {
      "type": "string",
      "maxLength": 998,
      "description": "Optional Subject header evaluated for unmerged placeholders, fake Re:/Fwd: prefixes, ALL-CAPS shouting, and punctuation triggers."
    },
    "preheader": {
      "type": "string",
      "maxLength": 1000,
      "description": "Optional preview/preheader snippet checked for unmerged merge tags and spam triggers."
    },
    "plainText": {
      "type": "string",
      "maxLength": 500000,
      "description": "Optional text/plain MIME companion body checked for unmerged placeholders and unsubscribe compliance."
    },
    "emailCategory": {
      "type": "string",
      "enum": [
        "transactional",
        "marketing",
        "cold_outreach"
      ],
      "default": "transactional",
      "description": "Outbound email class controlling compliance rules (marketing and cold_outreach require an unsubscribe mechanism)."
    }
  },
  "required": [
    "html"
  ],
  "additionalProperties": false
}
```

#### Examples
**Marketing email preflight** — Audits a campaign HTML payload with subject and preheader before dispatch.
```bash
curl -X POST "https://mail-preflight.apikeg.com/v1/audit-email-template" \
  -H "Content-Type: application/json" \
  -d '{"subject":"Welcome to Acme, {{first_name}}!","preheader":"Your workspace is provisioned.","emailCategory":"marketing","html":"<!DOCTYPE html><html><head><meta name=\"color-scheme\" content=\"light dark\"></head><body style=\"background-color:#f9fafb;\"><p>Hi there, <a href=\"https://acme.com/start\">Launch Workspace</a> · <a href=\"https://acme.com/unsubscribe\">Unsubscribe</a></p></body></html>"}'
```
Expected result: Flags unmerged {{first_name}} in subject as blocked with prioritized actionableFixes.

### `extract_plain_text_fallback` — Extract Plain-Text MIME Fallback
- **REST Endpoint:** `POST https://mail-preflight.apikeg.com/v1/extract-plain-text-fallback`
- **Tool Doc Page:** https://mail-preflight.apikeg.com/tools/extract-plain-text-fallback
- **MCP Tool Name:** `extract_plain_text_fallback`
- **Pricing:** $0.001 USD per HTML-to-plain-text MIME fallback conversion (x402 instant micropayment)
- **Side Effects:** readOnly=true, idempotent=true, destructive=false
- **Build vs. Buy ROI:** Strips email-specific &zwnj;/&#847; preheader spacers and formats multi-column/CTA HTML cleanly in <5ms for $0.001 instead of naive strip-tags regexes.

#### Input JSON Schema
```json
{
  "type": "object",
  "properties": {
    "html": {
      "type": "string",
      "minLength": 1,
      "maxLength": 900000,
      "description": "HTML email body to convert into a formatted text/plain MIME fallback with inline CTA URLs and stripped preheader padding."
    },
    "wrapWidth": {
      "type": "integer",
      "minimum": 40,
      "maximum": 120,
      "default": 78,
      "description": "Soft line-wrap width in characters (default 78 per RFC 5322; URLs are never split)."
    }
  },
  "required": [
    "html"
  ],
  "additionalProperties": false
}
```

#### Examples
**Transactional receipt HTML to text/plain**
```bash
curl -X POST "https://mail-preflight.apikeg.com/v1/extract-plain-text-fallback" \
  -H "Content-Type: application/json" \
  -d '{"html":"<div>Preview text &zwnj;&nbsp;&#847;&zwnj;&nbsp;&#847;</div><h1>Payment Received</h1><p>View your invoice: <a href=\"https://acme.com/inv/42\">Download PDF</a></p>","wrapWidth":78}'
```
Expected result: Returns clean wrapped text with 'Download PDF (https://acme.com/inv/42)' and zero invisible spacer characters.
