Hoppa till innehållet

Sending

Sending

Send messages one at a time or in a batch. Both take the same message and return the same result for each message. The reference lists every field, and Limits gives every size and count.

The message

  • from must be an address on a verified sending domain of your account.
  • to, cc and bcc take plain addresses or objects with email and a display name. The recipients of all three count together.
  • subject is required, and so is at least one of text and html. Send both when you can, since each receiver picks what it shows.
  • streamId sets the stream the message goes on. Without it, the message goes on the server’s default transactional stream.
  • trackOpens and trackLinks override the stream’s tracking for this message. Without them, the stream decides.

Attachments, headers, tags and metadata

POST /v1/email
curl -X POST https://api.sendora.se/v1/email \
  -H "Authorization: Bearer $SENDORA_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
  "from": {
    "email": "billing@example.se",
    "name": "Example AB"
  },
  "to": [
    {
      "email": "anna@example.com",
      "name": "Anna Andersson"
    }
  ],
  "cc": [
    "ekonomi@example.se"
  ],
  "subject": "Invoice 2026-0912",
  "text": "Hi Anna, invoice 2026-0912 is attached. It is due on 2026-10-15.",
  "html": "<p>Hi Anna,</p><p>Invoice <strong>2026-0912</strong> is attached. It is due on 2026-10-15.</p>",
  "attachments": [
    {
      "name": "invoice-2026-0912.pdf",
      "content": "JVBERi0xLjcKJcOkw7zDtsOfCg==",
      "contentType": "application/pdf"
    }
  ],
  "headers": {
    "Reply-To": "ekonomi@example.se",
    "X-Invoice-Id": "2026-0912"
  },
  "tag": "invoice",
  "metadata": {
    "invoiceId": "2026-0912",
    "customerId": "4711"
  }
}'
import { readFile } from 'node:fs/promises';
import { Sendora } from '@sendora/sdk';

const sendora = new Sendora({
  token: process.env.SENDORA_API_TOKEN,
});

const invoice = await readFile(
  new URL('./invoice-2026-0912.pdf', import.meta.url),
);

const { messageId } = await sendora.email.send({
  from: {
    email: 'billing@example.se',
    name: 'Example AB',
  },
  to: [
    { email: 'anna@example.com', name: 'Anna Andersson' },
  ],
  cc: ['ekonomi@example.se'],
  subject: 'Invoice 2026-0912',
  text: 'Hi Anna, invoice 2026-0912 is attached. It is due on 2026-10-15.',
  html: '<p>Hi Anna,</p><p>Invoice <strong>2026-0912</strong> is attached. It is due on 2026-10-15.</p>',
  attachments: [
    {
      name: 'invoice-2026-0912.pdf',
      content: invoice,
      contentType: 'application/pdf',
    },
  ],
  headers: {
    'Reply-To': 'ekonomi@example.se',
    'X-Invoice-Id': '2026-0912',
  },
  tag: 'invoice',
  metadata: {
    invoiceId: '2026-0912',
    customerId: '4711',
  },
});

console.log(messageId);
import os
from pathlib import Path

from sendora import Sendora

sendora = Sendora(os.environ["SENDORA_API_TOKEN"])

invoice = (
    Path(__file__)
    .with_name("invoice-2026-0912.pdf")
    .read_bytes()
)

accepted = sendora.email.send(
    from_={
        "email": "billing@example.se",
        "name": "Example AB",
    },
    to=[
        {
            "email": "anna@example.com",
            "name": "Anna Andersson",
        }
    ],
    cc=["ekonomi@example.se"],
    subject="Invoice 2026-0912",
    text=(
        "Hi Anna, invoice 2026-0912 is attached. "
        "It is due on 2026-10-15."
    ),
    html=(
        "<p>Hi Anna,</p>"
        "<p>Invoice <strong>2026-0912</strong> "
        "is attached. It is due on 2026-10-15.</p>"
    ),
    attachments=[
        {
            "name": "invoice-2026-0912.pdf",
            "content": invoice,
            "content_type": "application/pdf",
        }
    ],
    headers={
        "Reply-To": "ekonomi@example.se",
        "X-Invoice-Id": "2026-0912",
    },
    tag="invoice",
    metadata={
        "invoiceId": "2026-0912",
        "customerId": "4711",
    },
)

