Email Endpoints

Implementation: inc/Api/Email.php

Base URL: /wp-json/datamachine/v1/email

Overview

Email endpoints expose Data Machine email abilities over REST for sending mail, reading IMAP inboxes, replying, moving, flagging, deleting, unsubscribing, and testing the stored IMAP connection.

Authentication

All email routes require Data Machine manage permission via PermissionHelper::can_manage().

PermissionHelper::can_manage() passes when the caller has manage_flows, manage_settings, or manage_agents. Administrators also pass through the manage_options fallback built into PermissionHelper.

External REST clients should use WordPress application passwords or cookie auth. Inbox operations also require a configured email_imap auth provider; missing IMAP credentials return not_configured with HTTP 400.

Route Table

MethodRouteAbilityPurpose
POST/email/senddatamachine/send-emailSend an email with optional headers and attachments.
GET/email/fetchdatamachine/fetch-emailFetch messages from an IMAP folder.
GET/email/{uid}/readdatamachine/fetch-emailRead one message by UID.
POST/email/replydatamachine/email-replyReply with threading headers.
DELETE/email/{uid}datamachine/email-deleteDelete one message by UID.
POST/email/{uid}/movedatamachine/email-moveMove one message to another folder.
POST/email/{uid}/flagdatamachine/email-flagSet or clear an IMAP flag.
POST/email/batch/movedatamachine/email-batch-moveMove messages matching an IMAP search.
POST/email/batch/flagdatamachine/email-batch-flagFlag messages matching an IMAP search.
POST/email/batch/deletedatamachine/email-batch-deleteDelete messages matching an IMAP search.
POST/email/{uid}/unsubscribedatamachine/email-unsubscribeUnsubscribe from a list using one message’s headers.
POST/email/batch/unsubscribedatamachine/email-batch-unsubscribeUnsubscribe from lists matching an IMAP search.
POST/email/test-connectiondatamachine/email-test-connectionTest stored IMAP credentials.

Core Parameters

Send And Reply

ParameterTypeRequiredNotes
tostringyesRecipient email address or comma-separated addresses for send.
subjectstringyesSubject line. Send supports {month}, {year}, {site_name}, and {date} placeholders.
bodystringyesHTML or plain-text body.
cc, bccstringnoComma-separated addresses. bcc is send-only.
from_name, from_email, reply_tostringnoSend headers. Defaults use site name/admin email when omitted.
content_typestringnotext/html by default; text/plain is also supported.
attachmentsarraynoServer file paths for wp_mail() attachments.
in_reply_tostringreply onlyMessage-ID of the email being replied to.
referencesstringnoReferences header chain for threading.

Fetch And Message Operations

ParameterTypeDefaultNotes
folderstringINBOXSource IMAP folder.
uidintegerroute paramMessage UID for read/delete/move/flag/unsubscribe.
searchstringUNSEEN for fetchIMAP search string, for example ALL, UNSEEN, FROM "github.com", or SINCE "1-Mar-2026".
maxintegerroute-specificSafety limit for fetch or batch operations.
offsetinteger0Pagination offset for fetch.
headers_onlybooleanfalseFast list mode; skips body parsing.
mark_as_readbooleanfalseMark fetched messages as read.
download_attachmentsbooleanfalseDownload attachments into local storage.
destinationstringrequired for moveTarget IMAP folder, such as Archive or [Gmail]/All Mail.
flagstringrequired for flagIMAP flag such as Seen, Flagged, Answered, Deleted, or Draft.
actionstringsetset or clear for flag endpoints.

Response Shape

Email endpoints return ability-native success objects. Failed ability results are normalized to an email_error REST error with HTTP 400.

Send response shape:

json
{
  "success": true,
  "message": "Email sent successfully",
  "recipients": ["[email protected]"],
  "subject": "Weekly report",
  "logs": []
}

Fetch response shape:

json
{
  "success": true,
  "data": {
    "items": [],
    "count": 10,
    "total_matches": 42,
    "offset": 0,
    "has_more": true
  },
  "logs": []
}

Batch operation response shapes include counters such as moved_count, flagged_count, deleted_count, unsubscribed, failed, or no_header depending on the route.

Agent Usage Examples

List unread message headers without marking them read:

bash
curl "https://example.com/wp-json/datamachine/v1/email/fetch?search=UNSEEN&headers_only=1&max=20" 
  -u username:application_password

Move GitHub notifications to an archive folder:

bash
curl -X POST https://example.com/wp-json/datamachine/v1/email/batch/move 
  -H "Content-Type: application/json" 
  -u username:application_password 
  -d '{"search":"FROM "github.com"","destination":"Archive","max":100}'

Send a concise agent report:

bash
curl -X POST https://example.com/wp-json/datamachine/v1/email/send 
  -H "Content-Type: application/json" 
  -u username:application_password 
  -d '{"to":"[email protected]","subject":"Daily agent summary","body":"<p>No blockers.</p>"}'