Module Reference
Send and reply to emails from Cloud Code.
What This Adds In Cloud Code
- Transactional email sending from Cloud Code flows.
- Template-based sends and direct text/html sends.
- Reply helper with threading header handling.
Quick Start
const Mail = require('mail');
Mail.send({
to: 'customer@example.com',
subject: 'Welcome',
text: 'Welcome to our platform.'
});API
| Function | Params | Returns | Notes |
|---|---|---|---|
send(params) | mail payload | boolean | object | Requires to; non-template mail also requires subject and text. |
reply(params, rawHeaders, options) | payload + raw header string | boolean | object | Builds reply/thread metadata automatically. options supports fallback_to and fallback_subject. |
send payload fields:
| Field | Type | Notes |
|---|---|---|
to | string or string[] | Required. |
from | string | Defaults to the site's email address. |
cc, bcc | string or string[] | Optional. |
replyTo | string or string[] | Defaults to from. |
inReplyTo, references | string or string[] | Message ids for threading. reply() fills these in. |
subject, text, html | string | Required for non-template mail: subject and text. html is optional. |
template, variables | string, object | Send a notification template from the site instead of subject/text. |
headers | object | Custom X- headers, see below. |
attachments | object or array | { 'file.pdf': '<base64>' } or [{ filename, content }], content base64-encoded. |
Addresses accept display names: 'Support <support@example.com>'.
Custom headers
headers only adds your own X- headers:
Mail.send({
to: 'customer@example.com',
subject: 'Your export',
text: 'Your export is ready.',
headers: { 'X-Export-Id': exportId }
});These are dropped without an error, and the mail is still sent:
- Names that don't start with
X-(any case). - Names starting with a reserved prefix:
X-Nimbu-,X-Original-,X-TEM-,X-Scw-,X-Scaleway-. - Values containing a line break (CR or LF).
Practical Examples
const Mail = require('mail');
Mail.send({
to: request.params.email,
template: 'order-confirmation',
variables: {
first_name: request.params.first_name,
order_number: request.params.order_number
}
});Mail.reply(
{
from: 'support@example.com',
text: request.params.reply_text
},
request.params.raw_headers,
{
fallback_to: request.params.to,
fallback_subject: 'Re: Support'
}
);Failure Modes & Gotchas
- Missing required fields can return falsey result instead of hard crash.
- A
templatethat doesn't exist on the site sends nothing and logs an error. - Attachments with a file type the email provider refuses are dropped and logged; the mail is still sent.
- Keep template variable keys stable with mail template definitions.
- Validate recipient email before send.