print(accepted.message_id)
<?php

use Sendora\Attachment;
use Sendora\Sendora;

// The key comes from SENDORA_API_TOKEN.
$sendora = new Sendora();

$accepted = $sendora->email->send(
    from: [
        'email' => 'billing@example.se',
        'name' => 'Example AB',
    ],
    to: [
        [
            'email' => 'anna@example.com',
            'name' => 'Anna Andersson',
        ],
    ],
    cc: ['ekonomi@example.se'],
    subject: 'Invoice 2026-0912',
    text: 'Hi Anna, invoice 2026-0912 is attached. '
        . 'It is due on 2026-10-15.',
    html: '<p>Hi Anna,</p>'
        . '<p>Invoice <strong>2026-0912</strong> '
        . 'is attached. It is due on 2026-10-15.</p>',
    attachments: [
        Attachment::fromFile(
            __DIR__ . '/invoice-2026-0912.pdf',
            contentType: 'application/pdf',
        ),
    ],
    headers: [
        'Reply-To' => 'ekonomi@example.se',
        'X-Invoice-Id' => '2026-0912',
    ],
    tag: 'invoice',
    metadata: [
        'invoiceId' => '2026-0912',
        'customerId' => '4711',
    ],
);

echo $accepted->messageId, "\n";
import java.nio.file.Path;
import se.sendora.Sendora;
import se.sendora.model.AcceptedEmail;
import se.sendora.model.Address;
import se.sendora.model.Attachment;
import se.sendora.model.Email;

class SendWithAttachment {
  public static void main(String[] args) {
    Path pdf = Path.of("invoice-2026-0912.pdf");
    Attachment invoice =
        Attachment.fromFile(pdf)
            .contentType("application/pdf");
    Email email =
        Email.builder()
            .from("billing@example.se", "Example AB")
            .to(
                Address.of(
                    "anna@example.com", "Anna Andersson"))
            .cc("ekonomi@example.se")
            .subject("Invoice 2026-0912")
            .text(
                "Hi Anna, invoice 2026-0912 is attached."
                    + " It is due on 2026-10-15.")
            .html(
                "<p>Hi Anna,</p><p>Invoice"
                    + " <strong>2026-0912</strong> is"
                    + " attached. It is due on"
                    + " 2026-10-15.</p>")
            .addAttachment(invoice)
            .putHeader("Reply-To", "ekonomi@example.se")
            .putHeader("X-Invoice-Id", "2026-0912")
            .tag("invoice")
            .putMetadata("invoiceId", "2026-0912")
            .putMetadata("customerId", "4711")
            .build();

    // The server key comes from SENDORA_API_TOKEN.
    try (var sendora = Sendora.fromEnv()) {
      AcceptedEmail accepted =
          sendora.email().send(email);
      System.out.println(accepted.messageId());
    }
  }
}

Response

{
  "messageId": "4e1c3f2a-9b6d-4e5c-9a0f-3d4e5f6a7b82",
  "status": "accepted",
  "submittedAt": "2026-09-15T12:00:00.000Z",
  "test": false
}
  • attachments: send the content base64-encoded, without line breaks. contentType defaults to application/octet-stream. Set contentId to use the attachment as an inline image, with <img src="cid:…"> in the HTML. Sendora deletes the content once its mail server has the message, and keeps the name, type and size.
  • headers: your own headers, such as Reply-To, In-Reply-To or X- headers. Sendora writes the address, date, ID and trace headers itself, such as From, To, Subject, Date, Message-ID, Return-Path and DKIM-Signature. Setting one of them fails with invalid_request, and so does any header that starts with Content-, Resent- or ARC-. The headers field in the reference lists every one.
  • tag: one label per message, such as invoice or welcome. It comes back with the message, in the log’s search and in every webhook about the message.
  • metadata: your own key-value pairs. Keys are letters, digits, _, . and -. They come back with the message and in every webhook, so an order id here saves you a lookup when an event arrives.

