Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ON EMAIL

Monitors an email address and triggers a script when new emails arrive.

Syntax

' Basic - trigger on any email
ON EMAIL "address@example.com"

' With FROM filter - only from specific sender
ON EMAIL "address@example.com" FROM "sender@example.com"

' With SUBJECT filter - only matching subject
ON EMAIL "address@example.com" SUBJECT "pattern"

Description

The ON EMAIL keyword registers an email monitor that triggers a script whenever new emails arrive at the specified address. This is part of the event-driven programming model, similar to SET SCHEDULE and ON (for database triggers).

When an email arrives, the system creates an entry in email_received_events which can be processed by your script.

Parameters

ParameterTypeRequiredDescription
addressStringYesEmail address to monitor
FROMStringNoFilter to only trigger for emails from this sender
SUBJECTStringNoFilter to only trigger for emails matching this subject pattern

Examples

Basic Email Monitoring

' Monitor support inbox for any incoming email
ON EMAIL "support@company.com"
    email = GET LAST "email_received_events"
    TALK "New support request from " + email.from_address
    TALK "Subject: " + email.subject
END ON

Filter by Sender

' Only trigger for emails from a specific supplier
ON EMAIL "orders@company.com" FROM "supplier@vendor.com"
    email = GET LAST "email_received_events"
    
    ' Process supplier invoice
    IF email.has_attachments THEN
        attachments = email.attachments
        FOR EACH att IN attachments
            IF att.mime_type = "application/pdf" THEN
                COPY att.filename TO "account://finance@company.com/Invoices/"
            END IF
        NEXT
    END IF
END ON

Filter by Subject

' Only trigger for urgent alerts
ON EMAIL "alerts@company.com" SUBJECT "URGENT"
    email = GET LAST "email_received_events"
    
    ' Send SMS notification to on-call team
    SMS "+1-555-0100", "URGENT ALERT: " + email.subject
    
    ' Create task for follow-up
    CREATE TASK "Handle urgent alert", email.subject, "high"
END ON

Process Attachments

ON EMAIL "invoices@company.com"
    email = GET LAST "email_received_events"
    
    IF email.has_attachments THEN
        FOR EACH attachment IN email.attachments
            ' Save to drive
            filename = attachment.filename
            COPY attachment TO "account://user@gmail.com/Invoices/" + filename
            
            ' Extract data if PDF
            IF attachment.mime_type = "application/pdf" THEN
                data = SAVE FROM UNSTRUCTURED attachment, "invoices"
                TALK "Processed invoice: " + data.invoice_number
            END IF
        NEXT
    END IF
END ON

Auto-Reply System

ON EMAIL "info@company.com"
    email = GET LAST "email_received_events"
    
    ' Check if it's a common question
    response = LLM "Classify this email and suggest a response: " + email.subject
    
    IF response.category = "pricing" THEN
        SEND MAIL email.from_address, "RE: " + email.subject, "
            Thank you for your inquiry about pricing.
            Please visit our pricing page: https://company.com/pricing
            
            Best regards,
            Company Team
        "
    ELSE IF response.category = "support" THEN
        ' Create support ticket
        CREATE TASK "Support: " + email.subject, email.from_address, "normal"
        
        SEND MAIL email.from_address, "RE: " + email.subject, "
            Your support request has been received.
            Ticket ID: " + task.id + "
            We'll respond within 24 hours.
        "
    END IF
END ON

Multi-Account Monitoring

' Monitor multiple email addresses
ON EMAIL "sales@company.com"
    email = GET LAST "email_received_events"
    SET BOT MEMORY "lead_source", "sales_inbox"
    ' Process sales inquiry
END ON

ON EMAIL "support@company.com"
    email = GET LAST "email_received_events"
    SET BOT MEMORY "lead_source", "support_inbox"
    ' Process support request
END ON

ON EMAIL "billing@company.com" FROM "stripe.com"
    email = GET LAST "email_received_events"
    ' Process Stripe payment notifications
END ON

Email Event Properties

When an email is received, the event object contains:

PropertyTypeDescription
idUUIDUnique event identifier
monitor_idUUIDID of the monitor that triggered
message_uidIntegerEmail server message UID
message_idStringEmail Message-ID header
from_addressStringSender email address
to_addressesArrayList of recipient addresses
subjectStringEmail subject line
has_attachmentsBooleanWhether email has attachments
attachmentsArrayList of attachment objects

Attachment Properties

PropertyTypeDescription
filenameStringOriginal filename
mime_typeStringMIME type (e.g., “application/pdf”)
sizeIntegerSize in bytes

Database Tables

email_monitors

Stores the monitor configuration:

ColumnTypeDescription
idUUIDMonitor ID
bot_idUUIDBot that owns this monitor
email_addressVARCHARAddress being monitored
script_pathVARCHARScript to execute
is_activeBOOLEANWhether monitor is active
filter_fromVARCHAROptional FROM filter
filter_subjectVARCHAROptional SUBJECT filter
last_uidBIGINTLast processed message UID

email_received_events

Logs received emails:

ColumnTypeDescription
idUUIDEvent ID
monitor_idUUIDMonitor that triggered
message_uidBIGINTEmail server UID
from_addressVARCHARSender address
subjectVARCHAREmail subject
has_attachmentsBOOLEANHas attachments flag
processedBOOLEANWhether event was processed

Best Practices

  1. Use filters when possible - Reduce unnecessary script executions
  2. Handle errors gracefully - Emails may have unexpected formats
  3. Process attachments safely - Validate MIME types before processing
  4. Avoid infinite loops - Don’t send emails that trigger your own monitors
  5. Log important actions - Track what was processed for debugging

Comparison with Other Event Keywords

KeywordTrigger SourceUse Case
ON EMAILIncoming emailsProcess messages, auto-reply
ON CHANGEFile system changesSync files, process uploads
ON INSERTDatabase insertsReact to new data
SET SCHEDULETime-basedPeriodic tasks
WEBHOOKHTTP requestsExternal integrations

TriggerKind: EmailReceived = 5