ObjectStackObjectStack

services.email — the outbound mail API

Send email from a plugin or flow with services.email — send() and sendTemplate(), the result they return, and the typed error codes to handle.

  • Stability: stable
  • Canonical source: packages/spec/src/contracts/email-service.ts
  • Registry slot: email — resolve with ctx.getService('email').

Methods

services.email.send(input: SendEmailInput): Promise<SendEmailResult>
services.email.sendTemplate(input: SendTemplateInput): Promise<SendEmailResult>

services.email.getDefaultFrom?.() returns the configured sender, the from that send() uses when the input names none. It is undefined when no sender is configured.

SendTemplateInput and RenderTemplateInput take an optional callerPackageId: the package the send is made from. The template name is then resolved from that package: its own template, else the one holder in its declared dependency closure, else 409 RESOURCE_CONFLICT naming every candidate. A send that names no package reads the environment's own template, else the one package holder, else 409 naming every holder. The eight platform-requested auth.* mails resolve environment-wide whatever is passed. A flow's notify node passes its flow's package for you.

Over HTTP

POST /api/v1/email/send (client.email.send) hands a message to send(). Plugins and flows call send() directly and are not affected by what follows. The HTTP door checks four things first, and refuses a failure with 403 PERMISSION_DENIED before anything is sent:

  • The caller must hold the manage_platform_settings capability.
  • sentBy is always the authenticated caller. A body that names another user is refused.
  • The sender is the deployment's configured sender. A from naming another address is refused. A from naming the configured address is replaced by the configured value. Omit from to send as the configured sender. headers, when sent, must be a plain object that maps each header name to a single-line string value. Any other shape is refused, and so are custom From, Sender and Return-Path headers.
  • The message is recorded under the caller's active organization, which the door sets as organizationId. A body that names another organization is refused, and so is one that names an organization when the caller has none. identityMail is refused: identity mail (verification, password reset, invitations) is sent by the server itself. Omit both.

replyTo is not bounded. It is the caller's choice of Reply-To address and makes no claim about the sender.

From a script body

An action or a job body sends mail with ctx.email.send(input) when it declares the email.send capability (os build infers it from the call). The input is BodyMailSendInputSchema (@objectstack/spec/data): to, cc, bcc, replyTo, subject, text and/or html, and related, the record the message is about.

  • The message is sent from the configured sender. Its display name names the calling user beside the organization, for example Ann Lee via Acme. Replies go to replyTo.
  • It is recorded as sent by the calling user and under the caller's organization, with the same bounds as the HTTP door. from, sentBy, organizationId, headers and identityMail are not inputs, and a call that names one is refused with 400 VALIDATION_ERROR.
  • related must be a record the caller can read, in the caller's own scope. Otherwise the call is refused with 404 RECORD_NOT_FOUND and nothing is sent.
  • A message names at most 50 recipients across to, cc and bcc. Each of the three takes at most 4,000 characters as the mail log records it (every recipient as "Name" <address>, joined by , ), and a replyTo at most 320. The subject takes at most 998 characters, and text and html at most 1,000,000 each. A message over any of these is refused with 400 VALIDATION_ERROR.
  • While the organization's mail log records OS_EMAIL_APP_SEND_HOURLY_LIMIT messages (default 200) in the last hour, the call is refused with 429 RATE_LIMIT_EXCEEDED. A caller with no organization is counted against the whole log. The count is read before the send, so two calls racing for the last message of the hour can both be sent.
  • The message is sent only if the mail log records it. When the row cannot be written, the call is refused with 503 SERVICE_UNAVAILABLE and nothing is sent.
  • A message cannot be taken back, so a hook body cannot send mail: the write that runs the hook may still roll back. A hook whose body is granted email.send is refused when it is saved, validated, built or bound, and a call from a hook body is refused with 403 PERMISSION_DENIED. Send mail on a record change from an action, a job or a flow.
  • A call made while an action or a job runs inside a transaction, or inside the body's own ctx.api.transaction, is refused with 409 RESOURCE_CONFLICT.
  • A job body has no calling user, so its message is recorded with no sentBy.

The call answers { id, status }. A deployment with no mail service answers 501 NOT_IMPLEMENTED.

A row written into the mail log (sys_email, sys_identity_email) at queued by anything other than the mail service is sent from the configured sender, with no From, Sender or Return-Path header of its own, whatever the row said. Where no sender is configured, the mail log refuses such a row.

Breaking for direct inserts. Before this release, a queued row an app inserted into the mail log was sent from the from_address the row named. It is now sent from the configured sender. Send through ctx.email.send, which names the calling user in the sender's display name, instead of inserting the row.

Returns

SendEmailResult:

  • id: string
  • status: 'queued' | 'sent' | 'failed'
  • messageId?: string
  • error?: string

Typed Error Codes

sendTemplate may return/throw implementation errors including:

  • TEMPLATE_NOT_FOUND
  • TEMPLATE_INACTIVE
  • MISSING_VARIABLES
  • TEMPLATE_INVALID: the stored template has a hole the authoring rules refuse (a second formatter, or a currency argument that is not a literal ISO 4217 code). It is permanent: retrying fails until the template is re-published without that hole. renderTemplate raises it too. It is also raised when the template has a currency hole with no argument ({{ amount | currency }}) and the call carries no currency: such a hole formats in the organization's default currency (localization.currency), which the notification channels pass for you and a direct caller passes as currency. Nothing is sent until the default is set or the hole names its code.

On this page