Automation API

Android device automation

AgentEmail

Interface

Read emails via IMAP protocol

Access email methods through agent.email. Allows reading emails from IMAP-enabled email accounts.

AgentEmail Interface
TypeScript
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()

TypeScript
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

NameTypeDescription
emailstringEmail address to read from
passwordstringEmail account password or app-specific password
host?stringIMAP server hostname (auto-detected for common providers)
port?numberIMAP server port (default: 993)
skip?numberNumber of emails to skip (for pagination)
limit?numberMaximum number of emails to return
proxyHost?stringProxy server hostname. Available since app version 2.123 (135)
proxyPort?numberProxy server port. Available since app version 2.123 (135)
proxyUser?stringProxy username for authentication. Available since app version 2.123 (135)
proxyPassword?stringProxy password for authentication. Available since app version 2.123 (135)

Returns

Promise<Email[]>Array of email messages

Examples

Read latest emails
TypeScript
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("---");
}
Find verification code
TypeScript
const emails = await agent.email.readIMAPEmails(
"user@gmail.com",
"app-password",
undefined, undefined, 0, 5
);
// Find email with verification code
const 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]);
}
}
Custom IMAP server
TypeScript
const emails = await agent.email.readIMAPEmails(
"user@company.com",
"password",
"imap.company.com", // custom host
993, // custom port
0,
20
);
Using a proxy server (since v2.123 (135))
TypeScript
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()

TypeScript
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

NameTypeDescription
optionsReadIMAPEmailsAdvancedOptionsSingle 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

Read the latest unread emails with metadata
TypeScript
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);
}
}
Find a verification code from the last 10 minutes
TypeScript
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]);
}
Paginate oldest-first, headers only (fast)
TypeScript
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;
}
Read the Spam folder and list all folders
TypeScript
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.

TypeScript
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.

TypeScript
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.

TypeScript
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

Email

Represents an email message.

TypeScript
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

PropertyTypeDescription
idstringUnique identifier for the email
subjectstringEmail subject line
fromstringSender's email address
fromNamestringSender's display name
tostring[]Array of recipient email addresses
ccstring[]Array of CC recipient addresses
bccstring[]Array of BCC recipient addresses
datenumberEmail timestamp in Unix milliseconds
bodystringEmail body content (HTML or plain text)
isHtmlbooleanWhether the body content is HTML
isReadbooleanWhether the email has been read
hasAttachmentsbooleanWhether the email has attachments
attachmentNamesstring[]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.