The SDK takes attachment content as bytes, a Uint8Array or a Node Buffer, and encodes it. It passes a base64 string through as it is.

The SDK takes attachment content as bytes and encodes it. It passes a base64 string through as it is. The content id is content_id.

Attachment::fromFile() and Attachment::fromBytes() read the content and encode it. An array whose content is already base64 goes through as it is. The content id is contentId.

Attachment.fromFile(path) reads a file and Attachment.of(name, bytes) takes bytes, and the SDK encodes either. Attachment.ofBase64(name, base64) takes content that is already base64. The content id is set by contentId(id), which returns a copy of the attachment.

Idempotency

A network can fail after Sendora has accepted a message but before your code gets the answer. To retry safely, send an idempotency key: any printable text that identifies the request on your side. A retry with the same key returns the first answer instead of sending again. Without a key of your own, a retry of a request that went through sends the message twice. Limits gives the key’s length and how long Sendora remembers it.

POST /v1/email
curl -X POST https://api.sendora.se/v1/email \
  -H "Authorization: Bearer $SENDORA_API_TOKEN" \
  -H "Idempotency-Key: order-4711-confirmation" \
  -H "Content-Type: application/json" \
  -d '{
  "from": {
    "email": "order@example.se",
    "name": "Example AB"
  },
  "to": [
    "anna@example.com"
  ],
  "subject": "Order 4711 confirmed",
  "text": "Hi Anna, we have received order 4711 and will ship it tomorrow."
}'
import { Sendora } from '@sendora/sdk';

const sendora = new Sendora({
  token: process.env.SENDORA_API_TOKEN,
});

const { messageId } = await sendora.email.send(
  {
    from: {
      email: 'order@example.se',
      name: 'Example AB',
    },
    to: ['anna@example.com'],
    subject: 'Order 4711 confirmed',
    text: 'Hi Anna, we have received order 4711 and will ship it tomorrow.',
  },
  { idempotencyKey: 'order-4711-confirmation' },
);

console.log(messageId);
import os

from sendora import Sendora

sendora = Sendora(os.environ["SENDORA_API_TOKEN"])

accepted = sendora.email.send(
    from_={
        "email": "order@example.se",
        "name": "Example AB",
    },
    to=["anna@example.com"],
    subject="Order 4711 confirmed",
    text=(
        "Hi Anna, we have received order 4711 "
        "and will ship it tomorrow."
    ),
    idempotency_key="order-4711-confirmation",
)

print(accepted.message_id)
<?php

use Sendora\Sendora;

// The key comes from SENDORA_API_TOKEN.
$sendora = new Sendora();

$accepted = $sendora->email->send(
    from: [
        'email' => 'order@example.se',
        'name' => 'Example AB',
    ],
    to: ['anna@example.com'],
    subject: 'Order 4711 confirmed',
    text: 'Hi Anna, we have received order 4711 '
        . 'and will ship it tomorrow.',
    idempotencyKey: 'order-4711-confirmation',
);

echo $accepted->messageId, "\n";
import se.sendora.Sendora;
import se.sendora.model.AcceptedEmail;
import se.sendora.model.Email;

class ConfirmOrder {
  public static void main(String[] args) {
    Email email =
        Email.builder()
            .from("order@example.se", "Example AB")
            .to("anna@example.com")
            .subject("Order 4711 confirmed")
            .text(
                "Hi Anna, we have received order 4711 and"
                    + " will ship it tomorrow.")
            .build();
    String key = "order-4711-confirmation";

    // The server key comes from SENDORA_API_TOKEN.
    try (var sendora = Sendora.fromEnv()) {
      AcceptedEmail accepted =
          sendora.email().send(email, key);
      System.out.println(accepted.messageId());
    }
  }
}

Response

{
  "messageId": "5f2d4a3b-0c7e-4f6d-8b1a-4e5f6a7b8c93",
  "status": "accepted",
  "submittedAt": "2026-09-15T12:00:00.000Z",
  "test": false
}

