# Email API The Email API provides endpoints for email operations including sending, receiving, and managing email accounts through the Stalwart mail server integration. ## Overview Email functionality in General Bots is available through: 1. **REST API** - Documented in this chapter 2. **BASIC Keywords** - `SEND MAIL` for scripts 3. **Email Module** - Background processing and IMAP/SMTP integration ## Endpoints ### Send Email **POST** `/api/email/send` Send an email message. **Request:** ```json { "to": ["recipient@example.com"], "cc": ["cc@example.com"], "bcc": [], "subject": "Meeting Tomorrow", "body": "Hi, just a reminder about our meeting.", "body_type": "text", "attachments": [] } ``` **Response:** ```json { "message_id": "msg-abc123", "status": "sent", "timestamp": "2024-01-15T10:30:00Z" } ``` **Body Types:** - `text` - Plain text - `html` - HTML formatted ### List Emails **GET** `/api/email/inbox` Retrieve inbox messages. **Query Parameters:** - `folder` - Folder name (default: INBOX) - `limit` - Number of messages (default: 50) - `offset` - Pagination offset - `unread` - Filter unread only (boolean) - `since` - Messages since date (ISO 8601) **Response:** ```json { "messages": [ { "id": "email-001", "from": "sender@example.com", "subject": "Hello", "preview": "Just wanted to say hi...", "date": "2024-01-15T09:00:00Z", "read": false, "has_attachments": false } ], "total": 142, "unread_count": 5 } ``` ### Get Email **GET** `/api/email/:id` Get specific email details. **Response:** ```json { "id": "email-001", "from": { "name": "John Doe", "email": "john@example.com" }, "to": [ { "name": "You", "email": "you@example.com" } ], "cc": [], "subject": "Meeting Notes", "body": "Here are the notes from today's meeting...", "body_html": "
Here are the notes from today's meeting...
", "date": "2024-01-15T09:00:00Z", "read": true, "attachments": [ { "id": "att-001", "filename": "notes.pdf", "size": 102400, "content_type": "application/pdf" } ] } ``` ### Delete Email **DELETE** `/api/email/:id` Delete an email message. **Response:** ```json { "status": "deleted", "message_id": "email-001" } ``` ### Get Attachment **GET** `/api/email/:id/attachments/:attachment_id` Download an email attachment. **Response:** Binary file with appropriate Content-Type header. ### Mark as Read **PUT** `/api/email/:id/read` Mark email as read. **Request:** ```json { "read": true } ``` ### Move Email **PUT** `/api/email/:id/move` Move email to a different folder. **Request:** ```json { "folder": "Archive" } ``` ### List Folders **GET** `/api/email/folders` List available email folders. **Response:** ```json { "folders": [ { "name": "INBOX", "path": "INBOX", "unread_count": 5, "total_count": 142 }, { "name": "Sent", "path": "Sent", "unread_count": 0, "total_count": 89 }, { "name": "Drafts", "path": "Drafts", "unread_count": 0, "total_count": 3 } ] } ``` ### Create Draft **POST** `/api/email/drafts` Create an email draft. **Request:** ```json { "to": ["recipient@example.com"], "subject": "Draft subject", "body": "Draft content..." } ``` **Response:** ```json { "draft_id": "draft-001", "status": "saved" } ``` ### Send Draft **POST** `/api/email/drafts/:id/send` Send a previously saved draft. **Response:** ```json { "message_id": "msg-abc123", "status": "sent" } ``` ## Email Accounts ### List Accounts **GET** `/api/email/accounts` List configured email accounts. **Response:** ```json { "accounts": [ { "id": "account-001", "email": "user@example.com", "provider": "stalwart", "status": "connected" } ] } ``` ### Add Account **POST** `/api/email/accounts` Add a new email account. **Request:** ```json { "email": "user@example.com", "imap_server": "imap.example.com", "imap_port": 993, "smtp_server": "smtp.example.com", "smtp_port": 587, "username": "user@example.com", "password": "app-specific-password" } ``` **Response:** ```json { "account_id": "account-002", "status": "connected", "message": "Account added successfully" } ``` ## BASIC Integration Use email in your BASIC scripts: ```basic ' Simple email SEND MAIL "recipient@example.com", "Subject", "Body" ' With variables recipient = HEAR "Who should I email?" subject = HEAR "What's the subject?" body = HEAR "What's the message?" SEND MAIL recipient, subject, body TALK "Email sent!" ``` ## Configuration Configure email in `config.csv`: ```csv key,value smtp-server,smtp.gmail.com smtp-port,587 imap-server,imap.gmail.com imap-port,993 email-username,your-email@gmail.com email-password,your-app-password email-from,Your Name