Skip to main content

IMAP_RETURN_STATUS

IMAP operation return status codes.

IMAPMailToolkit

A toolkit for IMAP email operations. This toolkit provides comprehensive email functionality including:
  • Fetching emails with filtering options
  • Retrieving specific emails by ID
  • Sending emails via SMTP
  • Replying to emails
  • Moving emails to folders
  • Deleting emails
The toolkit implements connection pooling with automatic idle timeout to prevent resource leaks when used by LLM agents. Parameters:
  • imap_server (str, optional): IMAP server hostname. If not provided, will be obtained from environment variables.
  • imap_port (int, optional): IMAP server port. Defaults to 993. (default: 993)
  • smtp_server (str, optional): SMTP server hostname. If not provided, will be obtained from environment variables.
  • smtp_port (int, optional): SMTP server port. Defaults to 587. (default: 587)
  • username (str, optional): Email username. If not provided, will be obtained from environment variables.
  • password (str, optional): Email password. If not provided, will be obtained from environment variables.
  • timeout (Optional[float]): The timeout for the toolkit operations.
  • connection_idle_timeout (float): Maximum idle time (in seconds) before auto-closing connections. Defaults to 300 (5 minutes).

init

Initialize the IMAP Mail Toolkit. Parameters:
  • imap_server: IMAP server hostname (default: :obj:None)
  • imap_port: IMAP server port (default: :obj:993) (default: 993)
  • smtp_server: SMTP server hostname (default: :obj:None)
  • smtp_port: SMTP server port (default: :obj:587) (default: 587)
  • username: Email username (default: :obj:None)
  • password: Email password (default: :obj:None)
  • timeout: Timeout for operations (default: :obj:None)
  • connection_idle_timeout: Max idle time before auto-close (default: :obj:300 seconds)

_get_imap_connection

Returns: imaplib.IMAP4_SSL: Connected IMAP client

_get_smtp_connection

Returns: smtplib.SMTP: Connected SMTP client

_ensure_imap_ok

Ensure IMAP status is OK, otherwise raise a ConnectionError.

fetch_emails

Fetch emails from a folder with optional filtering. Parameters:
  • folder (Literal["INBOX"]): Email folder to search in (default: :obj:"INBOX")
  • limit (int): Maximum number of emails to retrieve (default: :obj:10)
  • unread_only (bool): If True, only fetch unread emails (default: :obj:False)
  • sender_filter (str, optional): Filter emails by sender email address (default: :obj:None)
  • subject_filter (str, optional): Filter emails by subject content (default: :obj:None)
Returns: List[Dict]: List of email dictionaries with metadata

get_email_by_id

Retrieve a specific email by ID with full metadata. Parameters:
  • email_id (str): ID of the email to retrieve
  • folder (Literal["INBOX"]): Folder containing the email (default: :obj:"INBOX")
Returns: Dict: Email dictionary with complete metadata

send_email

Send an email via SMTP. Parameters:
  • to_recipients (List[str]): List of recipient email addresses
  • subject (str): Email subject line
  • body (str): Plain text email body
  • cc_recipients (List[str], optional): List of CC recipient email addresses
  • bcc_recipients (List[str], optional): List of BCC recipient email addresses
  • html_body (str, optional): HTML version of email body
  • extra_headers (Dict[str, str], optional): Additional email headers
Returns: str: Success message

reply_to_email

Send a reply to an existing email. Parameters:
  • original_email_id (str): ID of the email to reply to
  • reply_body (str): Reply message body
  • folder (Literal["INBOX"]): Folder containing the original email (default: :obj:"INBOX")
  • html_body (str, optional): HTML version of reply body (default: :obj:None)
Returns: str: Success message

move_email_to_folder

Move an email to a different folder. Parameters:
  • email_id (str): ID of the email to move
  • target_folder (str): Destination folder name
  • source_folder (Literal["INBOX"]): Source folder name (default: :obj:"INBOX")
Returns: str: Success message

delete_email

Delete an email. Parameters:
  • email_id (str): ID of the email to delete
  • folder (Literal["INBOX"]): Folder containing the email (default: :obj:"INBOX")
  • permanent (bool): If True, permanently delete the email (default: :obj:False)
Returns: str: Success message

_extract_email_body

Extract plain text and HTML body from email message. Parameters:
  • email_message: Email message object
Returns: Dict[str, str]: Dictionary with ‘plain’ and ‘html’ body content

close

Close all open connections. This method should be called when the toolkit is no longer needed to properly clean up network connections.

del

Destructor to ensure connections are closed.

enter

Returns: IMAPMailToolkit: Self instance

exit

Context manager exit, ensuring connections are closed. Parameters:
  • exc_type: Exception type if an exception occurred
  • exc_val: Exception value if an exception occurred
  • exc_tb: Exception traceback if an exception occurred

get_tools

Returns: List[FunctionTool]: List of available tools