The SDK sends a key with every request: yours if you set idempotencyKey, or else a random one. So its own retries after a lost connection never send twice. Set your own key when a retry may come from another process, such as a job runner.

The SDK sends a key with every request: yours if you set idempotency_key, or else a random one. So its own retries after a lost connection never send twice. Set your own key when a retry may come from another process, such as a job runner.

The SDK sends a key with every request: yours if you set idempotencyKey:, or else a random one. So its own retries after a lost connection never send twice. Set your own key when a retry may come from another process, such as a queued job.

The SDK sends a key with every send: yours if you pass it as the last argument, as in send(email, key), or else a random one. So its own retries after a lost connection never send twice. Set your own key when a retry may come from another process, such as a queued job.

Send the key in the Idempotency-Key header. A replayed answer has the header Idempotent-Replayed: true.

Keys are per server. The same key with a different message fails with idempotency_key_mismatch, since a changed request is a new request.

Batches

A batch sends a list of messages in one request, up to the number on Limits. It needs an idempotency key. Over HTTP, a batch without one fails with idempotency_key_required. The SDKs send a random key when you give none.

POST /v1/email/batch
curl -X POST https://api.sendora.se/v1/email/batch \
  -H "Authorization: Bearer $SENDORA_API_TOKEN" \
  -H "Idempotency-Key: invoices-2026-09-15-run-1" \
  -H "Content-Type: application/json" \
  -d '[
  {
    "from": {
      "email": "billing@example.se",
      "name": "Example AB"
    },
    "to": [
      "anna@example.com"
    ],
    "subject": "Invoice 2026-0912",
    "text": "Hi Anna, invoice 2026-0912 is due on 2026-10-15.",
    "tag": "invoice",
    "metadata": {
      "invoiceId": "2026-0912"
    }
  },
  {
    "from": {
      "email": "billing@example.se",
      "name": "Example AB"
    },
    "to": [
      "bo@example.com"
    ],
    "subject": "Invoice 2026-0913",
    "text": "Hi Bo, invoice 2026-0913 is due on 2026-10-15.",
    "tag": "invoice",
    "metadata": {
      "invoiceId": "2026-0913"
    }
  }
]'
import { Sendora } from '@sendora/sdk';

const sendora = new Sendora({
  token: process.env.SENDORA_API_TOKEN,
});

const { results } = await sendora.email.sendBatch(
  [
    {
      from: {
        email: 'billing@example.se',
        name: 'Example AB',
      },
      to: ['anna@example.com'],
      subject: 'Invoice 2026-0912',
      text: 'Hi Anna, invoice 2026-0912 is due on 2026-10-15.',
      tag: 'invoice',
      metadata: { invoiceId: '2026-0912' },
    },
    {
      from: {
        email: 'billing@example.se',
        name: 'Example AB',
      },
      to: ['bo@example.com'],
      subject: 'Invoice 2026-0913',
      text: 'Hi Bo, invoice 2026-0913 is due on 2026-10-15.',
      tag: 'invoice',
      metadata: { invoiceId: '2026-0913' },
    },
  ],
  { idempotencyKey: 'invoices-2026-09-15-run-1' },
);

for (const result of results) {
  if (result.status === 'accepted') {
    console.log(
      result.index,
      result.messageId,
      result.replayed ? 'sent earlier' : 'sent',
    );
  } else {
    console.log(
      result.index,
      result.error,
      result.message,
    );
  }
}
import os

from sendora import Sendora

sendora = Sendora(os.environ["SENDORA_API_TOKEN"])

batch = sendora.email.send_batch(
    [
        {
            "from_": {
                "email": "billing@example.se",
                "name": "Example AB",
            },
            "to": ["anna@example.com"],
            "subject": "Invoice 2026-0912",
            "text": (
                "Hi Anna, invoice 2026-0912 "
                "is due on 2026-10-15."
            ),
            "tag": "invoice",
            "metadata": {"invoiceId": "2026-0912"},
        },
        {
            "from_": {
                "email": "billing@example.se",
                "name": "Example AB",
            },
            "to": ["bo@example.com"],
            "subject": "Invoice 2026-0913",
            "text": (
                "Hi Bo, invoice 2026-0913 "
                "is due on 2026-10-15."
            ),
            "tag": "invoice",
            "metadata": {"invoiceId": "2026-0913"},
        },
    ],
    idempotency_key="invoices-2026-09-15-run-1",
)

