This document provides a complete guide to the AI-powered email features now integrated into your application.
The application now includes two powerful AI-driven features for email marketing:
- AI Email Generation - Automatically generate professional email content using OpenAI's GPT
- Topic Analysis - Discover hidden topics and themes in your campaign emails using machine learning (LDA)
These features are seamlessly integrated into the email creation and analysis workflows.
Purpose: Generate professional, engaging email content with customizable tone and style.
Key Features:
- 🎯 Smart Subject Lines: Auto-generate compelling subject lines
- 📝 Professional Body Content: HTML-formatted email body with proper structure
- 🎨 Customizable Tone: Professional, Friendly, Persuasive, Casual, or Formal
- 📏 Adjustable Length: Short, Medium, or Long email formats
- 💾 Direct Save: Generated content can be saved directly to email templates
- 🔄 Iterative Refinement: Generate multiple times with different parameters
Required: OpenAI API Key (GPT-3.5-turbo or later)
Purpose: Automatically discover the main themes and topics discussed across your campaign emails.
Key Features:
- 🔍 Automatic Discovery: Identifies 2-10 distinct topics using Latent Dirichlet Allocation (LDA)
- 📊 Visual Results: Charts and tables showing topic distribution
- 🎯 Email Mapping: Shows which topic dominates each email
- 📈 Keyword Extraction: Lists top keywords for each discovered topic
- 💡 No Training Needed: Works with raw email content, no manual labeling required
No API Required: Works offline using machine learning algorithms
- OpenAI API Key - Get one at https://platform.openai.com/api-keys
- Required models:
gpt-3.5-turboor newer - Estimated cost: ~$0.002 per email generated (highly variable)
- Required models:
- Python Libraries (already installed in requirements.txt):
nltk- Natural Language Toolkitgensim- Topic modeling librarypandas- Data manipulation
Option A: Environment Variable (Recommended for production)
# On Windows
set OPENAI_API_KEY=your-api-key-here
# On macOS/Linux
export OPENAI_API_KEY=your-api-key-here
# In Docker or production environments
# Set via environment variable managementOption B: Environment File (.env)
# In your project root .env file
OPENAI_API_KEY=sk-...your-key-here...All required packages are already listed in requirements.txt:
pip install -r requirements.txtSpecific AI packages installed:
openai==1.3.9- OpenAI API clientnltk==3.8.1- Natural Language Processinggensim==4.3.2- Topic Modelingpandas==2.1.4- Data Analysis
The EmailAIAnalysis model stores AI results. Migration already applied:
python manage.py migrate campaigns- Go to Campaigns → Create Campaign (or edit existing)
- In the email template editor, click ✨ AI Generate button
- Email Topic/Subject: What the email is about (e.g., "New product launch", "Summer sale")
- Recipient: Who the email is for (e.g., "customer", "developer", "subscriber")
- Tone: How the email should sound
- Professional: Formal, business-like
- Friendly: Warm, conversational
- Persuasive: Compelling, call-to-action focused
- Casual: Relaxed, informal
- Formal: Official, institutional
- Length: Email length
- Short: 50-100 words
- Medium: 150-250 words
- Long: 300+ words
- Additional Context (Optional): Any specific details or requirements
- Click ✨ Generate
- Wait for AI to process (usually 5-15 seconds)
- Review the generated subject and body
- Make edits as needed
- Click Save Template to save
- Go to Campaigns → Email Analysis (new page)
- Or visit
/campaigns/email-analysis/
- Choose the campaign to analyze from the dropdown
- Adjust Number of Topics slider (2-10 topics)
- Click 📊 Analyze Topics
- Wait for analysis (30-60 seconds depending on email count)
- Results will show:
- Topic Cards: Each topic with top keywords and weights
- Email-Topic Table: Which topic dominates each email
- Distribution Chart: Visual breakdown of topics
- High confidence value (>0.7): Email clearly matches the topic
- Lower confidence value (<0.3): Email touches multiple topics
- Keywords: Most representative words for each topic
POST /api/campaigns/{campaign_id}/generate-email/
Request Body:
{
"subject_topic": "New product launch",
"recipient": "customer",
"tone": "professional",
"length": "medium",
"context": "We have a new AI-powered feature",
"email_id": null
}Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
| subject_topic | string | Yes | Topic or subject of the email |
| recipient | string | No | Target recipient type (default: "subscriber") |
| tone | string | No | Email tone (default: "professional") |
| length | string | No | Email length (default: "medium") |
| context | string | No | Additional context for generation |
| email_id | uuid | No | If provided, generated content is saved to this email |
Response (Success):
{
"message": "Email generated successfully",
"subject": "Introducing Our Latest Innovation",
"body_html": "<p>Dear Valued Customer,</p>...",
"saved": false
}Response (Error):
{
"error": "OPENAI_API_KEY environment variable not set",
"hint": "Please set OPENAI_API_KEY environment variable"
}POST /api/campaigns/{campaign_id}/analyze-topics/
Request Body:
{
"num_topics": 5,
"num_words": 5
}Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
| num_topics | integer | No | Number of topics to extract (2-10, default: 5) |
| num_words | integer | No | Words per topic to display (default: 5) |
Response (Success):
{
"message": "Topic analysis completed successfully",
"success": true,
"email_count": 12,
"topics": [
{
"topic_id": 0,
"top_word": "product",
"keywords": ["product", "feature", "launch", "new", "available"],
"weights": [0.0456, 0.0312, 0.0289, 0.0267, 0.0245]
}
],
"dominant_topics": [
{
"topic_id": 0,
"confidence": 0.7823
}
]
}Response (Error):
{
"error": "No emails in campaign to analyze",
"topics": [],
"email_count": 0
}campaigns/
├── ai_utils.py # Core AI utility functions
├── models.py # Database models (added EmailAIAnalysis)
├── views.py # API endpoints and view functions
├── urls.py # URL routing
└── migrations/
└── 0006_emailaianalysis.py # Database migration
templates/campaigns/
├── template.html # Email editor (updated with AI button)
└── email_analysis.html # Topic analysis interface
generate_email_content()
- Uses OpenAI API to generate email content
- Input: topic, recipient, tone, length, context
- Output: Dict with 'subject' and 'body_html'
- Handles JSON parsing and error handling
analyze_email_topics()
- Performs LDA topic modeling on email texts
- Input: List of email texts, number of topics
- Output: Dict with topics, dominant topics, document-topic distribution
- Includes preprocessing: tokenization, stemming, stopword removal
preprocess_text()
- Text cleaning and normalization
- Removes stopwords, stems words
- Filters tokens by length
summarize_topics()
- Formats raw LDA output into readable format
- Extracts keywords and weights per topic
class EmailAIAnalysis(models.Model):
# Relations
email = OneToOneField(Email)
# AI Generation Fields
generated_subject = CharField(max_length=200)
generated_body_html = TextField()
generation_prompt = TextField()
generation_model = CharField(default='gpt-3.5-turbo')
# Topic Analysis Fields
topics_json = JSONField()
dominant_topics = JSONField()
topic_analysis_count = IntegerField()
# Metadata
created_at = DateTimeField(auto_now_add=True)
updated_at = DateTimeField(auto_now=True)generate_email_with_ai(request, campaign_id)
- POST endpoint:
/api/campaigns/{campaign_id}/generate-email/ - Validates campaign ownership
- Calls
generate_email_content() - Optionally saves to EmailAIAnalysis record
analyze_campaign_topics(request, campaign_id)
- POST endpoint:
/api/campaigns/{campaign_id}/analyze-topics/ - Collects all email bodies from campaign
- Calls
analyze_email_topics() - Stores results in EmailAIAnalysis record
template.html
- Added "✨ AI Generate" button in email editor
- Modal dialog for generation parameters
- JavaScript handler to call API and update Quill editor
email_analysis.html
- New dedicated page for topic analysis
- Campaign selector dropdown
- Interactive slider for number of topics
- Real-time chart rendering with Chart.js
- Results display: topic cards, email-topic table, distribution chart
Solution:
# Test if key is set
python -c "import os; print(os.getenv('OPENAI_API_KEY'))"
# Set the key
# Windows:
set OPENAI_API_KEY=sk-your-key-here
# macOS/Linux:
export OPENAI_API_KEY=sk-your-key-here
# Restart your development server or Django appSymptoms: "rate_limit_exceeded" error after multiple generations
Solutions:
- Wait 60 seconds before generating again
- Upgrade your OpenAI plan: https://platform.openai.com/account/billing/overview
- Use a lower temperature value (reduces creativity, uses fewer tokens)
Causes:
- Large number of emails (20+)
- Long email content
- High number of topics (10)
Solutions:
- Reduce number of topics (try 3-5 instead of 10)
- Analyze subset of emails
- Use shorter campaign with fewer emails
- Run analysis during off-peak hours
Causes:
- All emails are empty/have no body
- Campaign has no emails
Solutions:
- Create/edit emails with actual content
- Use HTML editor to add body text
- Ensure emails are saved before analysis
Causes:
- API returned malformed HTML
- JSON parsing issue
Solutions:
- Click "✨ AI Generate" again with same parameters
- Manually edit the generated content
- Report to OpenAI if consistently broken
Solution:
# Reinstall dependencies
pip install -r requirements.txt
# Or specific packages
pip install nltk gensim pandas openaiCauses:
- Campaign has no emails
- All emails are empty
Solutions:
- Add emails to campaign with content
- Save emails before running analysis
- Check that body_html field is populated
- Time: 5-15 seconds per email
- Cost: ~$0.002-0.01 per email
- Rate Limit: ~3-4 per minute (OpenAI free tier)
- Cache: Store frequently used generations in EmailAIAnalysis
- Time: 30-60 seconds for 10 emails, 2-5 minutes for 50+ emails
- Memory: ~100MB for 100 emails
- Scalability: Works best with 5-50 emails per analysis
- Parallelization: Not parallelized (single-threaded)
- Batch Analysis: Analyze full campaigns at once instead of incremental
- Cache Results: Results are saved to DB and can be reused
- Lazy Loading: Only generate when user clicks button
- Async Processing: Consider Celery for long-running analyses (future enhancement)
Potential improvements for future versions:
- Async Processing: Use Celery to handle long-running topic analysis
- Email Scoring: Rate generated emails for quality/engagement
- A/B Testing: Generate multiple versions and compare
- Custom Tone Templates: Save user-defined tone preferences
- Advanced Topic Visualization: 3D topic space, interactive exploration
- Multi-language Support: Generate and analyze emails in other languages
- Fine-tuned Models: Train on user's historical email data for better results
- Cost Tracking: Monitor API spending and costs per campaign
- OpenAI Documentation: https://platform.openai.com/docs
- Gensim Documentation: https://radimrehurek.com/gensim/
- NLTK Documentation: https://www.nltk.org/
- Django REST Framework: https://www.django-rest-framework.org/
- ✅ AI email generation with GPT-3.5-turbo
- ✅ Topic modeling with LDA algorithm
- ✅ Database model for storing AI results
- ✅ Web interface for both features
- ✅ RESTful API endpoints
- ✅ Comprehensive error handling
- ✅ Email analysis dashboard
Last Updated: November 2025 Author: Alex Project Team Status: Production Ready