Systems Library / Marketing Automation / How to Build an AI Email Subject Line Generator
Marketing Automation email marketing

How to Build an AI Email Subject Line Generator

Generate high-open-rate subject lines using AI trained on your audience data.

Jay Banlasan

Jay Banlasan

The AI Systems Guy

This ai email subject line generator tool changed how I approach email campaigns. Instead of guessing, I feed in audience data and past performance and the system generates variations matched to what gets opens from your list.

I run this for every send now. It pulls open rate data from previous campaigns, identifies patterns, and creates subject lines that match.

What You Need Before Starting

Step 1: Load Historical Data

import csv

def load_email_history(csv_path):
    campaigns = []
    with open(csv_path) as f:
        reader = csv.DictReader(f)
        for row in reader:
            campaigns.append({
                "subject": row["subject_line"],
                "open_rate": float(row["open_rate"]),
                "send_count": int(row["send_count"]),
            })
    campaigns.sort(key=lambda x: -x["open_rate"])
    return campaigns

history = load_email_history("email_campaigns.csv")
top_performers = history[:10]

Step 2: Extract Patterns from Winners

import anthropic
from dotenv import load_dotenv

load_dotenv()
client = anthropic.Anthropic()

def analyze_patterns(top_campaigns):
    lines = "\n".join([f"- \"{c['subject']}\" ({c['open_rate']}%)" for c in top_campaigns])
    message = client.messages.create(
        model="claude-sonnet-4-20250514",
        max_tokens=1024,
        system="Identify patterns in high-performing email subject lines. Be specific.",
        messages=[{"role": "user", "content": f"Top subjects:\n{lines}\nWhat patterns work?"}]
    )
    return message.content[0].text

Step 3: Generate New Subject Lines

def generate_subjects(topic, patterns, count=10):
    message = client.messages.create(
        model="claude-sonnet-4-20250514",
        max_tokens=1024,
        system="Generate email subject lines under 50 chars. No clickbait. Match provided patterns.",
        messages=[{"role": "user", "content": f"Topic: {topic}\nPatterns: {patterns}\nGenerate {count} variations."}]
    )
    return message.content[0].text

Step 4: Score and Rank

def score_subject(subject, patterns):
    message = client.messages.create(
        model="claude-sonnet-4-20250514",
        max_tokens=256,
        system="Score this subject line 1-10 based on patterns. Number and one sentence.",
        messages=[{"role": "user", "content": f"Subject: {subject}\nPatterns: {patterns}"}]
    )
    return message.content[0].text

What to Build Next

Connect this to your ESP API so generated subject lines feed directly into A/B test slots. Track results monthly and retrain the pattern analysis.

Related Reading

Want this system built for your business?

Get a free assessment. We will map every system your business needs and show you the ROI.

Get Your Free Assessment

Related Systems