@softr/sdk 0.0.99-alpha.0 → 0.0.99-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,147 +0,0 @@
1
- ---
2
- name: email
3
- description: >-
4
- Send an email from the app itself, with nothing to connect and no workflow:
5
- defineEmailAction from @softr/sdk declares who may send it and the fields it
6
- fixes (to, cc, bcc, replyTo, and an optional senderId); email.send from
7
- @softr/sdk/server sends one with a subject and a text or HTML body, and throws
8
- an EmailError with a code when Softr does not send it. Load this for a contact
9
- form that emails the owner, a confirmation to the signed-in user, staff
10
- emailing a client, or any email the app sends while handling what a person
11
- does in it.
12
- metadata:
13
- type: sub-skill
14
- library: softr-sdk
15
- library_version: 0.0.99-alpha.0
16
- ---
17
-
18
- **Boundary.** `email` and `EmailError` live on `@softr/sdk/server` and run only inside a `createServerFn` handler — importing them into a component fails the build. `defineEmailAction` and every type come from `@softr/sdk` and can be imported anywhere.
19
-
20
- # Email
21
-
22
- An app sends an email through an email action. Softr sends it and bills the workspace's **workflow credits**, so there is no mail service to connect and no workflow to build. Use it for every email the app sends while it handles something a person does in it: a form submitted, a button pressed, a record saved through the app.
23
-
24
- A workflow is still the answer for an email on a schedule ("every Monday") or on a change made outside the app (a record edited in the table): load `automate-with-workflows` for those. A connected Gmail or Outlook is the answer only when the user wants the email to come from their own mailbox, through the `integrations` sub-skill.
25
-
26
- `defineEmailAction` missing from `@softr/sdk`? The app was built on an older version. Upgrade `@softr/sdk` to the latest version first, then write the action.
27
-
28
- ## Who can send it, and to whom
29
-
30
- The action's `permissions` is its audience: everyone in it can send an email through it, directly, without the app's own code. The app's code is not a guard. Only what the declaration fixes holds whatever the caller sends.
31
-
32
- - **Fix the recipients whenever the use case allows.** A contact form that emails the owner fixes `to` to the owner. "Email me this report" fixes `to` to `{LOGGED_IN_USER:EMAIL}`. Then nobody can send the app's email to anyone else.
33
- - **When people pick the recipients, narrow the audience.** "Staff email a client" leaves `to` open and keeps the action to the staff group with `customGroups`. An action with open recipients for all signed-in users lets anyone who signs up send any email to anyone from the app's address, up to the workspace's credits. Build that only when the user asks for it, and tell them so in one line. Such an action also needs a sender of the workspace's own (see below). An action open to visitors must fix `to`: one that fixes only `cc` or `bcc` is not stored.
34
-
35
- ## The sender
36
-
37
- An email comes from the first of these that is set:
38
-
39
- 1. the action's `senderId`;
40
- 2. the app's email sender, which also sends its sign-in emails (Users → Notifications in Studio);
41
- 3. Softr's testing address, at most 30 emails a day per workspace. The next one fails with `DAILY_LIMIT_REACHED`.
42
-
43
- A chosen sender that is missing or not confirmed fails every send with `SENDER_UNAVAILABLE`: Softr never falls back. An action that leaves `to` open needs 1 or 2, and fails the same way from Softr's address.
44
-
45
- **Look before you write the action.** Call `application_get` for the app's sender and `workspace_list_email_senders` for the workspace's. Everything you say about the sender comes from those two answers.
46
-
47
- **Ask, never pick.**
48
-
49
- - The app has its own sender: the emails come from it. Say so.
50
- - Otherwise, with confirmed senders in the workspace: name them and ask whether the emails should come from one, and if so for **the whole app** (sign-in emails too: `application_configure_email_sender` with the email only) or **only these emails** (the action's `senderId`, copied exactly; nothing else changes).
51
- - With none: the emails come from Softr's testing address. Offer to send from the user's own address. Custom senders need the Professional or Business plan.
52
-
53
- **Adding a new sender.** `application_configure_email_sender` with an email and a name adds one, and also makes it the app's sender.
54
-
55
- - For the whole app, call it once the user approves. Softr emails a confirmation link to that address; ask the user to click it, then check it with `workspace_list_email_senders`. Until then the app keeps its current sender. Give the user the DKIM and Return-Path records it returns: without them the emails are more likely to land in spam.
56
- - For only these emails, the user adds and confirms it in Studio: Users → Notifications → Email sender → Manage email senders, leaving the app's sender as it is. Then set it as the action's `senderId`.
57
-
58
- **In your closing message**, say where the emails come from: the sender's address, or "Softr's testing address, up to 30 emails a day". Never its raw address.
59
-
60
- ## What it costs
61
-
62
- Every email costs **5 workflow credits per recipient**, from the workspace's monthly workflow credits, which its workflows spend too. You do not know the workspace's plan, so never name one or count how many emails it covers. The plan and the closing message give the cost and where to watch it, for example: "Each submission emails you, at 5 workflow credits per email. Track them in Settings → Billing → Workflow usage, where this app has its own row."
63
-
64
- Emails sent from the preview are real and charged, and the actions in the code reach the live app as soon as they are saved. Send at most one test email, and only when the user asks for one. Never send emails in a loop to try something out.
65
-
66
- ## Declare the action
67
-
68
- ```ts
69
- const notifyOwner = defineEmailAction({
70
- id: 'notifyOwner',
71
- to: ['owner@acme.com'],
72
- permissions: { userGroupTypes: ['NON_LOGGED_IN_USERS', 'LOGGED_IN_USERS'] },
73
- })
74
-
75
- const emailClient = defineEmailAction({
76
- id: 'emailClient',
77
- replyTo: '{LOGGED_IN_USER:EMAIL}',
78
- permissions: { userGroupTypes: ['LOGGED_IN_USERS'], customGroups: ['<the staff group id>'] },
79
- })
80
- ```
81
-
82
- - `id` is a name you choose, unique across the app.
83
- - `to`, `cc`, `bcc`: arrays of addresses. Each one declared here replaces the call's, and declaring `to` also ignores the call's `cc` and `bcc`. An element may be a whole placeholder for the signed-in sender: `{LOGGED_IN_USER:EMAIL}`, or `{LOGGED_IN_USER:<field>}` for an email field of their user record. Never part of a string, never in the call.
84
- - `replyTo`: an address or one such placeholder.
85
- - `senderId`: one of the workspace's senders, as above. The call can never set the sender.
86
- - `permissions`: who may send it, as for queries and mutations.
87
-
88
- Write the object inline in `defineEmailAction`, every value a literal. A declaration that cannot be read is not stored, and the check reports it.
89
-
90
- Declare one action per kind of email: the owner's notification and the client's confirmation are two actions, each with its own recipients and audience.
91
-
92
- ## Core pattern
93
-
94
- ```ts
95
- const submit = createServerFn({ method: 'POST' })
96
- .validator(contactSchema)
97
- .handler(async ({ data }) => {
98
- const saved = await ds.create(saveSubmission, { data })
99
- try {
100
- await email.send(notifyOwner, {
101
- subject: `New message from ${data.name}`,
102
- body: renderSubmission(data), // the app's own HTML, with what the visitor typed escaped
103
- replyTo: data.email,
104
- })
105
- } catch (error) {
106
- if (error instanceof EmailError) return { saved: true, emailed: false, reason: error.message }
107
- throw error
108
- }
109
- return { saved: true, emailed: true }
110
- })
111
- ```
112
-
113
- `email.send(action, init)` takes:
114
-
115
- - `subject` and `body`. Required. `body` is plain text or HTML. Escape what visitors typed before you put it into HTML.
116
- - `to`, `cc`, `bcc`, `replyTo`: each used only where the action does not fix it, and `cc` and `bcc` never when it fixes `to`. Each entry is one address. At most 50 recipients per email.
117
-
118
- It answers `{ messageId }` once the email is sent. The answer never lists the recipients. An email carries no attachments: link to the file instead.
119
-
120
- Every call is a new email. Disable the button while the request runs, so a double click does not send twice.
121
-
122
- ## Failure modes
123
-
124
- **Not handling `EmailError`.** `email.send` throws an `EmailError` from `@softr/sdk/server`, with `status`, `code` and `message` when the email was not sent, and never retries by itself. Catch it, keep what the request already saved, and show the person a line they can act on: "Saved, but the email could not be sent." The codes:
125
-
126
- | `code` | Meaning |
127
- | --- | --- |
128
- | `NOT_FOUND` | No such action, or the caller is not in its audience |
129
- | `INVALID_INPUT` | No subject or body, an entry or placeholder that is not exactly one address, or more than 50 recipients |
130
- | `PLAN_RESTRICTED` | The workspace's plan does not allow this email |
131
- | `CREDITS_EXHAUSTED` | The workspace has no workflow credits left |
132
- | `RATE_LIMITED` | The app sent too often lately. Tell the person to try again later |
133
- | `DAILY_LIMIT_REACHED` | Softr's own address sent its 30 emails for today. Tell the person the email could not be sent today; the owner fixes it by choosing their own sender |
134
- | `SENDER_UNAVAILABLE` | The chosen sender is missing or not confirmed, or the action leaves `to` open and has no sender of the workspace's own. Tell the person the email could not be sent; the owner fixes the sender |
135
- | `PROVIDER_REJECTED` | The mail provider refused the email |
136
- | `UNAVAILABLE` | Softr could not send it right now or could not be reached |
137
- | `TIMEOUT` | No answer in time. It may still have been sent, so never send it again by yourself |
138
-
139
- **Letting a mistyped address reach Softr.** An address the person typed (the reply-to of a contact form, a client's email) that is not one address is refused with `INVALID_INPUT`, which reads as a vague failure. Check its format in the validator and tell them which field is wrong.
140
-
141
- **Calling from client code.** `email` is server-only. Wrap the send in a `createServerFn` and call that from the form or the button.
142
-
143
- **Taking recipients from the request when they could be fixed.** A `to` the browser sends is a `to` any caller can change. Fix it in the declaration, or read it on the server from the record the email is about.
144
-
145
- **Placeholders in the call.** `{LOGGED_IN_USER:EMAIL}` is filled only in the declaration. In the call it is sent as written and fails.
146
-
147
- **Building a workflow for an email the app sends itself.** A "Run custom workflow" trigger plus a "Send email" step does the same at more cost to the user: a workflow to keep published. Use the email action; keep workflows for schedules and table events.