Email Orchestration Workflow (EML)¶
Module Purpose: The central nervous system of the platform. It continuously connects to mailboxes (Outlook/Gmail/IMAP), extracts intent from unstructured emails using AI, and routes them to the correct workflow (Sales, ERP, or Support).
[!NOTE] Philosophy: "No Email Left Behind." Every incoming email must either be converted to a Lead, linked to an Order, or explicitly archived. Zero inbox is the goal.
Use Case Quick Reference¶
| ID | Title | Priority |
|---|---|---|
| EML-001 | Monitor Email Mailboxes | P1 |
| EML-002 | Classify Email to CRM Stream | P1 |
| EML-003 | Classify Email to ERP Stream | P1 |
| EML-004 | Create Lead from Email | P1 |
| EML-005 | Link Email to Existing Lead | P1 |
| EML-006 | Track Quote Milestone | P1 |
| EML-007 | Lookup Order in ERP | P1 |
| EML-008 | Set Dispatch Priority Flag | P1 |
| EML-009 | Send Dispatch Confirmation | P1 |
| EML-010 | Assign Task ID to Email | P1 |
| EML-011 | Update Task Status | P1 |
| EML-012 | Send Escalation Alert | P2 |
| EML-013 | Multi-Entity Validation | P1 |
| EML-014 | Pincode Route Planning | P2 |
| EML-015 | Campaign Result Tracking | P2 |
| EML-016 | Outlook-to-Salesforce Push | P2 |
UC-EML-001: Monitor Email Mailboxes¶
What It Does¶
Continuously polls connected mailboxes (Outlook 365, Gmail, IMAP), fetches new emails, downloads attachments, and pushes them into the central processing queue. Example: fetching sales@pebble.com every 30 seconds.
Who: System Background Worker
When: Continuous (24/7)
Why: To ensure real-time responsiveness without humans manually checking inboxes.
How It Works¶
- Poll: Worker wakes up every 30s.
- Fetch: Connects via OAuth2/IMAP. Queries
UNSEENmessages. - Download:
- Fetches Body (HTML/Text).
- Fetches Attachments (PDF, Excel, Images).
- Generates unique ID
EML-202X-XXXX. - Mark: Sets email value as
READon the server (if configured) or just tracks ID to avoid re-fetch. - Enqueue: Pushes to
processing_queuefor EML-002.
Edge Cases¶
Token Expiry: Automatically refreshes OAuth tokens. If refresh fails, alerts Admin.
Giant Attachment: If > 25MB, skips download, leaves link to original email, flags warning.
UC-EML-002: Classify Email to CRM Stream¶
What It Does¶
The "Traffic Cop". Analyzes incoming email content to decide if it belongs to the Sales/CRM pipeline (Enquiry, Complaint) or elsewhere.
Who: System AI Engine
When: Immediately after Ingestion
How It Works¶
- Input: Subject + Body Text.
- Analysis: AI Keyword Scanner matches against CRM definitions:
- "Quote", "Price", "Availability" -> Enquiry
- "Complaint", "Broken", "Late" -> Complaint
- "Demo", "Visit" -> Meeting Request
- Action:
- If Match: Tag as
Stream: CRM. - Forward to EML-004.
[!TIP] Implementation Reference: Detailed User Stories and Acceptance Criteria for this classification logic are defined in prd-ai-email-classification.md.
Success Signals¶
✅ Correctly segregates Sales emails from Spam/Ops emails.
UC-EML-003: Classify Email to ERP Stream¶
What It Does¶
Identifies operational emails related to existing orders, logistics, or procurement, which belong in the ERP workflow rather than Sales.
Who: System AI Engine
When: Immediately after Ingestion
How It Works¶
- Pattern Match: Looks for Order Patterns (e.g.,
PO-1234,INV-987) or Logistics keywords (Dispatch,Tracking,Invoice). - Action:
- If Match: Tag as
Stream: ERP. - Extract
Order IDif present. - Forward to EML-007.
[!TIP] Implementation Reference: Detailed extraction rules and multi-intent handling for ERP streams are in prd-ai-email-classification.md.
Edge Cases¶
Ambiguous Email: "Where is my order?" (Could be Complaint or Status Check). Logic: Default to CRM Complaint for human review if sentiment is negative.
UC-EML-004: Create Lead from Email¶
What It Does¶
Auto-generates a "Prelead" record from a raw email. It scrapes the sender's info, company signature, and product interest to pre-fill the form, saving the rep 5 minutes of data entry.
Who: System
When: Classification = Enquiry
How It Works¶
- Scraping:
- Sender: Use
Fromheader. - Company: Extract from
@domain.comor Email Signature text. - Phone: Regex scan signature for
+91... - Product: NLP keyword extraction from body ("Zinc Oxide", "Titanium").
- Creation:
- Inser into
preleadstable. - Status:
New. - Source:
Email Inbound. - Snapshot: Saves HTML rendering of email as a static image/file attached to the lead for audit.
Success Signals¶
✅ Pre-fills 80% of fields accurately.
✅ Captures Email Signature details (Phone/Designation).
UC-EML-005: Link Email to Existing Lead¶
What It Does¶
Prevents duplicates. Checks if the sender john@apple.com already exists in the CRM. If yes, it logs this email as an "Activity" on John's timeline instead of creating a new Lead.
Who: System Dedupe Engine
When: Before creating new lead
How It Works¶
- Lookup: Query
contactstable by Email Address. - Match Found:
- Add Email to
activitiestable linked to Contact ID. - Bump "Last Interaction" timestamp.
- Notify Account Manager: "New email from John".
- Thread Check: If subject matches previous thread, attach to existing Ticket/Opp.
- No Match: Proceed to EML-004.
UC-EML-006: Track Quote Milestone¶
What It Does¶
Monitors the "Quote Sent" SLA. If a lead requested a quote but none was sent within 24 hours, it flags the manager.
Who: System Watchdog
When: Periodic Check
How It Works¶
- Trigger: Card moved to "Quote Requested" column.
- Timer: Starts 24h countdown.
- Check: Has a file been uploaded to "Quotations" tab?
- Breach: If Timer > 24h and No Quote:
- Turn Card Red.
- Send Email to Team Lead.
UC-EML-007: Lookup Order in ERP¶
What It Does¶
For ERP-stream emails, it acts as a bridge. It takes the specific Order ID mentioned in the email and calls the ERP API to fetch live status (e.g., "Packed", "Shipped").
Who: System Connector
When: Stream = ERP
How It Works¶
- Extraction: Identify
ORD-2023-001. - API Call: GET
/erp/orders/ORD-2023-001. - Enrichment:
- Add "Order Status: Shipped" tag to the email card.
- Display "Expected Delivery: 12th Jan" on the card face.
UC-EML-008: Set Dispatch Priority Flag¶
What It Does¶
Allows a Sales Rep to mark an order as "Urgent" directly from the email interface. This pushes a priority flag to the Warehouse ERP view.
Who: Sales Rep
When: Manual Action (Card Button)
How It Works¶
- User Action: Clicks "Mark Urgent Dispatch" on email card.
- System:
- Updates CRM Task Priority = High.
- API Push: Updates ERP Order
priorityfield. - Notify: Slack message to Warehouse Manager.
UC-EML-009: Send Dispatch Confirmation¶
What It Does¶
Closes the loop. When ERP marks order as "Shipped", system auto-sends the tracking details to the customer, replying to their original status enquiry thread.
Who: System Automation
When: ERP Status Change event
How It Works¶
- Trigger: ERP Webhook
order.shipped. - Compose:
- Template: "Your order {ID} has shipped via {Carrier}."
- Tracking URL: Generated dynamically.
- Send: Reply to last customer email thread.
- Close: Archive the enquiry card.
UC-EML-010: Assign Task ID to Email¶
What It Does¶
Ensures every actionable email has a unique internal ID (e.g., TSK-9988) separate from the email ID, used for SLA tracking and ticketing.
Who: System
When: On Ingestion
How It Works¶
- Generate:
TSK-YYYY-{Sequence}. - Stamp: Save in database.
- Reference: Add to Email Subject in replies:
[Ref: TSK-9988]. This ensures future replies thread correctly.
UC-EML-011: Update Task Status¶
What It Does¶
Syncs the visual Kanban status (e.g., "In Progress") with the underlying database record, ensuring reporting dashboards are accurate.
Who: System / User
When: Drag-and-drop event
How It Works¶
- Event: User drags card to "Done".
- Update: Set
status = Closed,completed_at = Now(). - Log: Record "User X closed task TSK-9988" in audit log.
UC-EML-012: Send Escalation Alert¶
What It Does¶
The "Siren". If High Priority items sit untouched for > 4 hours, it notifies senior management.
Who: System Watchdog
When: Hourly Cron
How It Works¶
- Scan: Find cards where
Priority = HighANDLast_Activity > 4 hours. - Alert:
- Level 1 (4h): Email User.
- Level 2 (8h): Email Manager.
- Level 3 (24h): Email VP Sales.
UC-EML-013: Multi-Entity Validation¶
What It Does¶
Checks if the customer exists in any of the group companies. Prevents creating "Acme Corp" in Entity A if it already exists in Entity B (unless intentional), or helps link them as Group Companies.
Who: System Validator
When: Lead Creation
How It Works¶
- Input: CIN / GST / PAN / Company Name.
- Global Search: Query Master DB across all Tenancy scopes.
- Result:
- Match Found: "Company exists in 'Alpha Pebble Singapore'. Linking as Group Account."
- No Match: "Clean to create."
UC-EML-014: Pincode-based Route Planning¶
What It Does¶
Helps field sales planning. When a rep visits a customer, system suggests "Nearby Leads" based on pincode proximity to maximize trip value.
Who: Sales Rep (Mobile App)
When: Planning visits
How It Works¶
- Input: Target Customer Pincode (e.g., 400001).
- Search: Find all Open Enquiries with Pincode within 5km radius.
- Display: List/Map view. "3 other leads near here."
UC-EML-015: Campaign Result Tracking¶
What It Does¶
Closes the ROAS loop. Tracks how many inbound enquiries resulted from specific email/marketing campaigns.
Who: System Analytics
When: Enquiry Ingestion
How It Works¶
- Match: Check if sender email target was in a recent Outbound Campaign list.
- Tag: If yes, tag Lead Source = "Campaign: Q1 Promo".
- Attribute: If this lead converts to Order, attribute revenue to that Campaign ID.
UC-EML-016: Outlook-to-Salesforce Push¶
What It Does¶
A manual plugin for Outlook. Allows users to "Right Click -> Push to Pebble" for emails that bypassed the auto-ingestion (e.g., personal inbox).
Who: User (Outlook Plugin)
When: Manual
How It Works¶
- Select: User highlights email in Outlook.
- Click: "Push to Pebble" button.
- Action: Plugin sends JSON payload (Body, Attachments) to Pebble Ingestion API
v1/ingest/email. - Feedback: Show "Success! Ticket #123 created."
See Also¶
- PRD AI Email Classification:
tasks/prd-ai-email-classification.md - Entity Triage PRD:
tasks/prd-entity-triage.md - PRD Template:
.agent/workflows/prd.md
Traceability Map¶
- EML-001 Monitor Email Mailboxes -> Aligns with PRD: Email Orchestration UC-EML-001 and PRD US-001 (CRM/ERP routing) in
tasks/prd-ai-email-classification.md. - EML-002 Classify Email to CRM Stream -> See PRD: AI Email Classification US-002;
tasks/prd-ai-email-classification.md. - EML-003 Classify Email to ERP Stream -> See PRD: AI Email Classification US-003;
tasks/prd-ai-email-classification.md. - EML-004 Create Lead from Email -> See PRD: AI Email Classification US-004;
tasks/prd-ai-email-classification.md. - EML-013 Multi-Entity Validation -> See PRD: Entity Triage US-002/US-004;
tasks/prd-entity-triage.md.