for result in batch.results:
    if result.status == "accepted":
        print(
            result.index,
            result.message_id,
            "sent earlier" if result.replayed else "sent",
        )
    else:
        print(result.index, result.code, result.message)
<?php

use Sendora\Types\AcceptedBatchItem;
use Sendora\Sendora;

// The key comes from SENDORA_API_TOKEN.
$sendora = new Sendora();

$batch = $sendora->email->sendBatch(
    [
        [
            'from' => [
                'email' => 'billing@example.se',
                'name' => 'Example AB',
            ],
            'to' => ['anna@example.com'],
            'subject' => 'Invoice 2026-0912',
            'text' => 'Hi Anna, invoice 2026-0912 '
                . 'is due on 2026-10-15.',
            'tag' => 'invoice',
            'metadata' => ['invoiceId' => '2026-0912'],
        ],
        [
            'from' => [
                'email' => 'billing@example.se',
                'name' => 'Example AB',
            ],
            'to' => ['bo@example.com'],
            'subject' => 'Invoice 2026-0913',
            'text' => 'Hi Bo, invoice 2026-0913 '
                . 'is due on 2026-10-15.',
            'tag' => 'invoice',
            'metadata' => ['invoiceId' => '2026-0913'],
        ],
    ],
    idempotencyKey: 'invoices-2026-09-15-run-1',
);

foreach ($batch->results as $result) {
    if ($result instanceof AcceptedBatchItem) {
        printf(
            "%d %s %s\n",
            $result->index,
            $result->messageId,
            $result->replayed ? 'sent earlier' : 'sent',
        );
    } else {
        printf(
            "%d %s %s\n",
            $result->index,
            $result->errorCode,
            $result->message,
        );
    }
}
import java.util.List;
import se.sendora.Sendora;
import se.sendora.model.AcceptedBatchItem;
import se.sendora.model.BatchResult;
import se.sendora.model.Email;
import se.sendora.model.RefusedBatchItem;

class SendInvoices {
  public static void main(String[] args) {
    List<Email> emails =
        List.of(
            Email.builder()
                .from("billing@example.se", "Example AB")
                .to("anna@example.com")
                .subject("Invoice 2026-0912")
                .text(
                    "Hi Anna, invoice 2026-0912 is due on"
                        + " 2026-10-15.")
                .tag("invoice")
                .putMetadata("invoiceId", "2026-0912")
                .build(),
            Email.builder()
                .from("billing@example.se", "Example AB")
                .to("bo@example.com")
                .subject("Invoice 2026-0913")
                .text(
                    "Hi Bo, invoice 2026-0913 is due on"
                        + " 2026-10-15.")
                .tag("invoice")
                .putMetadata("invoiceId", "2026-0913")
                .build());
    String key = "invoices-2026-09-15-run-1";

    // The server key comes from SENDORA_API_TOKEN.
    try (var sendora = Sendora.fromEnv()) {
      BatchResult batch =
          sendora.email().sendBatch(emails, key);
      for (var item : batch.results()) {
        switch (item) {
          case AcceptedBatchItem sent ->
              System.out.printf(
                  "%d %s %s%n",
                  sent.index(),
                  sent.messageId(),
                  sent.replayed()
                      ? "sent earlier"
                      : "sent");
          case RefusedBatchItem refused ->
              System.out.printf(
                  "%d %s %s%n",
                  refused.index(),
                  refused.codeAsString(),
                  refused.message());
          default -> {} // a kind newer than this release
        }
      }
    }
  }
}

Response

