Autorun AI Docs

9. Email


SendEmailAsync(to, subject, body, from)

async · returns boolean

Sends an email. The body may be plain text or HTML.

Prop

Type

const sent = await context.functions.SendEmailAsync(
  "client@example.com",
  "Your invoice is ready",
  "<p>Invoice <strong>#" + context.record.InvoiceNumber + "</strong> is attached.</p>",
  "billing@mycompany.com"
);

if (!sent) console.error("Invoice email to " + context.record.Email + " was not sent");

Using the default sender:

await context.functions.SendEmailAsync(
  "client@example.com",
  "Welcome aboard",
  "<p>Your account is ready.</p>",
  null
);
All four arguments are required

The from parameter is not optional in the process API even though the mail service treats it as such. Pass null explicitly to fall back to the configured sender.

Returns true when the message was accepted for delivery and false when it was not. It does not throw, so an unchecked call fails quietly. Note that true means accepted by the mail provider, not read by the recipient.


Building the message

There is no template engine and no attachment support in the process API — compose the body yourself.

/** @event afterCreate @entity invoices */
const invoice = await context.functions.getById("invoices", context.record.ID);
const client = invoice.ClientId;   // lookups arrive expanded

const body = `
  <h2>Invoice ${invoice.InvoiceNumber}</h2>
  <p>Dear ${client.Name},</p>
  <p>Amount due: <strong>${invoice.Amount} EUR</strong><br>
     Due date: ${invoice.DueDate}</p>
`;

await context.functions.SendEmailAsync(client.Email, "Invoice " + invoice.InvoiceNumber, body, null);

Keep reusable fragments in context.env rather than hardcoding them:

const footer = `<hr><p style="color:#888">${context.env.COMPANY_NAME} · ${context.env.COMPANY_ADDRESS}</p>`;
await context.functions.SendEmailAsync(to, subject, body + footer, context.env.BILLING_FROM);

Emailing a group

There is no group email function available to processes. Read the recipients and send individually:

const managers = await context.functions.get("users", {
  filter: { field: "RoleId", operator: "equal", value: "3" },
  pagination: { skip: 0, take: 500 }
});

let failed = 0;
for (const user of managers.records) {
  const ok = await context.functions.SendEmailAsync(
    user.Email,
    "New contract pending approval",
    "<p>A contract has been submitted and requires your review.</p>",
    null
  );
  if (!ok) failed++;
}

if (failed > 0) console.error(failed + " of " + managers.rowCount + " emails failed");
Watch the timeout on large batches

Each send is a separate network call. A few hundred recipients will exceed the default 30-second process timeout — raise it with @timeout, or split the work across a scheduled process that sends in pages.