AgentEmail
InterfaceRead emails via IMAP protocol
Access email methods through agent.email. Allows reading emails from IMAP-enabled email accounts.
interface AgentEmail { readIMAPEmails( email: string, password: string, host?: string, port?: number, skip?: number, limit?: number, proxyHost?: string, proxyPort?: number, proxyUser?: string, proxyPassword?: string ): Promise<Email[]>;
readIMAPEmailsAdvanced( options: ReadIMAPEmailsAdvancedOptions ): Promise<ReadIMAPEmailsAdvancedResult>;}Methods
readIMAPEmails()
readIMAPEmails(email: string, password: string, host?: string, port?: number, skip?: number, limit?: number, proxyHost?: string, proxyPort?: number, proxyUser?: string, proxyPassword?: string): Promise<Email[]>Reads emails from an IMAP server. Useful for automations that need to verify email content (e.g., verification codes, confirmation emails).
Parameters
| Name | Type | Description |
|---|---|---|
email | string | Email address to read from |
password | string | Email account password or app-specific password |
host? | string | IMAP server hostname (auto-detected for common providers) |
port? | number | IMAP server port (default: 993) |
skip? | number | Number of emails to skip (for pagination) |
limit? | number | Maximum number of emails to return |
proxyHost? | string | Proxy server hostname. Available since app version 2.123 (135) |
proxyPort? | number | Proxy server port. Available since app version 2.123 (135) |
proxyUser? | string | Proxy username for authentication. Available since app version 2.123 (135) |
proxyPassword? | string | Proxy password for authentication. Available since app version 2.123 (135) |
Returns
Promise<Email[]>Array of email messages
Examples
const emails = await agent.email.readIMAPEmails( "user@gmail.com", "app-password-here", undefined, // auto-detect host undefined, // default port 0, // no skip 10 // limit to 10 emails);
for (const email of emails) { console.log("From:", email.from); console.log("Subject:", email.subject); console.log("---");}const emails = await agent.email.readIMAPEmails( "user@gmail.com", "app-password", undefined, undefined, 0, 5);
// Find email with verification codeconst verificationEmail = emails.find(e => e.subject.includes("Verification") || e.subject.includes("Code"));
if (verificationEmail) { // Extract code from email body const codeMatch = verificationEmail.body.match(/\b\d{6}\b/); if (codeMatch) { console.log("Verification code:", codeMatch[0]); }}const emails = await agent.email.readIMAPEmails( "user@company.com", "password", "imap.company.com", // custom host 993, // custom port 0, 20);const emails = await agent.email.readIMAPEmails( "user@gmail.com", "app-password", undefined, // auto-detect host undefined, // default port 0, // no skip 10, // limit "proxy.example.com", // proxy host 1080, // proxy port "proxyuser", // proxy username (optional) "proxypassword" // proxy password (optional));readIMAPEmailsAdvanced()
readIMAPEmailsAdvanced(options: ReadIMAPEmailsAdvancedOptions): Promise<ReadIMAPEmailsAdvancedResult>Advanced version of readIMAPEmails. Takes a single options object and adds server-side filtering (sender, recipient, subject, body, unread, date range), sorting by arrival time, custom folders, pagination metadata (total/unread/matched counts), markAsRead, timeouts, and body-size control. Never throws — connection or parameter errors are returned as { success: false, error }. Available since app version 3.0.4 (30004).
Parameters
| Name | Type | Description |
|---|---|---|
options | ReadIMAPEmailsAdvancedOptions | Single options object. Only email and password are required — see the type below for all supported fields and defaults. |
Returns
Promise<ReadIMAPEmailsAdvancedResult>{ success: false, error } on failure, or { success: true, metadata, emails } with folder counts and the matching emails
Examples
const result = await agent.email.readIMAPEmailsAdvanced({ email: "user@gmail.com", password: "app-password-here", unreadOnly: true, limit: 10,});
if (!result.success) { console.log("Failed to read emails:", result.error);} else { console.log("Total in INBOX:", result.metadata.totalEmails); console.log("Unread:", result.metadata.unreadEmails); for (const email of result.emails) { console.log(email.from, "-", email.subject); }}const result = await agent.email.readIMAPEmailsAdvanced({ email: "user@gmail.com", password: "app-password", from: "noreply@example.com", subject: "verification", since: Date.now() - 10 * 60 * 1000, // last 10 minutes sortOrder: "desc", limit: 1, markAsRead: true,});
if (result.success && result.emails.length > 0) { const codeMatch = result.emails[0].body.match(/\b\d{6}\b/); if (codeMatch) console.log("Verification code:", codeMatch[0]);}let skip = 0;while (true) { const result = await agent.email.readIMAPEmailsAdvanced({ email: "user@company.com", password: "password", host: "imap.company.com", sortOrder: "asc", // oldest first skip, limit: 50, includeBody: false, // headers only — much faster includeAttachmentNames: false, }); if (!result.success) break; for (const email of result.emails) console.log(email.date, email.subject); if (!result.metadata.hasMore) break; skip += result.emails.length;}const result = await agent.email.readIMAPEmailsAdvanced({ email: "user@gmail.com", password: "app-password", folder: "[Gmail]/Spam", includeFolderList: true, maxBodyLength: 500, // truncate long bodies});
if (result.success) { console.log("Available folders:", result.metadata.folders); console.log("Spam emails:", result.metadata.totalEmails);}Advanced Types
ReadIMAPEmailsAdvancedOptions
Options object accepted by readIMAPEmailsAdvanced(). Only email and password are required.
interface ReadIMAPEmailsAdvancedOptions { // Connection email: string; // Email address (required) password: string; // Password / app password (required) host?: string; // IMAP host (default: "imap.gmail.com") port?: number; // IMAP port (default: 993) folder?: string; // Folder to read (default: "INBOX") connectTimeoutMs?: number; // Connect timeout (default: 30000) readTimeoutMs?: number; // Read timeout (default: 60000)
// Pagination & sorting skip?: number; // Matching emails to skip (default: 0) limit?: number; // Max emails to return, 1-200 (default: 10) sortOrder?: "asc" | "desc"; // By arrival time; "desc" = newest first (default)
// Filters (combined with AND) since?: number; // Only emails with date >= this (Unix ms) before?: number; // Only emails with date < this (Unix ms) from?: string; // Sender address contains to?: string; // A TO recipient contains subject?: string; // Subject contains bodyContains?: string; // Body contains (server-side search) unreadOnly?: boolean; // Only unread emails (default: false)
// Content control includeBody?: boolean; // Include bodies (default: true; false is faster) preferHtml?: boolean; // Prefer HTML over plain text (default: true) maxBodyLength?: number; // Truncate body chars, 0 = unlimited (default: 0) includeAttachmentNames?: boolean; // Extract attachment names (default: true)
// Server-side effects & extras markAsRead?: boolean; // Mark returned emails as read (default: false) includeFolderList?: boolean; // Return folder list in metadata (default: false)
// Proxy proxyHost?: string; proxyPort?: number; proxyUser?: string; proxyPassword?: string;}ReadIMAPEmailsAdvancedResult
Discriminated union returned by readIMAPEmailsAdvanced(). Check success before accessing metadata/emails.
type ReadIMAPEmailsAdvancedResult = | { success: false; error: string } | { success: true; metadata: EmailReadMetadata; emails: AdvancedEmail[] };
interface EmailReadMetadata { totalEmails: number; // Total emails in the folder unreadEmails: number; // Unread emails in the folder (-1 if unknown) matchedEmails: number; // Emails matching the filters returnedEmails: number; // Emails in this page skip: number; // Skip applied limit: number; // Limit applied sortOrder: "asc" | "desc";// Sort order applied hasMore: boolean; // More matching emails beyond this page filtered: boolean; // Whether any filters were applied folder: string; // Folder that was read host: string; // IMAP host used port: number; // IMAP port used account: string; // Account that was read durationMs: number; // How long the read took folders?: string[]; // Folder list (when includeFolderList: true)}AdvancedEmail
Email shape returned by readIMAPEmailsAdvanced() — all Email fields plus extra IMAP details.
interface AdvancedEmail extends Email { uid: string | null; // Stable IMAP UID within the folder messageNumber: number; // IMAP message sequence number folder: string; // Folder the email was read from replyTo: string[]; // Reply-To addresses receivedDate: number | null; // Received timestamp (Unix ms) bodyTruncated: boolean; // true if body was cut by maxBodyLength isAnswered: boolean; // IMAP Answered flag isFlagged: boolean; // IMAP Flagged (starred) flag sizeBytes: number; // Message size in bytes (-1 if unknown)}Email Type
Represents an email message.
interface Email { id: string; // Unique email ID subject: string; // Email subject from: string; // Sender email address fromName: string; // Sender display name to: string[]; // Recipients cc: string[]; // CC recipients bcc: string[]; // BCC recipients date: number; // Timestamp (Unix ms) body: string; // Email body content isHtml: boolean; // true if body is HTML isRead: boolean; // Read status hasAttachments: boolean; // Has attachments attachmentNames: string[]; // Attachment file names}Email Properties
| Property | Type | Description |
|---|---|---|
id | string | Unique identifier for the email |
subject | string | Email subject line |
from | string | Sender's email address |
fromName | string | Sender's display name |
to | string[] | Array of recipient email addresses |
cc | string[] | Array of CC recipient addresses |
bcc | string[] | Array of BCC recipient addresses |
date | number | Email timestamp in Unix milliseconds |
body | string | Email body content (HTML or plain text) |
isHtml | boolean | Whether the body content is HTML |
isRead | boolean | Whether the email has been read |
hasAttachments | boolean | Whether the email has attachments |
attachmentNames | string[] | Names of attached files |
Security Note
For Gmail accounts, use an App Password instead of your regular password. Regular passwords won't work if 2FA is enabled.