@lovable.dev/email-js 0.2.0 → 0.3.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.
package/README.md CHANGED
@@ -44,7 +44,7 @@ export async function handleWebhook(req: Request) {
44
44
  {
45
45
  run_id: payload.run_id,
46
46
  to: payload.data?.email ?? "",
47
- from: "My App <noreply@example.com>",
47
+ from: { name: "My App", address: "noreply@example.com" },
48
48
  subject: "Welcome",
49
49
  html: "<p>Hello</p>",
50
50
  text: "Hello",
@@ -55,6 +55,15 @@ export async function handleWebhook(req: Request) {
55
55
  }
56
56
  ```
57
57
 
58
+ ### The `from` field
59
+
60
+ `from` takes either a raw RFC 5322 header string
61
+ (`"My App <noreply@example.com>"`) or a parts object
62
+ (`{ name: "My App", address: "noreply@example.com" }`). Prefer the parts
63
+ form: the SDK (`formatEmailFrom`, also exported) quotes and escapes the
64
+ display name, so brand names containing `,` `:` `;` or non-ASCII characters
65
+ cannot produce an unparseable header the send API would reject.
66
+
58
67
  ### Sending without a `run_id` (runtime-triggered emails)
59
68
 
60
69
  For transactional emails sent outside a build context (e.g., webhook handlers,
@@ -194,7 +203,8 @@ import { createAuthEmailHandler } from "@lovable.dev/email-js";
194
203
 
195
204
  const handler = createAuthEmailHandler({
196
205
  apiKey: process.env.LOVABLE_API_KEY!,
197
- from: "My App <noreply@notify.example.com>", // sender domain derived from the address
206
+ from: { name: "My App", address: "noreply@notify.example.com" }, // sender domain derived from the address
207
+ replyTo: { name: "My App Support", address: "support@example.com" },
198
208
  sendUrl: process.env.LOVABLE_SEND_URL,
199
209
  emails: {
200
210
  signup: { subject: "Confirm your email", render: (data) => <SignupEmail confirmationUrl={data.url} /> },
@@ -207,6 +217,10 @@ const handler = createAuthEmailHandler({
207
217
  });
208
218
  ```
209
219
 
220
+ `replyTo` takes the same string-or-parts form as `from` and sets the Reply-To
221
+ header, so a user who replies to a confirmation email reaches a monitored
222
+ inbox instead of the noreply sender. Omit it and replies go to `from`.
223
+
210
224
  `render` may be async — look up personalization data first, and fall back
211
225
  (`?? "friend"`) instead of throwing when it's missing: auth emails are
212
226
  load-bearing. A per-type entry can also be a single async function returning
package/dist/index.cjs CHANGED
@@ -63,6 +63,24 @@ async function emailApiRequest(options, path, init = { method: "GET" }) {
63
63
  //#endregion
64
64
  //#region src/email.ts
65
65
  const DEFAULT_SEND_PATH = "/v1/messaging/email/send";
66
+ const UNQUOTED_NAME_SAFE = /^[A-Za-z0-9 !#$%&'*+\-/^_`{|}~]*$/;
67
+ const ADDRESS_SHAPE = /^[^\s@<>"(),;:\\[\].]+(\.[^\s@<>"(),;:\\[\].]+)*@[^\s@<>"(),;:\\[\].]+(\.[^\s@<>"(),;:\\[\].]+)*$/;
68
+ const CONTROL_CHARS = /[\u0000-\u001F\u007F]/;
69
+ /**
70
+ * Builds an RFC 5322 From header from parts, quoting and escaping the display
71
+ * name so punctuation like ":" cannot yield an unparseable header. Strings pass through.
72
+ */
73
+ function formatEmailFrom(from) {
74
+ if (typeof from !== "object" || from === null) return from;
75
+ const name = (from.name ?? "").trim();
76
+ const address = (from.address ?? "").trim();
77
+ if (CONTROL_CHARS.test(name + address)) throw new Error("from name and address must not contain control characters");
78
+ if (!ADDRESS_SHAPE.test(address)) throw new Error(`Invalid from address: "${address}"`);
79
+ if (new TextEncoder().encode(name).length > 200) throw new Error("from name must be at most 200 bytes");
80
+ if (!name) return address;
81
+ if (UNQUOTED_NAME_SAFE.test(name)) return `${name} <${address}>`;
82
+ return `"${name.replace(/[\\"]/g, "\\$&")}" <${address}>`;
83
+ }
66
84
  async function sendLovableEmail(payload, options) {
67
85
  const apiKey = options.apiKey;
68
86
  if (!apiKey) throw new Error("Missing Lovable API key");
@@ -77,7 +95,10 @@ async function sendLovableEmail(payload, options) {
77
95
  const response = await fetch(url, {
78
96
  method: "POST",
79
97
  headers,
80
- body: JSON.stringify(payload)
98
+ body: JSON.stringify({
99
+ ...payload,
100
+ from: formatEmailFrom(payload.from)
101
+ })
81
102
  });
82
103
  await throwIfNotOk(response);
83
104
  return await response.json();
@@ -149,8 +170,10 @@ function domainOfAddress(from) {
149
170
  return /@([^@\s<>]+)>?\s*$/.exec(from.trim())?.[1];
150
171
  }
151
172
  function createAuthEmailHandler(options) {
152
- const { apiKey, from, emails } = options;
173
+ const { apiKey, emails } = options;
153
174
  if (!apiKey) throw new Error("Missing Lovable API key");
175
+ const from = formatEmailFrom(options.from);
176
+ const replyTo = options.replyTo === void 0 ? void 0 : formatEmailFrom(options.replyTo);
154
177
  const senderDomain = options.senderDomain ?? domainOfAddress(from);
155
178
  if (!senderDomain) throw new Error(`Cannot derive a sender domain from "${from}"; pass senderDomain`);
156
179
  return async (req) => {
@@ -188,6 +211,7 @@ function createAuthEmailHandler(options) {
188
211
  to: event.data.email,
189
212
  from,
190
213
  sender_domain: senderDomain,
214
+ reply_to: replyTo,
191
215
  subject,
192
216
  html,
193
217
  text,
@@ -252,6 +276,7 @@ async function setEmailUnsubscribe(request, options) {
252
276
  exports.EmailAPIError = EmailAPIError;
253
277
  exports.createAuthEmailHandler = createAuthEmailHandler;
254
278
  exports.createEmailWebhookHandler = createEmailWebhookHandler;
279
+ exports.formatEmailFrom = formatEmailFrom;
255
280
  exports.getEmailUnsubscribe = getEmailUnsubscribe;
256
281
  exports.listEmailLogs = listEmailLogs;
257
282
  exports.parseEmailWebhookPayload = parseEmailWebhookPayload;
package/dist/index.d.cts CHANGED
@@ -1,10 +1,16 @@
1
1
  import { ReactElement } from "react";
2
2
  import { EmailWebhookPayload, EmailWebhookPayload as EmailWebhookPayload$1 } from "@lovable.dev/webhooks-js";
3
3
  //#region src/types.d.ts
4
+ interface EmailFromParts {
5
+ name: string;
6
+ address: string;
7
+ }
8
+ /** Prefer the parts form: the SDK quotes/escapes the name, so any brand name yields a valid header. */
9
+ type EmailFrom = string | EmailFromParts;
4
10
  interface EmailSendRequest {
5
11
  run_id?: string;
6
12
  to: string;
7
- from: string;
13
+ from: EmailFrom;
8
14
  sender_domain?: string;
9
15
  subject: string;
10
16
  html: string;
@@ -128,9 +134,11 @@ type AuthEmailDefinition = {
128
134
  type AuthEmailDefinitions = Record<AuthEmailActionType, AuthEmailDefinition>;
129
135
  interface AuthEmailHandlerOptions {
130
136
  apiKey: string;
131
- from: string;
137
+ from: EmailFrom;
132
138
  /** Defaults to the domain of `from`. */
133
139
  senderDomain?: string;
140
+ /** Reply-To header. Replies go to `from` when omitted. */
141
+ replyTo?: EmailFrom;
134
142
  sendUrl?: string | undefined;
135
143
  emails: AuthEmailDefinitions;
136
144
  }
@@ -139,6 +147,11 @@ interface AuthEmailHandlerOptions {
139
147
  declare function createAuthEmailHandler(options: AuthEmailHandlerOptions): (req: Request) => Promise<Response>;
140
148
  //#endregion
141
149
  //#region src/email.d.ts
150
+ /**
151
+ * Builds an RFC 5322 From header from parts, quoting and escaping the display
152
+ * name so punctuation like ":" cannot yield an unparseable header. Strings pass through.
153
+ */
154
+ declare function formatEmailFrom(from: EmailFrom): string;
142
155
  declare function sendLovableEmail(payload: EmailSendRequest, options: SendEmailOptions): Promise<EmailSendResponse>;
143
156
  //#endregion
144
157
  //#region src/http.d.ts
@@ -163,4 +176,4 @@ declare function parseEmailWebhookPayload(body: string): EmailWebhookPayload$1;
163
176
  //#region src/webhook-handler.d.ts
164
177
  declare function createEmailWebhookHandler(options: EmailWebhookHandlerOptions): (req: Request) => Promise<Response>;
165
178
  //#endregion
166
- export { type AuthEmailActionType, type AuthEmailContent, type AuthEmailDefinition, type AuthEmailDefinitions, type AuthEmailHandlerOptions, type AuthEmailHookData, type AuthEmailWebhookPayload, EmailAPIError, type EmailClientOptions, type EmailEvent, type EmailLogsPagination, type EmailSendRequest, type EmailSendResponse, type EmailUnsubscribeResponse, type EmailWebhookEvent, type EmailWebhookEventType, type EmailWebhookHandlerOptions, type EmailWebhookHandlers, type EmailWebhookPayload, type GetEmailUnsubscribeRequest, type ListEmailLogsFilters, type ListEmailLogsResponse, type SendEmailOptions, type SetEmailUnsubscribeRequest, createAuthEmailHandler, createEmailWebhookHandler, getEmailUnsubscribe, listEmailLogs, parseEmailWebhookPayload, sendLovableEmail, setEmailUnsubscribe };
179
+ export { type AuthEmailActionType, type AuthEmailContent, type AuthEmailDefinition, type AuthEmailDefinitions, type AuthEmailHandlerOptions, type AuthEmailHookData, type AuthEmailWebhookPayload, EmailAPIError, type EmailClientOptions, type EmailEvent, type EmailFrom, type EmailFromParts, type EmailLogsPagination, type EmailSendRequest, type EmailSendResponse, type EmailUnsubscribeResponse, type EmailWebhookEvent, type EmailWebhookEventType, type EmailWebhookHandlerOptions, type EmailWebhookHandlers, type EmailWebhookPayload, type GetEmailUnsubscribeRequest, type ListEmailLogsFilters, type ListEmailLogsResponse, type SendEmailOptions, type SetEmailUnsubscribeRequest, createAuthEmailHandler, createEmailWebhookHandler, formatEmailFrom, getEmailUnsubscribe, listEmailLogs, parseEmailWebhookPayload, sendLovableEmail, setEmailUnsubscribe };
package/dist/index.d.ts CHANGED
@@ -1,10 +1,16 @@
1
1
  import { EmailWebhookPayload, EmailWebhookPayload as EmailWebhookPayload$1 } from "@lovable.dev/webhooks-js";
2
2
  import { ReactElement } from "react";
3
3
  //#region src/types.d.ts
4
+ interface EmailFromParts {
5
+ name: string;
6
+ address: string;
7
+ }
8
+ /** Prefer the parts form: the SDK quotes/escapes the name, so any brand name yields a valid header. */
9
+ type EmailFrom = string | EmailFromParts;
4
10
  interface EmailSendRequest {
5
11
  run_id?: string;
6
12
  to: string;
7
- from: string;
13
+ from: EmailFrom;
8
14
  sender_domain?: string;
9
15
  subject: string;
10
16
  html: string;
@@ -128,9 +134,11 @@ type AuthEmailDefinition = {
128
134
  type AuthEmailDefinitions = Record<AuthEmailActionType, AuthEmailDefinition>;
129
135
  interface AuthEmailHandlerOptions {
130
136
  apiKey: string;
131
- from: string;
137
+ from: EmailFrom;
132
138
  /** Defaults to the domain of `from`. */
133
139
  senderDomain?: string;
140
+ /** Reply-To header. Replies go to `from` when omitted. */
141
+ replyTo?: EmailFrom;
134
142
  sendUrl?: string | undefined;
135
143
  emails: AuthEmailDefinitions;
136
144
  }
@@ -139,6 +147,11 @@ interface AuthEmailHandlerOptions {
139
147
  declare function createAuthEmailHandler(options: AuthEmailHandlerOptions): (req: Request) => Promise<Response>;
140
148
  //#endregion
141
149
  //#region src/email.d.ts
150
+ /**
151
+ * Builds an RFC 5322 From header from parts, quoting and escaping the display
152
+ * name so punctuation like ":" cannot yield an unparseable header. Strings pass through.
153
+ */
154
+ declare function formatEmailFrom(from: EmailFrom): string;
142
155
  declare function sendLovableEmail(payload: EmailSendRequest, options: SendEmailOptions): Promise<EmailSendResponse>;
143
156
  //#endregion
144
157
  //#region src/http.d.ts
@@ -163,4 +176,4 @@ declare function parseEmailWebhookPayload(body: string): EmailWebhookPayload$1;
163
176
  //#region src/webhook-handler.d.ts
164
177
  declare function createEmailWebhookHandler(options: EmailWebhookHandlerOptions): (req: Request) => Promise<Response>;
165
178
  //#endregion
166
- export { type AuthEmailActionType, type AuthEmailContent, type AuthEmailDefinition, type AuthEmailDefinitions, type AuthEmailHandlerOptions, type AuthEmailHookData, type AuthEmailWebhookPayload, EmailAPIError, type EmailClientOptions, type EmailEvent, type EmailLogsPagination, type EmailSendRequest, type EmailSendResponse, type EmailUnsubscribeResponse, type EmailWebhookEvent, type EmailWebhookEventType, type EmailWebhookHandlerOptions, type EmailWebhookHandlers, type EmailWebhookPayload, type GetEmailUnsubscribeRequest, type ListEmailLogsFilters, type ListEmailLogsResponse, type SendEmailOptions, type SetEmailUnsubscribeRequest, createAuthEmailHandler, createEmailWebhookHandler, getEmailUnsubscribe, listEmailLogs, parseEmailWebhookPayload, sendLovableEmail, setEmailUnsubscribe };
179
+ export { type AuthEmailActionType, type AuthEmailContent, type AuthEmailDefinition, type AuthEmailDefinitions, type AuthEmailHandlerOptions, type AuthEmailHookData, type AuthEmailWebhookPayload, EmailAPIError, type EmailClientOptions, type EmailEvent, type EmailFrom, type EmailFromParts, type EmailLogsPagination, type EmailSendRequest, type EmailSendResponse, type EmailUnsubscribeResponse, type EmailWebhookEvent, type EmailWebhookEventType, type EmailWebhookHandlerOptions, type EmailWebhookHandlers, type EmailWebhookPayload, type GetEmailUnsubscribeRequest, type ListEmailLogsFilters, type ListEmailLogsResponse, type SendEmailOptions, type SetEmailUnsubscribeRequest, createAuthEmailHandler, createEmailWebhookHandler, formatEmailFrom, getEmailUnsubscribe, listEmailLogs, parseEmailWebhookPayload, sendLovableEmail, setEmailUnsubscribe };
package/dist/index.js CHANGED
@@ -62,6 +62,24 @@ async function emailApiRequest(options, path, init = { method: "GET" }) {
62
62
  //#endregion
63
63
  //#region src/email.ts
64
64
  const DEFAULT_SEND_PATH = "/v1/messaging/email/send";
65
+ const UNQUOTED_NAME_SAFE = /^[A-Za-z0-9 !#$%&'*+\-/^_`{|}~]*$/;
66
+ const ADDRESS_SHAPE = /^[^\s@<>"(),;:\\[\].]+(\.[^\s@<>"(),;:\\[\].]+)*@[^\s@<>"(),;:\\[\].]+(\.[^\s@<>"(),;:\\[\].]+)*$/;
67
+ const CONTROL_CHARS = /[\u0000-\u001F\u007F]/;
68
+ /**
69
+ * Builds an RFC 5322 From header from parts, quoting and escaping the display
70
+ * name so punctuation like ":" cannot yield an unparseable header. Strings pass through.
71
+ */
72
+ function formatEmailFrom(from) {
73
+ if (typeof from !== "object" || from === null) return from;
74
+ const name = (from.name ?? "").trim();
75
+ const address = (from.address ?? "").trim();
76
+ if (CONTROL_CHARS.test(name + address)) throw new Error("from name and address must not contain control characters");
77
+ if (!ADDRESS_SHAPE.test(address)) throw new Error(`Invalid from address: "${address}"`);
78
+ if (new TextEncoder().encode(name).length > 200) throw new Error("from name must be at most 200 bytes");
79
+ if (!name) return address;
80
+ if (UNQUOTED_NAME_SAFE.test(name)) return `${name} <${address}>`;
81
+ return `"${name.replace(/[\\"]/g, "\\$&")}" <${address}>`;
82
+ }
65
83
  async function sendLovableEmail(payload, options) {
66
84
  const apiKey = options.apiKey;
67
85
  if (!apiKey) throw new Error("Missing Lovable API key");
@@ -76,7 +94,10 @@ async function sendLovableEmail(payload, options) {
76
94
  const response = await fetch(url, {
77
95
  method: "POST",
78
96
  headers,
79
- body: JSON.stringify(payload)
97
+ body: JSON.stringify({
98
+ ...payload,
99
+ from: formatEmailFrom(payload.from)
100
+ })
80
101
  });
81
102
  await throwIfNotOk(response);
82
103
  return await response.json();
@@ -148,8 +169,10 @@ function domainOfAddress(from) {
148
169
  return /@([^@\s<>]+)>?\s*$/.exec(from.trim())?.[1];
149
170
  }
150
171
  function createAuthEmailHandler(options) {
151
- const { apiKey, from, emails } = options;
172
+ const { apiKey, emails } = options;
152
173
  if (!apiKey) throw new Error("Missing Lovable API key");
174
+ const from = formatEmailFrom(options.from);
175
+ const replyTo = options.replyTo === void 0 ? void 0 : formatEmailFrom(options.replyTo);
153
176
  const senderDomain = options.senderDomain ?? domainOfAddress(from);
154
177
  if (!senderDomain) throw new Error(`Cannot derive a sender domain from "${from}"; pass senderDomain`);
155
178
  return async (req) => {
@@ -187,6 +210,7 @@ function createAuthEmailHandler(options) {
187
210
  to: event.data.email,
188
211
  from,
189
212
  sender_domain: senderDomain,
213
+ reply_to: replyTo,
190
214
  subject,
191
215
  html,
192
216
  text,
@@ -248,4 +272,4 @@ async function setEmailUnsubscribe(request, options) {
248
272
  });
249
273
  }
250
274
  //#endregion
251
- export { EmailAPIError, createAuthEmailHandler, createEmailWebhookHandler, getEmailUnsubscribe, listEmailLogs, parseEmailWebhookPayload, sendLovableEmail, setEmailUnsubscribe };
275
+ export { EmailAPIError, createAuthEmailHandler, createEmailWebhookHandler, formatEmailFrom, getEmailUnsubscribe, listEmailLogs, parseEmailWebhookPayload, sendLovableEmail, setEmailUnsubscribe };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lovable.dev/email-js",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "description": "Lovable Email JS",
5
5
  "repository": {
6
6
  "type": "git",
@@ -21,7 +21,7 @@
21
21
  ],
22
22
  "license": "MIT",
23
23
  "dependencies": {
24
- "@lovable.dev/webhooks-js": "0.0.1"
24
+ "@lovable.dev/webhooks-js": "0.0.3"
25
25
  },
26
26
  "peerDependencies": {
27
27
  "@react-email/render": ">=1.0.0",
@@ -55,8 +55,8 @@
55
55
  "build": "tsdown",
56
56
  "check:package": "tmp=$(mktemp -d) && pnpm pack --out \"$tmp/pkg.tgz\" && tar -xzf \"$tmp/pkg.tgz\" -C \"$tmp\" && publint \"$tmp/package\" --strict && attw \"$tmp/pkg.tgz\"",
57
57
  "dev": "tsdown --watch",
58
- "test": "vitest run",
58
+ "test": "pnpm --filter @lovable.dev/webhooks-js run build && vitest run",
59
59
  "clean": "rimraf dist",
60
- "typecheck": "tsgo --noEmit"
60
+ "typecheck": "pnpm --filter @lovable.dev/webhooks-js run build && tsc --noEmit"
61
61
  }
62
62
  }