@molecule/api-emails-mailgun 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,115 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work.
38
+
39
+ "Derivative Works" shall mean any work, whether in Source or Object
40
+ form, that is based on (or derived from) the Work and for which the
41
+ editorial revisions, annotations, elaborations, or other modifications
42
+ represent, as a whole, an original work of authorship.
43
+
44
+ "Contribution" shall mean any work of authorship, including the
45
+ original version of the Work and any modifications or additions
46
+ to that Work, that is intentionally submitted to the Licensor for
47
+ inclusion in the Work by the copyright owner or by an individual or
48
+ Legal Entity authorized to submit on behalf of the copyright owner.
49
+
50
+ "Contributor" shall mean Licensor and any individual or Legal Entity
51
+ on behalf of whom a Contribution has been received by the Licensor and
52
+ subsequently incorporated within the Work.
53
+
54
+ 2. Grant of Copyright License. Subject to the terms and conditions of
55
+ this License, each Contributor hereby grants to You a perpetual,
56
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
57
+ copyright license to reproduce, prepare Derivative Works of,
58
+ publicly display, publicly perform, sublicense, and distribute the
59
+ Work and such Derivative Works in Source or Object form.
60
+
61
+ 3. Grant of Patent License. Subject to the terms and conditions of
62
+ this License, each Contributor hereby grants to You a perpetual,
63
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
64
+ patent license to make, have made, use, offer to sell, sell, import,
65
+ and otherwise transfer the Work.
66
+
67
+ 4. Redistribution. You may reproduce and distribute copies of the
68
+ Work or Derivative Works thereof in any medium, with or without
69
+ modifications, and in Source or Object form, provided that You
70
+ meet the following conditions:
71
+
72
+ (a) You must give any other recipients of the Work or
73
+ Derivative Works a copy of this License; and
74
+
75
+ (b) You must cause any modified files to carry prominent notices
76
+ stating that You changed the files; and
77
+
78
+ (c) You must retain, in the Source form of any Derivative Works
79
+ that You distribute, all copyright, patent, trademark, and
80
+ attribution notices from the Source form of the Work,
81
+ excluding those notices that do not pertain to any part of
82
+ the Derivative Works; and
83
+
84
+ (d) If the Work includes a "NOTICE" text file as part of its
85
+ distribution, then any Derivative Works that You distribute must
86
+ include a readable copy of the attribution notices contained
87
+ within such NOTICE file.
88
+
89
+ 5. Submission of Contributions.
90
+
91
+ 6. Trademarks. This License does not grant permission to use the trade
92
+ names, trademarks, service marks, or product names of the Licensor.
93
+
94
+ 7. Disclaimer of Warranty. Unless required by applicable law or
95
+ agreed to in writing, Licensor provides the Work on an "AS IS" BASIS,
96
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
97
+
98
+ 8. Limitation of Liability. In no event and under no legal theory shall
99
+ any Contributor be liable to You for damages.
100
+
101
+ 9. Accepting Warranty or Additional Liability.
102
+
103
+ Copyright 2026 Molecule Dev, Inc.
104
+
105
+ Licensed under the Apache License, Version 2.0 (the "License");
106
+ you may not use this file except in compliance with the License.
107
+ You may obtain a copy of the License at
108
+
109
+ http://www.apache.org/licenses/LICENSE-2.0
110
+
111
+ Unless required by applicable law or agreed to in writing, software
112
+ distributed under the License is distributed on an "AS IS" BASIS,
113
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
114
+ See the License for the specific language governing permissions and
115
+ limitations under the License.
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=browser-guard.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browser-guard.d.ts","sourceRoot":"","sources":["../src/browser-guard.ts"],"names":[],"mappings":"AAuBA,OAAO,EAAE,CAAA"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Browser guard — `@molecule/api-emails-mailgun` is SERVER-ONLY.
3
+ *
4
+ * Generated by scripts/gen-browser-guards.mjs (workspace root) — edit THAT, not this.
5
+ * Evaluating a server package in a browser bundle is always an import-graph mistake
6
+ * (node APIs, secrets); without this guard it surfaces as a cryptic downstream crash
7
+ * ("Buffer is not defined") far from the culprit. Throwing here names the package and
8
+ * the fix at the exact moment the client bundle evaluates it. jsdom tests and SSR are
9
+ * unaffected: the throw requires browser globals AND the absence of a node runtime.
10
+ */
11
+ const g = globalThis;
12
+ if (g.window !== undefined && g.document !== undefined && !g.process?.versions?.node) {
13
+ throw new Error('@molecule/api-emails-mailgun is SERVER-ONLY: it was bundled into browser/client code. Import it only ' +
14
+ 'from server code (a server route/function or your API), or dynamic-import it inside ' +
15
+ 'the server handler — never from components or shared client modules, and never ' +
16
+ 'polyfill Buffer/process to silence this.');
17
+ }
18
+ export {};
19
+ //# sourceMappingURL=browser-guard.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browser-guard.js","sourceRoot":"","sources":["../src/browser-guard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,CAAC,GAAG,UAIT,CAAA;AACD,IAAI,CAAC,CAAC,MAAM,KAAK,SAAS,IAAI,CAAC,CAAC,QAAQ,KAAK,SAAS,IAAI,CAAC,CAAC,CAAC,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;IACrF,MAAM,IAAI,KAAK,CACb,uGAAuG;QACrG,sFAAsF;QACtF,iFAAiF;QACjF,0CAA0C,CAC7C,CAAA;AACH,CAAC"}
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Mailgun email provider for molecule.dev.
3
+ *
4
+ * @see https://www.npmjs.com/package/nodemailer
5
+ * @see https://www.npmjs.com/package/nodemailer-mailgun-transport
6
+ *
7
+ * @remarks
8
+ * - **EU-region Mailgun accounts must set `MAILGUN_API_HOST=api.eu.mailgun.net`**
9
+ * (optional env; defaults to Mailgun's US endpoint). Without it every send
10
+ * fails upstream with 401 even though the key is valid — wrong region, not
11
+ * wrong key.
12
+ * - **Sandbox domains auto-enable Mailgun test mode**: when `MAILGUN_DOMAIN`
13
+ * matches `sandbox*.mailgun.org` (or `MAILGUN_TEST_MODE=true`), sends carry
14
+ * `o:testmode=yes` — Mailgun accepts, validates, and assigns a message id
15
+ * but NEVER delivers. A sandbox 403 for an unauthorized recipient is
16
+ * reported as a synthetic success (`response: 'sandbox-test-mode'`). "Send
17
+ * succeeded but no email arrived" in dev is this behavior, not a bug.
18
+ * - Credentials are read lazily on first send and fail fast with a tagged
19
+ * `config.notConfigured` error naming the missing key (`MAILGUN_API_KEY`,
20
+ * then `MAILGUN_DOMAIN`). On success `accepted` echoes the message's own
21
+ * recipients (Mailgun's transport returns no per-recipient verdict).
22
+ * - **The `from` address's domain must equal `MAILGUN_DOMAIN`** (Mailgun sends
23
+ * through, and signs SPF/DKIM for, that verified domain). A `from` on any other
24
+ * domain — a hardcoded `noreply@example.com`, `noreply@store.com`, etc. — is
25
+ * rejected or unsigned (spam). Default the sender to the sending domain:
26
+ * `` process.env.EMAIL_FROM ?? `no-reply@${process.env.MAILGUN_DOMAIN}` `` — never
27
+ * a literal placeholder domain.
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * import { setTransport } from '@molecule/api-emails'
32
+ * import { provider } from '@molecule/api-emails-mailgun'
33
+ *
34
+ * setTransport(provider)
35
+ * ```
36
+ *
37
+ * @module
38
+ */
39
+ export * from './browser-guard.js';
40
+ export * from './provider.js';
41
+ export * from './secrets.js';
42
+ export * from './types.js';
43
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAEH,cAAc,oBAAoB,CAAA;AAClC,cAAc,eAAe,CAAA;AAC7B,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Mailgun email provider for molecule.dev.
3
+ *
4
+ * @see https://www.npmjs.com/package/nodemailer
5
+ * @see https://www.npmjs.com/package/nodemailer-mailgun-transport
6
+ *
7
+ * @remarks
8
+ * - **EU-region Mailgun accounts must set `MAILGUN_API_HOST=api.eu.mailgun.net`**
9
+ * (optional env; defaults to Mailgun's US endpoint). Without it every send
10
+ * fails upstream with 401 even though the key is valid — wrong region, not
11
+ * wrong key.
12
+ * - **Sandbox domains auto-enable Mailgun test mode**: when `MAILGUN_DOMAIN`
13
+ * matches `sandbox*.mailgun.org` (or `MAILGUN_TEST_MODE=true`), sends carry
14
+ * `o:testmode=yes` — Mailgun accepts, validates, and assigns a message id
15
+ * but NEVER delivers. A sandbox 403 for an unauthorized recipient is
16
+ * reported as a synthetic success (`response: 'sandbox-test-mode'`). "Send
17
+ * succeeded but no email arrived" in dev is this behavior, not a bug.
18
+ * - Credentials are read lazily on first send and fail fast with a tagged
19
+ * `config.notConfigured` error naming the missing key (`MAILGUN_API_KEY`,
20
+ * then `MAILGUN_DOMAIN`). On success `accepted` echoes the message's own
21
+ * recipients (Mailgun's transport returns no per-recipient verdict).
22
+ * - **The `from` address's domain must equal `MAILGUN_DOMAIN`** (Mailgun sends
23
+ * through, and signs SPF/DKIM for, that verified domain). A `from` on any other
24
+ * domain — a hardcoded `noreply@example.com`, `noreply@store.com`, etc. — is
25
+ * rejected or unsigned (spam). Default the sender to the sending domain:
26
+ * `` process.env.EMAIL_FROM ?? `no-reply@${process.env.MAILGUN_DOMAIN}` `` — never
27
+ * a literal placeholder domain.
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * import { setTransport } from '@molecule/api-emails'
32
+ * import { provider } from '@molecule/api-emails-mailgun'
33
+ *
34
+ * setTransport(provider)
35
+ * ```
36
+ *
37
+ * @module
38
+ */
39
+ export * from './browser-guard.js';
40
+ export * from './provider.js';
41
+ export * from './secrets.js';
42
+ export * from './types.js';
43
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAEH,cAAc,oBAAoB,CAAA;AAClC,cAAc,eAAe,CAAA;AAC7B,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA"}
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Mailgun email provider implementation.
3
+ *
4
+ * @see https://www.npmjs.com/package/nodemailer
5
+ * @see https://www.npmjs.com/package/nodemailer-mailgun-transport
6
+ *
7
+ * @module
8
+ */
9
+ import './secrets.js';
10
+ import nodemailer from 'nodemailer';
11
+ import type { EmailMessage, EmailSendResult, EmailTransport } from '@molecule/api-emails';
12
+ /**
13
+ * Sends an email message via the Mailgun API, with automatic test-mode handling for sandbox domains.
14
+ *
15
+ * @param message - The email message (to, from, subject, text/html, attachments).
16
+ * @returns Send result with accepted/rejected addresses and message ID.
17
+ */
18
+ export declare const sendMail: (message: EmailMessage) => Promise<EmailSendResult>;
19
+ /**
20
+ * The Mailgun email provider implementing the `EmailTransport` interface.
21
+ */
22
+ export declare const provider: EmailTransport;
23
+ /**
24
+ * Raw nodemailer transport for direct access.
25
+ * @deprecated Use `sendMail()` or `provider` instead.
26
+ */
27
+ export declare const transport: {
28
+ sendMail: (msg: nodemailer.SendMailOptions) => Promise<any>;
29
+ };
30
+ /**
31
+ * Raw nodemailer transport alias.
32
+ * @deprecated Use `sendMail()` or `provider` instead.
33
+ */
34
+ export declare const email: {
35
+ sendMail: (msg: nodemailer.SendMailOptions) => Promise<any>;
36
+ };
37
+ //# sourceMappingURL=provider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAKH,OAAO,cAAc,CAAA;AACrB,OAAO,UAAU,MAAM,YAAY,CAAA;AAInC,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AA+GzF;;;;;GAKG;AACH,eAAO,MAAM,QAAQ,GAAU,SAAS,YAAY,KAAG,OAAO,CAAC,eAAe,CA+D7E,CAAA;AAED;;GAEG;AACH,eAAO,MAAM,QAAQ,EAAE,cAEtB,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,SAAS;oBACJ,UAAU,CAAC,eAAe;CAC3C,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,KAAK;oBAPA,UAAU,CAAC,eAAe;CAOd,CAAA"}
@@ -0,0 +1,197 @@
1
+ /**
2
+ * Mailgun email provider implementation.
3
+ *
4
+ * @see https://www.npmjs.com/package/nodemailer
5
+ * @see https://www.npmjs.com/package/nodemailer-mailgun-transport
6
+ *
7
+ * @module
8
+ */
9
+ // Side-effect import: registers this bond's secret definitions so the
10
+ // runtime registry is populated even when provider.js is imported directly
11
+ // (not through the package barrel).
12
+ import './secrets.js';
13
+ import nodemailer from 'nodemailer';
14
+ import mailgun from 'nodemailer-mailgun-transport';
15
+ import { getLogger } from '@molecule/api-bond';
16
+ import { configNotConfiguredError } from '@molecule/api-secrets';
17
+ const logger = getLogger();
18
+ /**
19
+ * The underlying nodemailer transport.
20
+ *
21
+ * @see https://www.npmjs.com/package/nodemailer
22
+ * @see https://www.npmjs.com/package/nodemailer-mailgun-transport
23
+ */
24
+ let _transport = null;
25
+ /**
26
+ * Returns the lazily-initialized nodemailer transport configured with Mailgun credentials.
27
+ * @returns The nodemailer `Transporter` instance.
28
+ */
29
+ function getTransport() {
30
+ if (!_transport) {
31
+ const apiKey = process.env.MAILGUN_API_KEY;
32
+ if (!apiKey) {
33
+ // Tagged config-missing error → the API middleware returns a clean 503 +
34
+ // 'config.notConfigured', and the message carries the registered
35
+ // definition's description + setup URL (see classifyTaggedError).
36
+ throw configNotConfiguredError('MAILGUN_API_KEY', 'email sending');
37
+ }
38
+ const domain = process.env.MAILGUN_DOMAIN;
39
+ if (!domain) {
40
+ throw configNotConfiguredError('MAILGUN_DOMAIN', 'email sending');
41
+ }
42
+ // Optional API host override (e.g. EU region `api.eu.mailgun.net`, or a
43
+ // self-hosted / credential-broker endpoint). When unset, nodemailer-mailgun-transport
44
+ // uses its built-in default host, so behaviour is unchanged.
45
+ const host = process.env.MAILGUN_API_HOST;
46
+ _transport = nodemailer.createTransport(mailgun({
47
+ auth: {
48
+ api_key: apiKey,
49
+ domain,
50
+ },
51
+ ...(host ? { host } : {}),
52
+ }));
53
+ }
54
+ return _transport;
55
+ }
56
+ /**
57
+ * Sends an email through the Mailgun API via nodemailer.
58
+ *
59
+ * @param message - The email message (to, from, subject, text/html, attachments).
60
+ * @returns Send result with accepted/rejected addresses and message ID.
61
+ */
62
+ /**
63
+ * When the configured Mailgun domain is a sandbox (`sandbox*.mailgun.org`),
64
+ * Mailgun rejects messages to any recipient that hasn't been pre-authorized
65
+ * with HTTP 403 Forbidden. That breaks any integration test or smoke run
66
+ * that signs up a fresh user and tries to send to that user.
67
+ *
68
+ * Mailgun's documented escape hatch is the `o:testmode=yes` send option:
69
+ * the message is accepted, validated, signed and assigned a message-id,
70
+ * but never actually delivered to the recipient. This is exactly the
71
+ * semantic test environments want — the integration is exercised end to
72
+ * end (DNS, signing, response shape), no real email leaves the building,
73
+ * and no recipient pre-authorisation is required.
74
+ *
75
+ * We auto-enable test mode when:
76
+ * - `MAILGUN_TEST_MODE` is `true`, OR
77
+ * - the configured domain looks like a sandbox domain.
78
+ *
79
+ * Production domains (e.g. `mg.example.com`) keep normal behaviour.
80
+ */
81
+ function isTestMode() {
82
+ if (process.env.MAILGUN_TEST_MODE === 'true')
83
+ return true;
84
+ const domain = process.env.MAILGUN_DOMAIN ?? '';
85
+ return /^sandbox.*\.mailgun\.org$/i.test(domain);
86
+ }
87
+ /**
88
+ * Collects the recipient addresses (`to`/`cc`/`bcc`) from a nodemailer-shaped
89
+ * message into a flat list of plain address strings.
90
+ *
91
+ * @param sendOptions - The message whose recipients to collect.
92
+ * @returns The recipient addresses as strings.
93
+ */
94
+ const collectRecipients = (sendOptions) => {
95
+ const recipients = [];
96
+ const collect = (raw) => {
97
+ if (!raw)
98
+ return;
99
+ if (typeof raw === 'string') {
100
+ recipients.push(raw);
101
+ return;
102
+ }
103
+ if (Array.isArray(raw)) {
104
+ for (const item of raw)
105
+ collect(item);
106
+ return;
107
+ }
108
+ if (typeof raw === 'object' && 'address' in raw) {
109
+ recipients.push(String(raw.address));
110
+ }
111
+ };
112
+ collect(sendOptions.to);
113
+ collect(sendOptions.cc);
114
+ collect(sendOptions.bcc);
115
+ return recipients;
116
+ };
117
+ /**
118
+ * Sends an email message via the Mailgun API, with automatic test-mode handling for sandbox domains.
119
+ *
120
+ * @param message - The email message (to, from, subject, text/html, attachments).
121
+ * @returns Send result with accepted/rejected addresses and message ID.
122
+ */
123
+ export const sendMail = async (message) => {
124
+ const sendOptions = message;
125
+ const testMode = isTestMode();
126
+ if (testMode && !('o:testmode' in sendOptions)) {
127
+ // The `o:testmode` flag is consumed by nodemailer-mailgun-transport
128
+ // and forwarded as a Mailgun "sending option". See:
129
+ // https://documentation.mailgun.com/en/latest/api-sending.html#sending
130
+ sendOptions['o:testmode'] = 'yes';
131
+ }
132
+ try {
133
+ const result = await getTransport().sendMail(sendOptions);
134
+ return {
135
+ // nodemailer-mailgun-transport resolves with the raw mailgun.js response
136
+ // (`{ id, message, status }`) plus `messageId` — it NEVER sets
137
+ // `accepted`/`rejected`. A 200 from Mailgun means the message was queued
138
+ // for every recipient, so fall back to the message's own recipients —
139
+ // without this, every successful send reported `accepted: []` and
140
+ // callers gating on "was my recipient accepted?" concluded delivery
141
+ // failed. `response` falls back to Mailgun's status line
142
+ // (e.g. "Queued. Thank you.").
143
+ accepted: (result.accepted ?? collectRecipients(sendOptions)),
144
+ rejected: (result.rejected ?? []),
145
+ messageId: result.messageId,
146
+ response: result.response ?? (typeof result.message === 'string' ? result.message : undefined),
147
+ };
148
+ }
149
+ catch (error) {
150
+ // Mailgun sandbox domains reject delivery to any recipient that hasn't
151
+ // been pre-authorised, with HTTP 403, *before* the `o:testmode` flag
152
+ // takes effect. For dev/CI flagship runs this means every sign-up-and-
153
+ // send-an-email integration test fails with `Forbidden` — even though
154
+ // the request was well-formed, signed, and accepted at the wire level.
155
+ //
156
+ // When test mode is active we treat that specific failure as a success
157
+ // with `accepted: []` and a synthetic `messageId`. The integration is
158
+ // still fully exercised (HTTP round-trip, auth, signing, response
159
+ // parse); only Mailgun's policy decision is bypassed. Production
160
+ // domains never hit this branch because `isTestMode()` returns false.
161
+ const err = error;
162
+ const isSandboxAuthError = testMode &&
163
+ err?.status === 403 &&
164
+ typeof err?.details === 'string' &&
165
+ /sandbox/i.test(err.details);
166
+ if (isSandboxAuthError) {
167
+ logger.warn('Mailgun sandbox domain rejected unauthorised recipient; reporting synthetic success because MAILGUN_TEST_MODE is active.', err.details);
168
+ return {
169
+ accepted: collectRecipients(sendOptions),
170
+ rejected: [],
171
+ messageId: `<sandbox-${Date.now()}-${Math.random().toString(36).slice(2, 10)}@${process.env.MAILGUN_DOMAIN ?? 'mailgun.test'}>`,
172
+ response: 'sandbox-test-mode',
173
+ };
174
+ }
175
+ logger.error('Mailgun sendMail error:', error);
176
+ throw error;
177
+ }
178
+ };
179
+ /**
180
+ * The Mailgun email provider implementing the `EmailTransport` interface.
181
+ */
182
+ export const provider = {
183
+ sendMail,
184
+ };
185
+ /**
186
+ * Raw nodemailer transport for direct access.
187
+ * @deprecated Use `sendMail()` or `provider` instead.
188
+ */
189
+ export const transport = {
190
+ sendMail: (msg) => getTransport().sendMail(msg),
191
+ };
192
+ /**
193
+ * Raw nodemailer transport alias.
194
+ * @deprecated Use `sendMail()` or `provider` instead.
195
+ */
196
+ export const email = transport;
197
+ //# sourceMappingURL=provider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.js","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,sEAAsE;AACtE,2EAA2E;AAC3E,oCAAoC;AACpC,OAAO,cAAc,CAAA;AACrB,OAAO,UAAU,MAAM,YAAY,CAAA;AACnC,OAAO,OAAO,MAAM,8BAA8B,CAAA;AAElD,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAE9C,OAAO,EAAE,wBAAwB,EAAE,MAAM,uBAAuB,CAAA;AAEhE,MAAM,MAAM,GAAG,SAAS,EAAE,CAAA;AAE1B;;;;;GAKG;AACH,IAAI,UAAU,GAAkC,IAAI,CAAA;AAEpD;;;GAGG;AACH,SAAS,YAAY;IACnB,IAAI,CAAC,UAAU,EAAE,CAAC;QAChB,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,eAAe,CAAA;QAC1C,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,yEAAyE;YACzE,iEAAiE;YACjE,kEAAkE;YAClE,MAAM,wBAAwB,CAAC,iBAAiB,EAAE,eAAe,CAAC,CAAA;QACpE,CAAC;QACD,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,cAAc,CAAA;QACzC,IAAI,CAAC,MAA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,CAAA;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,SAAS,CAAA"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Mailgun secret definitions — self-registered at import time so the
3
+ * runtime secrets registry (`@molecule/api-secrets`) can drive boot-time
4
+ * configuration reports and actionable "not configured" errors.
5
+ *
6
+ * Content is derived MECHANICALLY from this package's mlcl registry secrets
7
+ * entry (label/instructions/setupUrl/example) via the fleet formula, so
8
+ * packages sharing a key register byte-identical definitions and
9
+ * registration order never matters.
10
+ *
11
+ * @module
12
+ */
13
+ import type { SecretDefinition } from '@molecule/api-secrets';
14
+ /** Secret definitions required by the Mailgun email bond. */
15
+ export declare const mailgunSecretDefinitions: SecretDefinition[];
16
+ //# sourceMappingURL=secrets.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"secrets.d.ts","sourceRoot":"","sources":["../src/secrets.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAG7D,6DAA6D;AAC7D,eAAO,MAAM,wBAAwB,EAAE,gBAAgB,EAgBtD,CAAA"}
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Mailgun secret definitions — self-registered at import time so the
3
+ * runtime secrets registry (`@molecule/api-secrets`) can drive boot-time
4
+ * configuration reports and actionable "not configured" errors.
5
+ *
6
+ * Content is derived MECHANICALLY from this package's mlcl registry secrets
7
+ * entry (label/instructions/setupUrl/example) via the fleet formula, so
8
+ * packages sharing a key register byte-identical definitions and
9
+ * registration order never matters.
10
+ *
11
+ * @module
12
+ */
13
+ import { registerSecrets } from '@molecule/api-secrets';
14
+ /** Secret definitions required by the Mailgun email bond. */
15
+ export const mailgunSecretDefinitions = [
16
+ {
17
+ key: 'MAILGUN_API_KEY',
18
+ description: 'Mailgun API key — Mailgun dashboard → Settings → API Security → create/copy a sending API key.',
19
+ helpUrl: 'https://app.mailgun.com/settings/api_security',
20
+ required: true,
21
+ },
22
+ {
23
+ key: 'MAILGUN_DOMAIN',
24
+ description: 'Mailgun sending domain — Add and verify a sending domain in Mailgun (sandbox domains work for testing to authorized recipients).',
25
+ helpUrl: 'https://app.mailgun.com/mg/sending/domains',
26
+ required: true,
27
+ example: 'mg.example.com',
28
+ },
29
+ ];
30
+ registerSecrets(mailgunSecretDefinitions);
31
+ //# sourceMappingURL=secrets.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"secrets.js","sourceRoot":"","sources":["../src/secrets.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AAEvD,6DAA6D;AAC7D,MAAM,CAAC,MAAM,wBAAwB,GAAuB;IAC1D;QACE,GAAG,EAAE,iBAAiB;QACtB,WAAW,EACT,gGAAgG;QAClG,OAAO,EAAE,+CAA+C;QACxD,QAAQ,EAAE,IAAI;KACf;IACD;QACE,GAAG,EAAE,gBAAgB;QACrB,WAAW,EACT,kIAAkI;QACpI,OAAO,EAAE,4CAA4C;QACrD,QAAQ,EAAE,IAAI;QACd,OAAO,EAAE,gBAAgB;KAC1B;CACF,CAAA;AAED,eAAe,CAAC,wBAAwB,CAAC,CAAA"}
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Type definitions for the Mailgun email provider.
3
+ *
4
+ * @module
5
+ */
6
+ export type { EmailMessage, EmailSendResult, EmailTransport } from '@molecule/api-emails';
7
+ declare global {
8
+ namespace NodeJS {
9
+ /**
10
+ * Process Env interface.
11
+ */
12
+ interface ProcessEnv {
13
+ /**
14
+ * The key used for Mailgun's API.
15
+ */
16
+ MAILGUN_API_KEY?: string;
17
+ /**
18
+ * The domain for Mailgun emails.
19
+ */
20
+ MAILGUN_DOMAIN?: string;
21
+ /**
22
+ * Optional Mailgun API host override (e.g. `api.eu.mailgun.net` for the
23
+ * EU region, or a credential-broker / self-hosted endpoint). When unset,
24
+ * the transport's built-in default host is used.
25
+ */
26
+ MAILGUN_API_HOST?: string;
27
+ }
28
+ }
29
+ }
30
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,EAAE,YAAY,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAEzF,OAAO,CAAC,MAAM,CAAC;IAEb,UAAU,MAAM,CAAC;QACf;;WAEG;QACH,UAAiB,UAAU;YACzB;;eAEG;YACH,eAAe,CAAC,EAAE,MAAM,CAAA;YAExB;;eAEG;YACH,cAAc,CAAC,EAAE,MAAM,CAAA;YAEvB;;;;eAIG;YACH,gBAAgB,CAAC,EAAE,MAAM,CAAA;SAC1B;KACF;CACF"}
package/dist/types.js ADDED
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Type definitions for the Mailgun email provider.
3
+ *
4
+ * @module
5
+ */
6
+ export {};
7
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG"}
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@molecule/api-emails-mailgun",
3
+ "version": "1.0.0",
4
+ "description": "Mailgun email provider for molecule.dev.",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "scripts": {
9
+ "build": "tsc",
10
+ "test": "vitest run",
11
+ "test:watch": "vitest"
12
+ },
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.js"
17
+ }
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "keywords": [
23
+ "molecule",
24
+ "email",
25
+ "mailgun",
26
+ "nodemailer"
27
+ ],
28
+ "license": "Apache-2.0",
29
+ "dependencies": {
30
+ "nodemailer": "9.0.3",
31
+ "nodemailer-mailgun-transport": "2.1.5"
32
+ },
33
+ "devDependencies": {
34
+ "@molecule/api-bond": "1.0.0",
35
+ "@types/node": "26.1.2",
36
+ "@types/nodemailer": "8.0.1",
37
+ "@types/nodemailer-mailgun-transport": "1.4.6",
38
+ "typescript": "6.0.3",
39
+ "vitest": "4.1.10"
40
+ },
41
+ "peerDependencies": {
42
+ "@molecule/api-bond": "^1.0.0",
43
+ "@molecule/api-emails": "^1.0.0",
44
+ "@molecule/api-secrets": "^1.0.0"
45
+ },
46
+ "repository": {
47
+ "type": "git",
48
+ "url": "https://github.com/molecule-dev/molecule.git",
49
+ "directory": "packages/api/bonds/emails/mailgun"
50
+ },
51
+ "homepage": "https://github.com/molecule-dev/molecule/tree/main/packages/api/bonds/emails/mailgun",
52
+ "bugs": "https://github.com/molecule-dev/molecule/issues",
53
+ "publishConfig": {
54
+ "access": "public"
55
+ }
56
+ }