┌─────────────────────────────────────────────────────────────────────────────────────┐
│ EMAIL MARKETING AI AGENT WORKFLOW │
└─────────────────────────────────────────────────────────────────────────────────────┘
┌──────────────────┐ ┌──────────────────┐ ┌──────────────────┐
│ Data Sources │ │ AI Processing │ │ Output Systems │
└──────────────────┘ └──────────────────┘ └──────────────────┘
┌─────────────────────────────────────────────────────────────────────────────────────┐
│ STEP 1: DATA COLLECTION │
└─────────────────────────────────────────────────────────────────────────────────────┘
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ Zoho CRM │ ──────▶│ get_emails │────────▶│ zoho_emails │
│ (Existing │ │ .py │ │ .csv │
│ Customers) │ │ │ │ │
└─────────────┘ └─────────────┘ └─────────────┘
│
┌─────────────┐ │ ┌─────────────┐
│ prospects │ │ │ Google │
│ .csv │────────────────┼───────────────▶│ Sheets │
│ (New Leads) │ │ │ (Email DB) │
└─────────────┘ │ └─────────────┘
▼
┌─────────────┐
│ Domain │
│ Comparison │
│ Analysis │
└─────────────┘
┌─────────────────────────────────────────────────────────────────────────────────────┐
│ STEP 2: AI EMAIL GENERATION │
└─────────────────────────────────────────────────────────────────────────────────────┘
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ Domain │ YES │ Personalized│ │ OpenAI │
│ Match? │────────▶│ Email │───────▶│ GPT-4 │
│ │ │ Generation │ │ │
└─────────────┘ └─────────────┘ └─────────────┘
│ │
│ NO │
▼ ▼
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ Generic │ │ Email │ │ Generated │
│ Email │────────▶│ Templates │───────▶│ Email with │
│ Generation │ │ │ │ Tracking │
└─────────────┘ └─────────────┘ └─────────────┘
┌─────────────────────────────────────────────────────────────────────────────────────┐
│ STEP 3: EMAIL SENDING & TRACKING │
└─────────────────────────────────────────────────────────────────────────────────────┘
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ Google │ │ Google Apps │ │ Email │
│ Sheets │────────▶│ Script │───────▶│ Recipients │
│ (Email DB) │ │ (Sender) │ │ │
└─────────────┘ └─────────────┘ └─────────────┘
│ │
│ │
▼ ▼
┌─────────────┐ ┌─────────────┐
│ Tracking │◀───────│ Email │
│ Pixel │ │ Opens │
│ (Web App) │ │ │
└─────────────┘ └─────────────┘
│
▼
┌─────────────┐
│ Update Open │
│ Count in │
│ Sheets │
└─────────────┘
┌─────────────────────────────────────────────────────────────────────────────────────┐
│ STEP 4: ENGAGEMENT ANALYSIS & FOLLOW-UP │
└─────────────────────────────────────────────────────────────────────────────────────┘
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ Email Opens │ │ Opens │ YES │ AI Call │
│ Monitoring │───────▶│ > 5? │────────▶│ Script │
│ │ │ │ │ Generation │
└─────────────┘ └─────────────┘ └─────────────┘
│ │
│ NO │
▼ ▼
┌─────────────┐ ┌─────────────┐
│ Continue │ │ Upload to │
│ Monitoring │ │ Call Script │
│ │ │ Sheet │
└─────────────┘ └─────────────┘
│
▼
┌─────────────┐
│ Upload to │
│ Zoho CRM │
│ (Lead Data) │
└─────────────┘
┌─────────────────────────────────────────────────────────────────────────────────────┐
│ SYSTEM COMPONENTS │
└─────────────────────────────────────────────────────────────────────────────────────┘
┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ Python │ │ Google Apps │ │ Google │ │ OpenAI │ │ Zoho CRM │
│ Scripts │ │ Script │ │ Sheets API │ │ GPT-4 API │ │ API │
│ │ │ │ │ │ │ │ │ │
│• get_emails │ │• Email │ │• Data │ │• Email │ │• Customer │
│• clean_and_ │ │ Sending │ │ Storage │ │ Generation │ │ Data │
│ gen_call_ │ │• Tracking │ │• Open │ │• Call │ │• Lead │
│ script │ │ Pixel │ │ Logging │ │ Scripts │ │ Management │
└─────────────┘ └─────────────┘ └─────────────┘ └─────────────┘ └─────────────┘
┌─────────────────────────────────────────────────────────────────────────────────────┐
│ KEY FEATURES │
└─────────────────────────────────────────────────────────────────────────────────────┘
✅ Multi-alias email sending
✅ Real-time open tracking with hidden pixels
✅ AI-powered personalized email generation
✅ Automatic call script creation for high-engagement leads
✅ Integration with Zoho CRM and Google Workspace
✅ Domain-based customer relationship detection
✅ Automated lead scoring and follow-up prioritization
This diagram shows the complete workflow of your email marketing AI agent, from data collection through AI-powered email generation, tracking, and automated follow-up. The system intelligently personalizes emails based on existing customer relationships and automatically generates call scripts for highly engaged prospects.
demo.mp4
app_script_email_automation/
├── README.md
├── requirements.txt
└── src/
├── app_script.js (Main Apps Script code)
├── credentials.json (Credentials for external services)
├── get_emails.py (Fetches and uploads email data to Sheets)
├── clean_and_gen_call_script.py (Cleans data & triggers call script creation)
└── functions/
├── agent.py (AI-related logic)
├── config.py (Configuration & constants)
├── create_call_script.py (Generates call scripts)
├── create_email.py (Builds email content)
└── utlis.py (Utility functions)
- Send emails from multiple aliases.
- Track opens with a web app and hidden pixel.
- Log open counts and timestamps in Google Sheets.
- Dynamically generate call scripts for emails exceeding five opens.
- Append generated scripts to a separate Sheet for follow-up actions.
- Set Up Google Sheets: Create or update source/destination Sheets for email list and lead data.
- Deploy Web App: Host the code in Google Apps Script and enable the web endpoint for open tracking.
- Configure Credentials: Store any required credentials for third-party services (e.g., OpenAI API).
- Run & Track: Send emails, check open stats, and watch new call scripts appear in the leads Sheet.
- Clone the Repository
git clone https://github.com/bigdata5911/email-marketing-ai-agent.git
cd email-marketing-ai-agent
-
Create Environment File (.env)
- Add your OpenAI API key and other credentials.
- Copy and paste the following into your
.env
file, replacing the placeholder values with your actual keys:
OPENAI_API_KEY=your_key_here SHEET_API_KEY=your_sheet_key_here
-
Install Dependencies
- In local Python projects, run:
pip install -r requirements.txt
- In local Python projects, run:
-
Create or Update config.py
Place this file undersrc/helpers/
and store your credentials, Sheet IDs, and any other constants.
For example:email_google_sheet = "YOUR_EMAIL_SHEET_ID" call_script_google_sheet = "YOUR_CALL_SCRIPT_SHEET_ID" CLIENT_ID = "YOUR_ZOHO_CLIENT_ID" CLIENT_SECRET = "YOUR_ZOHO_CLIENT_SECRET" REFRESH_TOKEN = "YOUR_ZOHO_REFRESH_TOKEN" ZOHO_API_BASE_URL = "https://www.zohoapis.com/crm/v2" TOKEN_URL = "https://accounts.zoho.com/oauth/v2/token"
-
Configure Google Apps Script
- Paste the
.gs
/app_script.js
code into your Google Apps Script Editor. - Set up triggers or web app deployments as required.
- Paste the
-
Deploy Production
- Push your local changes to GitHub or your chosen repo host.
- In Google Apps Script, deploy with appropriate permissions for the tracking pixel.
- Configure your Google Apps Script project with the provided code.
- Enable the web endpoint for handling the tracking pixel.
- Create or update your Google Sheets for storing email data, tracking opens, and logging call scripts.
- Set necessary environment variables (e.g., API keys) in your script or use a .env file locally.
- Deploy as a web app; then run the main script to send emails and track opens.
- Activate your virtual environment (.venv) if needed, then run
get_emails.py
to gather and upload emails to a Google Sheet. - Open the Google Apps Script editor and run
app_script.js
via the web interface to handle sending and tracking.
-
Data Import and Comparison
- Import existing customers from Zoho CRM.
- Import new customer data from a CSV file.
- Compare the domains of existing and new customers.
-
AI-Powered Email Generation
- If a domain match is found, generate a personalized email using AI.
- Otherwise, generate a generic marketing email.
-
Email Upload and Sending
- Upload generated emails to a Google Sheet.
- Send all emails from the Sheet using Google Apps Script.
-
Email Tracking
- Track email activity via Google Apps Script.
- Update the Sheet with open status whenever an email is opened.
-
Engagement Analysis and Call Script Generation
- If an email’s open rate exceeds the threshold:
- Generate a personalized call script via AI.
- Save this call script to another Google Sheet for follow-up.
- If an email’s open rate exceeds the threshold:
This project is licensed under the MIT License.