MailBridge

object MailBridge : CodBi

MailBridge — AI-initiated email sending with rate limiting

Allows the AI model to send emails via CALL:mail(to='...', subject='...', body='...') markers. Uses FORMCYCLE's mail API at runtime: obtains the system mail context via MailContextProvider.getSystemContext(), then sends through SimpleTextMail.

Security & Rate Limiting

  • Global cap: max GLOBAL_MAX_MAILS_PER_HOUR mails per hour across all sessions

  • Per-session cap: max SESSION_MAX_MAILS mails per streaming session

  • Recipient whitelist: only addresses matching allowedRecipientPattern are permitted

  • Subject and body are sanitized (no header injection)

Configuration

PropertyFormatExample
AI_Mail_Enabledtrue/falsetrue
AI_Mail_AllowedRecipientsRegex pattern.*@mycompany\.com
AI_Mail_MaxPerHourInteger10
AI_Mail_MaxPerSessionInteger3
AI_Mail_DisclaimerFree textAI-Generated

Types

Link copied to clipboard
data class MailResult(val success: Boolean, val recipient: String? = null, val subject: String? = null, val error: String? = null)

Result of an email send attempt.

Properties

Link copied to clipboard

Disclaimer text appended to every AI-sent email. Customise via AI_Mail_Disclaimer.

Link copied to clipboard

Regex pattern that recipient addresses must match. null = allow all.

Link copied to clipboard

Matches CALL:mail(to='...', subject='...', body='...') in model output (complete pattern).

Link copied to clipboard

Fallback pattern for truncated CALL:mail where the body was cut off by token limit. Captures to, subject, and whatever body text is available (even without closing quote/paren).

Link copied to clipboard

Whether the mail bridge is enabled. Enable via AI_Mail_Enabled=true.

Link copied to clipboard

Whether the mail bridge is ready to send. If the system mail context is unavailable at runtime, sendMail will return an error.

Link copied to clipboard

Maximum mails per hour (global across all sessions).

Link copied to clipboard

Maximum mails per streaming session. Customise via AI_Mail_MaxPerSession.

Functions

Link copied to clipboard

Cleans an email address captured from model output. Strips emojis, icons (e.g. ✉), whitespace, and newlines.

Link copied to clipboard
fun clearSession(sessionId: String)

Cleans up rate-limiting state for a finished session.

Link copied to clipboard

Formats a mail result into a text block suitable for injecting into the conversation.

Link copied to clipboard
fun sendMail(to: String, subject: String, body: String, sessionId: String, clientIP: String = "unknown"): MailBridge.MailResult

Attempts to send an email on behalf of the AI model.