βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β 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/danieladdisonorg/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.