Send an email from a script
sendEmail sends a message through a mail service the environment names, just like callService. It never stops the script. It tells you whether the email went, and the script decides what to do about it. In the sample, a paid order records Sent or Not sent with the reason. Here is the call, what the mail service receives, and why an email step shouldn't refuse a save.

When an order is paid, the customer should get a confirmation. A server-side script sends one with
sendEmail. We carried on with the order sample from
call an outside service.
The call
var sent = sendEmail('par_mail_service', {
to: email,
subject: `Order ${getValue('par_name')} is paid`,
body: `Thank you. Your payment reference is ${getValue('par_payment_reference')}.`
});
Like callService, the first argument is a service name, an environment variable that says
where the mail service is. The second is the message:
| Field | What it is |
|---|---|
to |
the address, or a list of them |
subject |
the subject line |
body |
the text |
cc |
more addresses, optional |
replyTo |
where replies go, optional |
html |
true if body is HTML |
What the mail service receives
Parsware doesn't talk to a mail server directly. It posts the message as JSON to the address in the variable, and whatever is there sends it: your mail provider's HTTP API, or a small adapter in front of your own mail server. Every script's message arrives in the same shape, so you set up one mail adapter per environment, not one per script.
We set Mail service to our stand-in:

Then we saved a new order, ORD-1002, as Paying, with the customer email lee@example.com. The
mail service received:
{
"to": ["lee@example.com"],
"cc": [],
"replyTo": null,
"subject": "Order ORD-1002 is paid",
"body": "Thank you. Your payment reference is PAY-593459.",
"html": false
}
And the order says so:

It never stops the script
sendEmail gives back { ok, error }, and that's all. It never stops the script, whatever went
wrong: an address the service refused, a mail service that's down, or no mail service set at all.
The script decides what a failed email means.
We pointed the variable at an address that answers 503, and paid another order, ORD-1003:

The order is paid and stored, and the email field says why the email didn't go. Here are the three orders side by side:

This is the code that does it:
if (sent.ok) {
updateRecord('par_order', recordId(), { par_email_status: 'Sent' });
} else {
updateRecord('par_order', recordId(), { par_email_status: `Not sent: ${sent.error}` });
}
The whole confirmation, once
The email lives in the same solution script as the payment, Order payment, so the two steps share one definition of what a paid order is:
func confirmByEmail() {
var email = getValue('par_customer_email');
if (getValue('par_status') == 'Paid' && getPreValue('par_status') != 'Paid' && !isEmpty(email)) {
var sent = sendEmail('par_mail_service', {
to: email,
subject: `Order ${getValue('par_name')} is paid`,
body: `Thank you. Your payment reference is ${getValue('par_payment_reference')}.`
});
if (sent.ok) {
updateRecord('par_order', recordId(), { par_email_status: 'Sent' });
} else {
updateRecord('par_order', recordId(), { par_email_status: `Not sent: ${sent.error}` });
}
}
}
The condition on the second line is what keeps the email to one. getPreValue reads the status as it was
before this save, so the email goes only at the moment the order becomes Paid. Saving a paid order again, to
correct a typing mistake in its notes, sends nothing. Without that check, every later save would email the customer
again.
An HTML email
Set html to true and the body is sent as HTML, which allows a heading, a paragraph and a link. Anything that comes
from a record, such as a customer's name, should be passed through htmlEncode first, so a name containing < or &
can't break the message or slip markup into it:
var name = htmlEncode(getValue('par_customer_name'));
var sent = sendEmail('par_mail_service', {
to: [getValue('par_customer_email')],
cc: ['accounts@example.com'],
replyTo: 'orders@example.com',
subject: `Order ${getValue('par_name')} is paid`,
html: true,
body: `<p>Dear ${name},</p><p>Your payment reference is <strong>${htmlEncode(getValue('par_payment_reference'))}</strong>.</p>`
});
to and cc take a list, so one message can go to several people. replyTo sends the customer's answer to a shared
inbox instead of to whatever address the mail service sends from. (The field par_customer_name isn't in the sample;
it's here to show the encoding.)
Making failed emails easy to put right
Recording Not sent: … on the order is only half the job; somebody has to notice. Two small additions make that reliable:
- A view of what didn't go. A view of orders filtered on Confirmation email contains Not sent is the list somebody works through when the mail service comes back. Filters with And and Or builds it.
- A way to try again. Because the email step only sends when the status changes to Paid, re-saving won't resend. A small field, Resend confirmation, that a plugin step watches, or a command button that starts a short process, gives a person a deliberate way to send it once more.
The general rule from both articles holds here: an answer is not a fault. A refused address, a mail service that is
down, a missing variable: each comes back as ok: false with a reason, and the script decides what that means for the
record. Only the script knows whether an unsent email matters.
When you don't need a script at all
Not every email needs code. For tell these people when a record is created, changed or deleted, the platform's own
notifications may already be enough: a rule on a table, with the addresses to tell, set up without writing a
script. Use sendEmail when the message depends on the record's values, goes to an address on the record itself, or is
part of a larger piece of logic, like a payment.
Setting it up per environment
As with callService, the mail service's address is an environment variable, so each environment sends through its
own:
| Environment | par_mail_service |
|---|---|
| Development | A webhook-testing address, so messages are captured rather than delivered |
| Test | The provider's test mode, or an inbox only the team reads |
| Production | The real mail provider |
That way nobody in Test can email a real customer by accident. Environment variables shows where the value is set, and the value never travels in the solution.
Questions that come up
Can a form rule send an email? No. Sending happens on the server, in plugin steps, Custom APIs and scheduled jobs, where the mail service's address can't be seen or changed from a browser.
Can I attach a file? The message has text, or HTML, and the fields in the table above. To share a document, put a link to it in the body; Files from a script shows how a script makes a file and a link to it.
Who is the email from? Whatever the mail service is set up to send as. The script sets the replyTo, not the sender,
so the sending address is decided once, by whoever runs the mail service, rather than in every script.
After the write, and why it doesn't refuse
The email step runs after the order is saved: it's a PostOperation step. That's the right place. The order is paid and stored by then, so a mail service being down delays a confirmation; it doesn't undo a payment.
It also means an email step shouldn't refuse the save. When we started this article, the sample
did: if the email failed, it called throwValidationError. But the order was already saved. So the
person saving got an error about a save that had actually worked. Their form still showed Paying
with no reference, and pressing Save again said Somebody else changed this record, which wasn't
true either.
So the sample now records what happened on the order instead, as above. The rule for your own scripts: in a PostOperation step, record a problem; don't refuse. A refusal can't undo anything there, it can only confuse. To stop a save, check before the write, in a PreOperation step, as the payment step in call an outside service does.
Where it works
Like callService, sendEmail is available in plugin steps, Custom APIs and scheduled
jobs. It isn't in form rules or command buttons, which run in the browser, or in process steps
yet.
Try it
Set the mail service to a webhook-testing address, save an order as Paying with your own address as the customer email, and look at what arrives. Then clear the variable and pay another one: the order still saves, and says why no email went.
Next: files from a script: attaching a file to a record, and writing one to storage.