Email Processing

Last updated: April 11, 2026

Email Processing Pipeline

The aiemail package handles the ingestion, parsing, and routing of inbound emails, acting as the bridge between legacy communication channels and the AI assistant.

IMAP Polling

The system operates a background loop that periodically connects to configured mailboxes (e.g., support@, alerts@) using IMAP.

Parsing and Normalization

Emails are complex structures. The processing pipeline standardizes them for AI consumption:

  1. Header Extraction: Captures From, To, Subject, and Date.
  2. Multipart Handling: Extracts the text/plain body if available; otherwise, it strips HTML from the text/html part to provide a clean text representation.
  3. Sanitization: Removes large base64 encoded images or attachments that would overwhelm the LLM's context window.

Routing Logic

1. NOC Alerts

If the From address matches a known monitoring system (e.g., sysafe-monitor@...), the email is intercepted. The subject and parsed body are passed directly to assistant.RecordNOCAlert for debouncing and batch analysis by the noc_intelligence system.

2. Support Tickets

If the email is a customer inquiry, it is packaged into a prompt and sent to a specialized SupportAgent persona. The agent generates a draft reply, which is either automatically sent or queued for human review depending on the configuration.

3. Summarization

The aiemailsummary package runs periodically to read all unread emails in a specified folder, generating a condensed bullet-point summary of inbox activity for the user's daily briefing.

Component Diagram: Email Ingestion

graph TD
    IMAP[(IMAP Mailbox)] --> Poller[aiemail.go Poller]
    Poller --> Parse[Parse Headers & Body]
    
    Parse --> Route{Check Sender/To}
    
    Route -- Monitoring Tool --> NOC[assistant.RecordNOCAlert]
    Route -- Customer --> Supp[SupportAgent Reply]
    Route -- End of Day --> Sum[aiemailsummary.go]

Guidance for AI Agents

  • Email Format: Emails parsed by this system are plain text. When drafting replies, do not use Markdown tables, bolding, or code blocks, as the recipient's email client may not render them correctly.
  • Tone: Support emails must strictly adhere to the business persona guidelines (e.g., professional, empathetic).

Cross-References