{
  "results": [
    {
      "index": 0,
      "messageId": "2c9a1d0e-7f4b-4c3a-9e8d-1b2c3d4e5f60",
      "status": "accepted",
      "submittedAt": "2026-09-15T12:00:00.000Z",
      "test": false,
      "replayed": false
    },
    {
      "index": 1,
      "messageId": "3d0b2e1f-8a5c-4d4b-8f9e-2c3d4e5f6a71",
      "status": "accepted",
      "submittedAt": "2026-09-15T12:00:00.000Z",
      "test": false,
      "replayed": false
    }
  ]
}

Sendora accepts or refuses each message on its own, so one invalid message never fails the batch. The batch returns 200 with one result per message, in the order you sent them. Each has the status accepted, with the message’s id, or error, with the code in error and the message that a single send would have returned.

  • A retry with the same key sends nothing twice. Messages accepted before come back with replayed: true, and only the messages that failed are tried again. You may correct a failed message in the retry, since only accepted messages are remembered. Changing an accepted one fails that message with idempotency_key_mismatch.
  • A refusal that concerns the whole account fails the batch before any message is checked. That covers a paused account, one whose live servers wait for a payment, and sending stopped by Sendora.
  • The per-minute limit and the monthly cap count message by message, in order. The messages up to the limit are accepted, and the rest get rate_limited or monthly_cap_reached in their results.

Send from Better Auth

Better Auth sends no mail itself. For each kind of mail, it calls a function that you give it, with the user and the link to send. Send each one with the SDK, as below. The from address must be on a verified domain.

import {
  Sendora,
  type SendEmailRequest,
} from '@sendora/sdk';
import { betterAuth } from 'better-auth';

const sendora = new Sendora({
  token: process.env.SENDORA_API_TOKEN,
});

// Better Auth waits for the promise that a sender
// returns. This one resolves before Sendora answers, so
// the time a request takes never shows whether an
// account exists. A rejection that nothing catches would
// stop Node.
function sendInBackground(email: SendEmailRequest) {
  sendora.email.send(email).catch((error: unknown) => {
    console.error('Sendora did not take the mail', error);
  });
  return Promise.resolve();
}

const from = {
  email: 'no-reply@example.se',
  name: 'Example AB',
};

export const auth = betterAuth({
  emailAndPassword: {
    enabled: true,
    sendResetPassword: ({ user, url }) =>
      sendInBackground({
        from,
        to: [user.email],
        subject: 'Reset your password',
        text: `Choose a new password at Example: ${url}`,
        tag: 'password-reset',
      }),
  },
  emailVerification: {
    sendOnSignUp: true,
    sendVerificationEmail: ({ user, url }) =>
      sendInBackground({
        from,
        to: [user.email],
        subject: 'Confirm your email address',
        text: `Confirm your address at Example: ${url}`,
        tag: 'email-verification',
      }),
  },
});
  • Better Auth asks you not to await a send, so that the time it takes to answer never shows whether an account exists. The SDK throws when Sendora refuses a mail. Catch the error, since Node stops on a rejection that nothing catches.
  • On a host that can stop a function once it has answered, such as Vercel or Cloudflare Workers, pass the send to the host’s waitUntil instead.
  • In development, use a test server’s key. Sendora checks each mail and keeps it in the log, but delivers none. It never shows a mail’s text, so print the link while you develop.
  • The tag sets each kind of mail apart in the log and in every webhook.
  • Tracking leaves a text-only mail as it is. If you add html and the stream tracks links, set trackLinks: false, so that the link and its token stay out of the click events.

Better Auth’s plugins take functions of the same kind, and you send from each the same way. They are sendMagicLink for a magic link, sendVerificationOTP for a one-time code and sendInvitationEmail for an invitation to an organisation.

Send from a framework

Each framework’s guide sends mail the framework’s way:

  • Django: a view that sends with the Python SDK.
  • Express: a route that sends with the TypeScript SDK.
  • Laravel: Laravel’s mailables and notifications, through the Laravel package.
  • Next.js: a Server Action that sends with the TypeScript SDK.
  • Symfony: Symfony Mailer, through the PHP SDK’s transport.

What happens next

Sendora stores the message encrypted and hands it to its mail server within seconds. Its recipients[].status then changes as receivers answer, and its timeline fills with events, as Messages and events shows. To be told instead of polling, register a webhook.