The Challenge
Multi-channel marketplace selling sounds like more revenue, but it multiplies operational complexity. Amazon sends emails to one mailbox, eBay notifications arrive at another, and Walmart order alerts land somewhere else entirely. The same dispute might arrive in two different inboxes — and if you accidentally handle it twice, Amazon penalizes you.
The client was running 6 marketplace channels and receiving 500+ emails per week, varying from routine order confirmations to time-sensitive A-to-Z claims that could result in account suspension if not addressed within 72 hours. Finding the critical emails in the noise was exhausting and error-prone.
The real risk: A missed A-to-Z claim or eBay defect notice doesn't just cost one sale. It can trigger a performance review that suspends the entire seller account — wiping out thousands of dollars in monthly revenue while the appeal process runs for weeks.
Marketplace Channels
Each channel has its own email sender patterns, notification formats, and urgency rules. We mapped all of them before writing a single line of code:
Processing Pipeline
Every incoming email passes through a multi-stage pipeline before a human ever sees it:
Ingest
IMAP fetch from all mailboxes
Dedup
from+subject+date hash
Marketing Filter
SQL + PHP rules
Gemini AI
Classify + extract
Dashboard
Per-channel KPIs
Stage 1: Deduplication
Many marketplace emails arrive in multiple mailboxes simultaneously. An Amazon A-to-Z claim might land in the primary inbox and in a forwarded copy. The deduplication stage creates a hash of the sender address, subject line, and date header. Any email matching an existing hash is discarded before it ever reaches the AI classification step.
$hash = md5(
strtolower(trim($email['from'])) . '|' .
strtolower(trim($email['subject'])) . '|' .
date('Y-m-d', strtotime($email['date']))
);
// Check for duplicate before processing
$exists = $db->query(
"SELECT 1 FROM email_ingest WHERE dedup_hash = ?",
[$hash]
)->fetch();
if ($exists) { continue; } // skip duplicate
Stage 2: Marketing Filter (Two Layers)
A huge percentage of marketplace emails are promotional — deal notifications, policy updates, feature announcements. Processing these through Gemini would waste API calls and add noise to the dashboard. We filter them out at two levels:
- SQL-level filter: Known promotional sender addresses and subject line patterns (e.g., "% off", "New feature", "Weekly digest") are excluded in the ingestion query itself — no PHP processing needed.
- PHP-level filter: Emails that pass the SQL filter get their HTML stripped and checked for the pattern of 3+ prices in
$XX.XXformat within 500 characters — a reliable signal of a product promotion grid.
Stage 3: Gemini 2.0 Flash Classification
Non-marketing emails are sent to Gemini 2.0 Flash with a structured prompt. The AI returns a JSON object containing:
"category": "A-to-Z Claim",
"urgency": "critical", // critical|high|medium|low
"action_needed": "File response within 72 hours",
"marketplace": "amazon",
"order_id": "114-5832941-2938475",
"asin": "B0891ZNBZ9",
"summary": "Buyer claims item not received...",
"suggested_response": "Thank you for contacting us..."
}
Urgency Classification
The four urgency levels map to specific business actions:
Dashboard
The dashboard surfaces per-marketplace KPIs at the top — open disputes, unread criticals, response rate — with the urgent items listed below in account-specific tabs. Each marketplace has its own logo, badge color, and left-border color for instant visual identification.
eBay was split into two separate tabs: the selling account (aztekcwholesale) and the buying account, since the urgency profiles are completely different — a selling defect is critical, a buying issue is usually just a tracking question.
AI Chat Advisor
The dashboard includes an embedded Gemini chat interface pre-loaded with context: the 20 most recent issues across all channels, current KPI stats, and the client's account history. From the chat, the user can ask questions like "what A-to-Z claims need responses today?" or "draft an appeal for order 114-5832941." The AI has full context to give actionable, specific answers rather than generic advice.
Email Body Cleanup
Marketplace emails are often brutally complex HTML — nested tables, inline styles, tracking pixels. Sending raw HTML to the AI or displaying it in the dashboard would be both expensive and unreadable. The cleanup pipeline strips all HTML and CSS using a DOMParser-based approach, with a regex fallback for malformed markup:
$body = preg_replace('#<style[^>]*>.*?</style>#si', '', $body);
// Step 2: Strip <script> blocks
$body = preg_replace('#<script[^>]*>.*?</script>#si', '', $body);
// Step 3: Convert block tags to newlines
$body = preg_replace('#<(br|p|div|tr|li)[^>]*>#i', "\n", $body);
// Step 4: Strip remaining tags, decode entities
$body = html_entity_decode(strip_tags($body));
// Step 5: Collapse whitespace
$body = preg_replace('/\s{3,}/', "\n\n", trim($body));
// Result: clean, readable text for AI + dashboard display
Results
| Metric | Before | After |
|---|---|---|
| Email sorting time/week | 15+ hours | 0 hours |
| Critical issue detection | Manual | Automatic (within minutes) |
| Missed A-to-Z claims | Occasional | Zero |
| Cross-mailbox duplicates | Handled twice | Deduplicated |
| Marketing email noise | ~40% of inbox | Filtered at SQL level |
| Channels consolidated | 6 separate inboxes | 1 unified dashboard |
Tech Stack
Gemini 2.0 Flash API • PHP 8.1 • MySQL 8.0 • IMAP • JavaScript (vanilla) • Custom CSS dashboard • MD5 deduplication hashing