Skip to content

agent.email

Interface

Read emails from IMAP-enabled email accounts.

AgentEmail Interface
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[]>;
}

readIMAPEmails

Signature
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).

ParameterTypeDescription
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

Read latest emails
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
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
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))
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)
);

Email

Represents an email message.

Email
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

NameTypeDescription
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
For Gmail accounts, use an App Password instead of your regular password. Regular passwords won't work if 2FA is enabled.