Send Email (SendGrid)
Send an email from a rule through your own SendGrid account, such as the link a Human Intervention action creates for a reviewer.
The platform does not send email for you. Send Email (SendGrid) is a marketplace extension that sends it through a SendGrid account you own, so the mail comes from your own verified sender, under your own sending reputation and limits, and SendGrid's activity feed shows you every message. The extension calls SendGrid's v3 Mail Send API with your API key, which stays in your environment as a secret variable.
Set it up once
- In SendGrid, create an API key under Settings > API Keys. Choose Restricted Access and give it Mail Send access, nothing more. SendGrid shows the key once, so copy it now.
- Verify a sender under Settings > Sender Authentication: a single sender address, or your whole domain. The From address of every email has to be one of these, or SendGrid refuses the email.
- In your environment, add a secret variable, for example
SENDGRID_API_KEY, holding the key exactly as SendGrid shows it, starting withSG.and without aBearerprefix. See Variables & Context. - Add an Extension action, choose Send Email (SendGrid), and map its SendGrid API Key managed parameter to that variable.
Store the key in a secret variable and map it as the managed parameter. Never pass it as an input: inputs are context values, which a run's log can show. The key is sent only as SendGrid's Authorization header, and a run's log masks it wherever a line would quote it.
Inputs
| Input | What it takes |
|---|---|
| To | Required. One address, or several separated by commas, semicolons or line breaks. A list from the context works too. Each person sees the others in the To line. |
| From | Required. A sender you have verified in SendGrid. |
| From Name | Optional. The name shown beside the From address, such as “Acme Approvals”. |
| Reply To | Optional. Where replies go, when not to the From address. |
| Subject | Required unless a template sets it. |
| Text Body | Required unless you use a template. The plain-text email. |
| HTML Body | Optional. Sent with the text body, for mail apps that show HTML. |
| Template ID | Optional. A SendGrid dynamic template, starting d-, to send instead of a subject and body. |
| Template Data | Optional. A JSON object of the values the template uses. |
Bind any input to a constant, a context key or an expression, so a body can carry context values, such as 'Please review order ' + ctx.orderId + ':\n' + ctx.approvalUrl. Every input is sent as text exactly as it is: quotes, line breaks, markup and any language's characters arrive in the email unchanged, and nothing an input holds can change the request itself.
Click tracking is turned off for these emails, so a link in them is delivered as written rather than rewritten through SendGrid's tracking domain. That matters for a one-time approval link.
Outputs
The extension runs as a run of its own, so its outputs arrive in the action's Callback RuleSet, along with the context of the run that called it.
| Output | What it holds |
|---|---|
| Sent | True when SendGrid accepted the email. |
| SendGrid Message ID | SendGrid's X-Message-Id, to find the email in SendGrid's activity feed. |
| Status Code | The status SendGrid answered with: 202 when it accepted the email. Empty when SendGrid could not be reached. |
| Error | When the email was not sent, SendGrid's reason. Empty when it was. |
Accepted means SendGrid has queued the email for delivery, not that it reached the inbox. SendGrid's activity feed shows what happened to it next.
Emailing a Human Intervention link
A Human Intervention action writes the reviewer's link to a context key and hands off; the actions after it run straight away. So the email goes in the same rule, after it:
- A Human Intervention action, with Generated URL Context Key set to, say,
approvalUrl, and its continuation RuleSet holding the steps that wait for the answer. - An Extension action for Send Email (SendGrid), with To bound to the approver's address, and a Text Body expression that includes
ctx.approvalUrl. - Optionally, a Callback RuleSet that checks Sent and, when it is false, tells someone the request never went out.
The reviewer opens the link from the email, signs in if they need to, and answers. Their answer starts the continuation RuleSet. The link is for that reviewer alone: send it only to the person who should answer.
Sending a dynamic template
Set Template ID to a dynamic template from SendGrid's Email API > Dynamic Templates and leave the subject and bodies empty: the template supplies them. Pass its values as Template Data, a JSON object such as {"order": "42", "link": "https://..."}. A Create JSON action is the easiest way to build one from the context.
When SendGrid refuses an email
A refusal does not stop the extension's run. Its log shows a warning with SendGrid's status, then a line with SendGrid's own reason, for example SendGrid did not send the email (status 403): The from address does not match a verified Sender Identity. The same reason is the Error output, and Sent is false.
| Status | Usual cause |
|---|---|
| 400 | Something in the email itself: an address that is not valid, no subject or body without a template, or template data that is not a JSON object. |
| 401 | The key is wrong, revoked, or the variable holds something else. Check the variable the managed parameter is mapped to. |
| 403 | The From address is not a verified sender, or the key does not have Mail Send access. |
| 413 | The email is too large. |
| 429, 500, 502, 503 or 504 | SendGrid is busy or unavailable. The call is tried twice more, a few seconds apart, before it is reported. |
Testing a RuleSet that sends email
In a test case, the Extension action is intercepted like every action with a side effect, so no email is sent. Add an action mock named after it to give the rest of the RuleSet the outputs it expects.