|
| 1 | +<a id="camel.toolkits.imap_mail_toolkit"></a> |
| 2 | + |
| 3 | +<a id="camel.toolkits.imap_mail_toolkit.IMAP_RETURN_STATUS"></a> |
| 4 | + |
| 5 | +## IMAP_RETURN_STATUS |
| 6 | + |
| 7 | +```python |
| 8 | +class IMAP_RETURN_STATUS(Enum): |
| 9 | +``` |
| 10 | + |
| 11 | +IMAP operation return status codes. |
| 12 | + |
| 13 | +<a id="camel.toolkits.imap_mail_toolkit.IMAPMailToolkit"></a> |
| 14 | + |
| 15 | +## IMAPMailToolkit |
| 16 | + |
| 17 | +```python |
| 18 | +class IMAPMailToolkit(BaseToolkit): |
| 19 | +``` |
| 20 | + |
| 21 | +A toolkit for IMAP email operations. |
| 22 | + |
| 23 | +This toolkit provides comprehensive email functionality including: |
| 24 | +- Fetching emails with filtering options |
| 25 | +- Retrieving specific emails by ID |
| 26 | +- Sending emails via SMTP |
| 27 | +- Replying to emails |
| 28 | +- Moving emails to folders |
| 29 | +- Deleting emails |
| 30 | + |
| 31 | +The toolkit implements connection pooling with automatic idle timeout |
| 32 | +to prevent resource leaks when used by LLM agents. |
| 33 | + |
| 34 | +**Parameters:** |
| 35 | + |
| 36 | +- **imap_server** (str, optional): IMAP server hostname. If not provided, will be obtained from environment variables. |
| 37 | +- **imap_port** (int, optional): IMAP server port. Defaults to 993. (default: 993) |
| 38 | +- **smtp_server** (str, optional): SMTP server hostname. If not provided, will be obtained from environment variables. |
| 39 | +- **smtp_port** (int, optional): SMTP server port. Defaults to 587. (default: 587) |
| 40 | +- **username** (str, optional): Email username. If not provided, will be obtained from environment variables. |
| 41 | +- **password** (str, optional): Email password. If not provided, will be obtained from environment variables. |
| 42 | +- **timeout** (Optional[float]): The timeout for the toolkit operations. |
| 43 | +- **connection_idle_timeout** (float): Maximum idle time (in seconds) before auto-closing connections. Defaults to 300 (5 minutes). |
| 44 | + |
| 45 | +<a id="camel.toolkits.imap_mail_toolkit.IMAPMailToolkit.__init__"></a> |
| 46 | + |
| 47 | +### __init__ |
| 48 | + |
| 49 | +```python |
| 50 | +def __init__( |
| 51 | + self, |
| 52 | + imap_server: Optional[str] = None, |
| 53 | + imap_port: int = 993, |
| 54 | + smtp_server: Optional[str] = None, |
| 55 | + smtp_port: int = 587, |
| 56 | + username: Optional[str] = None, |
| 57 | + password: Optional[str] = None, |
| 58 | + timeout: Optional[float] = None, |
| 59 | + connection_idle_timeout: float = 300.0 |
| 60 | +): |
| 61 | +``` |
| 62 | + |
| 63 | +Initialize the IMAP Mail Toolkit. |
| 64 | + |
| 65 | +**Parameters:** |
| 66 | + |
| 67 | +- **imap_server**: IMAP server hostname (default: :obj:`None`) |
| 68 | +- **imap_port**: IMAP server port (default: :obj:`993`) (default: 993) |
| 69 | +- **smtp_server**: SMTP server hostname (default: :obj:`None`) |
| 70 | +- **smtp_port**: SMTP server port (default: :obj:`587`) (default: 587) |
| 71 | +- **username**: Email username (default: :obj:`None`) |
| 72 | +- **password**: Email password (default: :obj:`None`) |
| 73 | +- **timeout**: Timeout for operations (default: :obj:`None`) |
| 74 | +- **connection_idle_timeout**: Max idle time before auto-close (default: :obj:`300` seconds) |
| 75 | + |
| 76 | +<a id="camel.toolkits.imap_mail_toolkit.IMAPMailToolkit._get_imap_connection"></a> |
| 77 | + |
| 78 | +### _get_imap_connection |
| 79 | + |
| 80 | +```python |
| 81 | +def _get_imap_connection(self): |
| 82 | +``` |
| 83 | + |
| 84 | +**Returns:** |
| 85 | + |
| 86 | + imaplib.IMAP4_SSL: Connected IMAP client |
| 87 | + |
| 88 | +<a id="camel.toolkits.imap_mail_toolkit.IMAPMailToolkit._get_smtp_connection"></a> |
| 89 | + |
| 90 | +### _get_smtp_connection |
| 91 | + |
| 92 | +```python |
| 93 | +def _get_smtp_connection(self): |
| 94 | +``` |
| 95 | + |
| 96 | +**Returns:** |
| 97 | + |
| 98 | + smtplib.SMTP: Connected SMTP client |
| 99 | + |
| 100 | +<a id="camel.toolkits.imap_mail_toolkit.IMAPMailToolkit._ensure_imap_ok"></a> |
| 101 | + |
| 102 | +### _ensure_imap_ok |
| 103 | + |
| 104 | +```python |
| 105 | +def _ensure_imap_ok(self, status: str, action: str): |
| 106 | +``` |
| 107 | + |
| 108 | +Ensure IMAP status is OK, otherwise raise a ConnectionError. |
| 109 | + |
| 110 | +<a id="camel.toolkits.imap_mail_toolkit.IMAPMailToolkit.fetch_emails"></a> |
| 111 | + |
| 112 | +### fetch_emails |
| 113 | + |
| 114 | +```python |
| 115 | +def fetch_emails( |
| 116 | + self, |
| 117 | + folder: Literal['INBOX'] = 'INBOX', |
| 118 | + limit: int = 10, |
| 119 | + unread_only: bool = False, |
| 120 | + sender_filter: Optional[str] = None, |
| 121 | + subject_filter: Optional[str] = None |
| 122 | +): |
| 123 | +``` |
| 124 | + |
| 125 | +Fetch emails from a folder with optional filtering. |
| 126 | + |
| 127 | +**Parameters:** |
| 128 | + |
| 129 | +- **folder** (`Literal["INBOX"]`): Email folder to search in (default: :obj:`"INBOX"`) |
| 130 | +- **limit** (int): Maximum number of emails to retrieve (default: :obj:`10`) |
| 131 | +- **unread_only** (bool): If True, only fetch unread emails (default: :obj:`False`) |
| 132 | +- **sender_filter** (str, optional): Filter emails by sender email address (default: :obj:`None`) |
| 133 | +- **subject_filter** (str, optional): Filter emails by subject content (default: :obj:`None`) |
| 134 | + |
| 135 | +**Returns:** |
| 136 | + |
| 137 | + List[Dict]: List of email dictionaries with metadata |
| 138 | + |
| 139 | +<a id="camel.toolkits.imap_mail_toolkit.IMAPMailToolkit.get_email_by_id"></a> |
| 140 | + |
| 141 | +### get_email_by_id |
| 142 | + |
| 143 | +```python |
| 144 | +def get_email_by_id(self, email_id: str, folder: Literal['INBOX'] = 'INBOX'): |
| 145 | +``` |
| 146 | + |
| 147 | +Retrieve a specific email by ID with full metadata. |
| 148 | + |
| 149 | +**Parameters:** |
| 150 | + |
| 151 | +- **email_id** (str): ID of the email to retrieve |
| 152 | +- **folder** (`Literal["INBOX"]`): Folder containing the email (default: :obj:`"INBOX"`) |
| 153 | + |
| 154 | +**Returns:** |
| 155 | + |
| 156 | + Dict: Email dictionary with complete metadata |
| 157 | + |
| 158 | +<a id="camel.toolkits.imap_mail_toolkit.IMAPMailToolkit.send_email"></a> |
| 159 | + |
| 160 | +### send_email |
| 161 | + |
| 162 | +```python |
| 163 | +def send_email( |
| 164 | + self, |
| 165 | + to_recipients: List[str], |
| 166 | + subject: str, |
| 167 | + body: str, |
| 168 | + cc_recipients: Optional[List[str]] = None, |
| 169 | + bcc_recipients: Optional[List[str]] = None, |
| 170 | + html_body: Optional[str] = None |
| 171 | +): |
| 172 | +``` |
| 173 | + |
| 174 | +Send an email via SMTP. |
| 175 | + |
| 176 | +**Parameters:** |
| 177 | + |
| 178 | +- **to_recipients** (List[str]): List of recipient email addresses |
| 179 | +- **subject** (str): Email subject line |
| 180 | +- **body** (str): Plain text email body |
| 181 | +- **cc_recipients** (List[str], optional): List of CC recipient email addresses |
| 182 | +- **bcc_recipients** (List[str], optional): List of BCC recipient email addresses |
| 183 | +- **html_body** (str, optional): HTML version of email body |
| 184 | +- **extra_headers** (Dict[str, str], optional): Additional email headers |
| 185 | + |
| 186 | +**Returns:** |
| 187 | + |
| 188 | + str: Success message |
| 189 | + |
| 190 | +<a id="camel.toolkits.imap_mail_toolkit.IMAPMailToolkit.reply_to_email"></a> |
| 191 | + |
| 192 | +### reply_to_email |
| 193 | + |
| 194 | +```python |
| 195 | +def reply_to_email( |
| 196 | + self, |
| 197 | + original_email_id: str, |
| 198 | + reply_body: str, |
| 199 | + folder: Literal['INBOX'] = 'INBOX', |
| 200 | + html_body: Optional[str] = None |
| 201 | +): |
| 202 | +``` |
| 203 | + |
| 204 | +Send a reply to an existing email. |
| 205 | + |
| 206 | +**Parameters:** |
| 207 | + |
| 208 | +- **original_email_id** (str): ID of the email to reply to |
| 209 | +- **reply_body** (str): Reply message body |
| 210 | +- **folder** (`Literal["INBOX"]`): Folder containing the original email (default: :obj:`"INBOX"`) |
| 211 | +- **html_body** (str, optional): HTML version of reply body (default: :obj:`None`) |
| 212 | + |
| 213 | +**Returns:** |
| 214 | + |
| 215 | + str: Success message |
| 216 | + |
| 217 | +<a id="camel.toolkits.imap_mail_toolkit.IMAPMailToolkit.move_email_to_folder"></a> |
| 218 | + |
| 219 | +### move_email_to_folder |
| 220 | + |
| 221 | +```python |
| 222 | +def move_email_to_folder( |
| 223 | + self, |
| 224 | + email_id: str, |
| 225 | + target_folder: str, |
| 226 | + source_folder: Literal['INBOX'] = 'INBOX' |
| 227 | +): |
| 228 | +``` |
| 229 | + |
| 230 | +Move an email to a different folder. |
| 231 | + |
| 232 | +**Parameters:** |
| 233 | + |
| 234 | +- **email_id** (str): ID of the email to move |
| 235 | +- **target_folder** (str): Destination folder name |
| 236 | +- **source_folder** (`Literal["INBOX"]`): Source folder name (default: :obj:`"INBOX"`) |
| 237 | + |
| 238 | +**Returns:** |
| 239 | + |
| 240 | + str: Success message |
| 241 | + |
| 242 | +<a id="camel.toolkits.imap_mail_toolkit.IMAPMailToolkit.delete_email"></a> |
| 243 | + |
| 244 | +### delete_email |
| 245 | + |
| 246 | +```python |
| 247 | +def delete_email( |
| 248 | + self, |
| 249 | + email_id: str, |
| 250 | + folder: Literal['INBOX'] = 'INBOX', |
| 251 | + permanent: bool = False |
| 252 | +): |
| 253 | +``` |
| 254 | + |
| 255 | +Delete an email. |
| 256 | + |
| 257 | +**Parameters:** |
| 258 | + |
| 259 | +- **email_id** (str): ID of the email to delete |
| 260 | +- **folder** (`Literal["INBOX"]`): Folder containing the email (default: :obj:`"INBOX"`) |
| 261 | +- **permanent** (bool): If True, permanently delete the email (default: :obj:`False`) |
| 262 | + |
| 263 | +**Returns:** |
| 264 | + |
| 265 | + str: Success message |
| 266 | + |
| 267 | +<a id="camel.toolkits.imap_mail_toolkit.IMAPMailToolkit._extract_email_body"></a> |
| 268 | + |
| 269 | +### _extract_email_body |
| 270 | + |
| 271 | +```python |
| 272 | +def _extract_email_body(self, email_message: email.message.Message): |
| 273 | +``` |
| 274 | + |
| 275 | +Extract plain text and HTML body from email message. |
| 276 | + |
| 277 | +**Parameters:** |
| 278 | + |
| 279 | +- **email_message**: Email message object |
| 280 | + |
| 281 | +**Returns:** |
| 282 | + |
| 283 | + Dict[str, str]: Dictionary with 'plain' and 'html' body content |
| 284 | + |
| 285 | +<a id="camel.toolkits.imap_mail_toolkit.IMAPMailToolkit.close"></a> |
| 286 | + |
| 287 | +### close |
| 288 | + |
| 289 | +```python |
| 290 | +def close(self): |
| 291 | +``` |
| 292 | + |
| 293 | +Close all open connections. |
| 294 | + |
| 295 | +This method should be called when the toolkit is no longer needed |
| 296 | +to properly clean up network connections. |
| 297 | + |
| 298 | +<a id="camel.toolkits.imap_mail_toolkit.IMAPMailToolkit.__del__"></a> |
| 299 | + |
| 300 | +### __del__ |
| 301 | + |
| 302 | +```python |
| 303 | +def __del__(self): |
| 304 | +``` |
| 305 | + |
| 306 | +Destructor to ensure connections are closed. |
| 307 | + |
| 308 | +<a id="camel.toolkits.imap_mail_toolkit.IMAPMailToolkit.__enter__"></a> |
| 309 | + |
| 310 | +### __enter__ |
| 311 | + |
| 312 | +```python |
| 313 | +def __enter__(self): |
| 314 | +``` |
| 315 | + |
| 316 | +**Returns:** |
| 317 | + |
| 318 | + IMAPMailToolkit: Self instance |
| 319 | + |
| 320 | +<a id="camel.toolkits.imap_mail_toolkit.IMAPMailToolkit.__exit__"></a> |
| 321 | + |
| 322 | +### __exit__ |
| 323 | + |
| 324 | +```python |
| 325 | +def __exit__( |
| 326 | + self, |
| 327 | + exc_type, |
| 328 | + exc_val, |
| 329 | + exc_tb |
| 330 | +): |
| 331 | +``` |
| 332 | + |
| 333 | +Context manager exit, ensuring connections are closed. |
| 334 | + |
| 335 | +**Parameters:** |
| 336 | + |
| 337 | +- **exc_type**: Exception type if an exception occurred |
| 338 | +- **exc_val**: Exception value if an exception occurred |
| 339 | +- **exc_tb**: Exception traceback if an exception occurred |
| 340 | + |
| 341 | +<a id="camel.toolkits.imap_mail_toolkit.IMAPMailToolkit.get_tools"></a> |
| 342 | + |
| 343 | +### get_tools |
| 344 | + |
| 345 | +```python |
| 346 | +def get_tools(self): |
| 347 | +``` |
| 348 | + |
| 349 | +**Returns:** |
| 350 | + |
| 351 | + List[FunctionTool]: List of available tools |
0 commit comments