Docs / Language & functions
a9script authoring pack — email functions
The email functions, with their arguments and a line of real script each. Called by bare name; nothing is imported. A call marked pauses does real work outside the script: the run is saved, and continues on the next line with the result.
sendMail(message) — pauses
Sends an email through one of the environment’s mail accounts. The message says what to send; which server and which credentials is the account’s configuration, so a script never holds a password. When the environment has exactly one account it is used by default — name one in account when there are several.
message(object) —{ to, subject, text?, html?, cc?, bcc?, from?, replyTo?, account?, attachments? }. Recipients (to,cc,bcc) are addresses as text — comma-separated — or a list of them.textandhtmlare the two ways to say the body; give both and the reader’s mail client picks.fromreplaces the account’s own address as the visible sender (“Alerts alerts@acme.example”).attachmentsis a list of{ filename, contentBase64, contentType?, cid? }— contents as base64 text, the same shapecreatePdfanswers; give an attachment acidand the html body can show it inline as<img src="cid:...">. Any other field is refused by name.
Answers: { ok: true, mode, messageId? } — the send succeeded. mode is what the account did with it: deliver sent it as written, redirect sent it to the addresses configured on the account instead of the recipients you named, and record sent nothing and wrote the message into this run’s log. messageId is the identifier the mail server will show the message under, when there was a send and it stated one. Under record the answer also carries recorded — { to, cc, bcc, subject, body }, the message that was not sent. When the server took the message but turned some recipients away, the send stands and the answer carries refused, the addresses it would not take. A send that cannot happen — no account, an unknown account name, several accounts and none named, contents that are not base64, a server that refuses, a message over 4 MB through a Microsoft 365 account — raises a catchable error saying which, naming the account. A send that failed after the server had taken the message raises an error saying the mail may have been sent: check before you send it again, or the recipients may get it twice.
const report = createPdf({
title: "Weekly report",
blocks: [{ kind: "heading", level: 1, text: "Weekly report" }]
});
const receipt = sendMail({
to: "ops@acme.example",
subject: "Weekly report",
text: "The report is attached.",
attachments: [{ filename: "report.pdf", contentBase64: report, contentType: "application/pdf" }]
});
log("sent as " + receipt.messageId);