Jmap.Client (JMAP v0.0.3)

View Source

JMAP client implementation that handles the core protocol communication.

Summary

Functions

Archives the specified email by moving it to the Archive folder.

Fetches the content of a blob by its ID.

Fetches the full content of a specific email.

Fetches emails from the Inbox.

Fetches a thread by its ID.

Creates a new JMAP client using configuration values.

Creates a new JMAP client with the given configuration.

Functions

archive_email(client, email_id)

Archives the specified email by moving it to the Archive folder.

fetch_blob(client, blob_id, type \\ "application/octet-stream")

Fetches the content of a blob by its ID.

Parameters

  • client: The JMAP client struct
  • blob_id: The ID of the blob to fetch
  • type: The expected content type (optional)
    • "text/html" or "text/plain" will return a string
    • "application/octet-stream" or other types will return binary

Returns

  • {:ok, content} - on success
    • For text types: {:ok, string}
    • For binary types: {:ok, binary}
  • {:error, reason} - on failure

Examples

iex> Jmap.Client.fetch_blob(client, "blob123", "text/html")
{:ok, "<html>...</html>"}
iex> Jmap.Client.fetch_blob(client, "blob123", "application/octet-stream")
{:ok, <<...>>}

fetch_email(client, email_id)

Fetches the full content of a specific email.

Parameters

  • client: The JMAP client struct
  • email_id: The ID of the email to fetch

Returns

  • {:ok, email} - on success, where email contains:
    • id: The email ID
    • subject: The email subject
    • from: List of sender email addresses
    • to: List of recipient email addresses
    • receivedAt: Timestamp when the email was received
    • textBody: List of text/plain body parts
    • htmlBody: List of text/html body parts
    • attachments: List of attachments
    • threadId: The thread ID this email belongs to
  • {:error, reason} - on failure

Examples

iex> Jmap.Client.fetch_email(client, "email123")
{:ok, %{
  "id" => "email123",
  "subject" => "Hello",
  "from" => [%{"email" => "sender@example.com", "name" => "Sender"}],
  "to" => [%{"email" => "recipient@example.com", "name" => "Recipient"}],
  "receivedAt" => "2024-01-01T00:00:00Z",
  "textBody" => [
    %{"blobId" => "blob123", "type" => "text/plain"},
    %{"blobId" => "blob124", "type" => "text/plain"}
  ],
  "htmlBody" => [
    %{"blobId" => "blob125", "type" => "text/html"}
  ],
  "attachments" => [],
  "threadId" => "thread123"
}}

fetch_emails(client, options \\ [])

Fetches emails from the Inbox.

Options

  • limit: Maximum number of emails to fetch (default: 50)
  • offset: Number of emails to skip (default: 0)
  • sort: Custom sort order (default: [%{"isAscending" => true, "property" => "receivedAt"}])

Returns

  • {:ok, result} - on success, where result contains:
    • "ids": List of email IDs
    • "total": Total number of emails
    • "position": Current position in the result set
  • {:error, reason} - on failure

fetch_thread(client, thread_id)

Fetches a thread by its ID.

Parameters

  • client: The JMAP client struct
  • thread_id: The ID of the thread to fetch

Returns

  • {:ok, thread} - on success, containing thread ID and associated email IDs
  • {:error, reason} - on failure

Examples

iex> Jmap.Client.fetch_thread(client, "thread123")
{:ok, %Jmap.Thread{id: "thread123", emailIds: ["email1", "email2"]}}

new()

Creates a new JMAP client using configuration values.

Returns

  • {:ok, client} - on success
  • {:error, reason} - on failure

Examples

iex> Jmap.new()
{:ok, %Jmap.Client{}}

new(api_token, provider, options \\ [])

Creates a new JMAP client with the given configuration.

Parameters

  • api_token: The JMAP API token (for Fastmail, generate this in Settings -> Password & Security -> App Passwords)
  • provider: The JMAP provider (e.g., :fastmail)
  • options: Additional options (optional)
    • api_url: Custom API URL (defaults to provider's default)
    • timeout: Request timeout in milliseconds (default: 30_000)

Returns

  • {:ok, client} - on success
  • {:error, reason} - on failure

Examples

iex> Jmap.new("your-api-token", :fastmail)
{:ok, %Jmap.Client{}}