simple-smtp-server 0.2.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,23 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Dan Lynch <pyramation@gmail.com>
4
+ Copyright (c) 2025 Constructive <developers@constructive.io>
5
+ Copyright (c) 2020-present, Interweb, Inc.
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in all
15
+ copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,167 @@
1
+ # simple-smtp-server
2
+
3
+ SMTP-based email sender for Constructive services. This package exposes a `send` helper with the same call shape used by `@launchql/postmaster` (e.g. `{ to, subject, html, text }`).
4
+
5
+ Configuration is managed through the centralized `@pgpmjs/env` system, which merges defaults, config files, environment variables, and runtime overrides.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ pnpm add simple-smtp-server
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```ts
16
+ import { send } from 'simple-smtp-server';
17
+
18
+ await send({
19
+ to: 'user@example.com',
20
+ subject: 'Welcome',
21
+ html: '<p>Hello from SMTP</p>'
22
+ });
23
+ ```
24
+
25
+ ### Programmatic overrides
26
+
27
+ You can pass SMTP configuration overrides as a second argument to `send()`:
28
+
29
+ ```ts
30
+ import { send } from 'simple-smtp-server';
31
+
32
+ await send(
33
+ {
34
+ to: 'user@example.com',
35
+ subject: 'Welcome',
36
+ html: '<p>Hello from SMTP</p>'
37
+ },
38
+ {
39
+ host: 'smtp.example.com',
40
+ port: 587,
41
+ secure: false,
42
+ user: 'myuser',
43
+ pass: 'mypassword',
44
+ from: 'no-reply@example.com'
45
+ }
46
+ );
47
+ ```
48
+
49
+ ### Resetting the transport
50
+
51
+ If you need to reset the cached SMTP transport (e.g., in tests), use `resetTransport()`:
52
+
53
+ ```ts
54
+ import { resetTransport } from 'simple-smtp-server';
55
+
56
+ resetTransport();
57
+ ```
58
+
59
+ ## Environment variables
60
+
61
+ Required (unless noted):
62
+
63
+ - `SMTP_HOST` (required)
64
+ - `SMTP_PORT` (optional, default: `587`)
65
+ - `SMTP_SECURE` (`true`/`false`; default: `false`, set `true` for port `465`)
66
+ - `SMTP_USER` (optional if the server allows anonymous auth)
67
+ - `SMTP_PASS` (required when `SMTP_USER` is set and auth is required)
68
+ - `SMTP_FROM` (default sender address if `from` is not passed to `send`)
69
+
70
+ Optional:
71
+
72
+ - `SMTP_REPLY_TO` (default reply-to address when not provided per message)
73
+ - `SMTP_REQUIRE_TLS` (`true`/`false`)
74
+ - `SMTP_TLS_REJECT_UNAUTHORIZED` (`true`/`false`, default: `true`)
75
+ - `SMTP_POOL` (`true`/`false`)
76
+ - `SMTP_MAX_CONNECTIONS` (number)
77
+ - `SMTP_MAX_MESSAGES` (number)
78
+ - `SMTP_NAME` (client hostname)
79
+ - `SMTP_LOGGER` (`true`/`false`, nodemailer transport logging)
80
+ - `SMTP_DEBUG` (`true`/`false`, nodemailer debug output)
81
+
82
+ ## Test / debug
83
+
84
+ This package ships a small test runner you can use to validate your SMTP settings.
85
+
86
+ ```bash
87
+ SMTP_HOST=smtp.example.com \
88
+ SMTP_PORT=587 \
89
+ SMTP_USER=example \
90
+ SMTP_PASS=secret \
91
+ SMTP_FROM="no-reply@example.com" \
92
+ SMTP_TEST_TO="you@example.com" \
93
+ pnpm --filter "simple-smtp-server" test:send
94
+ ```
95
+
96
+ To use the built-in local SMTP catcher instead of a real SMTP server:
97
+
98
+ ```bash
99
+ SMTP_TEST_USE_CATCHER=true \
100
+ pnpm --filter "simple-smtp-server" test:send
101
+ ```
102
+
103
+ Optional test overrides:
104
+
105
+ - `SMTP_TEST_FROM`
106
+ - `SMTP_TEST_SUBJECT`
107
+ - `SMTP_TEST_TEXT`
108
+ - `SMTP_TEST_HTML`
109
+ - `SMTP_TEST_USE_CATCHER`
110
+
111
+ ---
112
+
113
+ ## Education and Tutorials
114
+
115
+ 1. 🚀 [Quickstart: Getting Up and Running](https://constructive.io/learn/quickstart)
116
+ Get started with modular databases in minutes. Install prerequisites and deploy your first module.
117
+
118
+ 2. 📦 [Modular PostgreSQL Development with Database Packages](https://constructive.io/learn/modular-postgres)
119
+ Learn to organize PostgreSQL projects with pgpm workspaces and reusable database modules.
120
+
121
+ 3. ✏️ [Authoring Database Changes](https://constructive.io/learn/authoring-database-changes)
122
+ Master the workflow for adding, organizing, and managing database changes with pgpm.
123
+
124
+ 4. 🧪 [End-to-End PostgreSQL Testing with TypeScript](https://constructive.io/learn/e2e-postgres-testing)
125
+ Master end-to-end PostgreSQL testing with ephemeral databases, RLS testing, and CI/CD automation.
126
+
127
+ 5. ⚡ [Supabase Testing](https://constructive.io/learn/supabase)
128
+ Use TypeScript-first tools to test Supabase projects with realistic RLS, policies, and auth contexts.
129
+
130
+ 6. 💧 [Drizzle ORM Testing](https://constructive.io/learn/drizzle-testing)
131
+ Run full-stack tests with Drizzle ORM, including database setup, teardown, and RLS enforcement.
132
+
133
+ 7. 🔧 [Troubleshooting](https://constructive.io/learn/troubleshooting)
134
+ Common issues and solutions for pgpm, PostgreSQL, and testing.
135
+
136
+ ## Related Constructive Tooling
137
+
138
+ ### 📦 Package Management
139
+
140
+ * [pgpm](https://github.com/constructive-io/constructive/tree/main/pgpm/pgpm): **🖥️ PostgreSQL Package Manager** for modular Postgres development. Works with database workspaces, scaffolding, migrations, seeding, and installing database packages.
141
+
142
+ ### 🧪 Testing
143
+
144
+ * [pgsql-test](https://github.com/constructive-io/constructive/tree/main/postgres/pgsql-test): **📊 Isolated testing environments** with per-test transaction rollbacks—ideal for integration tests, complex migrations, and RLS simulation.
145
+ * [pgsql-seed](https://github.com/constructive-io/constructive/tree/main/postgres/pgsql-seed): **🌱 PostgreSQL seeding utilities** for CSV, JSON, SQL data loading, and pgpm deployment.
146
+ * [supabase-test](https://github.com/constructive-io/constructive/tree/main/postgres/supabase-test): **🧪 Supabase-native test harness** preconfigured for the local Supabase stack—per-test rollbacks, JWT/role context helpers, and CI/GitHub Actions ready.
147
+ * [graphile-test](https://github.com/constructive-io/constructive/tree/main/graphile/graphile-test): **🔐 Authentication mocking** for Graphile-focused test helpers and emulating row-level security contexts.
148
+ * [pg-query-context](https://github.com/constructive-io/constructive/tree/main/postgres/pg-query-context): **🔒 Session context injection** to add session-local context (e.g., `SET LOCAL`) into queries—ideal for setting `role`, `jwt.claims`, and other session settings.
149
+
150
+ ### 🧠 Parsing & AST
151
+
152
+ * [pgsql-parser](https://www.npmjs.com/package/pgsql-parser): **🔄 SQL conversion engine** that interprets and converts PostgreSQL syntax.
153
+ * [libpg-query-node](https://www.npmjs.com/package/libpg-query): **🌉 Node.js bindings** for `libpg_query`, converting SQL into parse trees.
154
+ * [pg-proto-parser](https://www.npmjs.com/package/pg-proto-parser): **📦 Protobuf parser** for parsing PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
155
+ * [@pgsql/enums](https://www.npmjs.com/package/@pgsql/enums): **🏷️ TypeScript enums** for PostgreSQL AST for safe and ergonomic parsing logic.
156
+ * [@pgsql/types](https://www.npmjs.com/package/@pgsql/types): **📝 Type definitions** for PostgreSQL AST nodes in TypeScript.
157
+ * [@pgsql/utils](https://www.npmjs.com/package/@pgsql/utils): **🛠️ AST utilities** for constructing and transforming PostgreSQL syntax trees.
158
+
159
+ ## Credits
160
+
161
+ **🛠 Built by the [Constructive](https://constructive.io) team — creators of modular Postgres tooling for secure, composable backends. If you like our work, contribute on [GitHub](https://github.com/constructive-io).**
162
+
163
+ ## Disclaimer
164
+
165
+ AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
166
+
167
+ No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
package/esm/index.js ADDED
@@ -0,0 +1,98 @@
1
+ import nodemailer from 'nodemailer';
2
+ import { getEnvOptions } from '@pgpmjs/env';
3
+ const buildTransportOptions = (smtpOpts) => {
4
+ const { host, port, secure, user, pass, requireTLS, tlsRejectUnauthorized, pool, maxConnections, maxMessages, name, logger, debug } = smtpOpts;
5
+ if (!host) {
6
+ throw new Error('Missing SMTP_HOST');
7
+ }
8
+ const resolvedPort = port ?? (secure ? 465 : 587);
9
+ const resolvedSecure = secure ?? resolvedPort === 465;
10
+ const auth = user
11
+ ? {
12
+ user,
13
+ pass: pass ?? ''
14
+ }
15
+ : undefined;
16
+ const options = {
17
+ host,
18
+ port: resolvedPort,
19
+ secure: resolvedSecure
20
+ };
21
+ if (auth) {
22
+ options.auth = auth;
23
+ }
24
+ if (requireTLS !== undefined) {
25
+ options.requireTLS = requireTLS;
26
+ }
27
+ if (tlsRejectUnauthorized !== undefined) {
28
+ options.tls = {
29
+ rejectUnauthorized: tlsRejectUnauthorized
30
+ };
31
+ }
32
+ if (pool !== undefined) {
33
+ options.pool = pool;
34
+ }
35
+ if (maxConnections !== undefined) {
36
+ options.maxConnections = maxConnections;
37
+ }
38
+ if (maxMessages !== undefined) {
39
+ options.maxMessages = maxMessages;
40
+ }
41
+ if (name) {
42
+ options.name = name;
43
+ }
44
+ if (logger !== undefined) {
45
+ options.logger = logger;
46
+ }
47
+ if (debug !== undefined) {
48
+ options.debug = debug;
49
+ }
50
+ return options;
51
+ };
52
+ let transport;
53
+ let cachedSmtpOpts;
54
+ const getTransport = (overrides) => {
55
+ const opts = getEnvOptions({ smtp: overrides });
56
+ const smtpOpts = opts.smtp ?? {};
57
+ if (!transport || overrides) {
58
+ transport = nodemailer.createTransport(buildTransportOptions(smtpOpts));
59
+ cachedSmtpOpts = smtpOpts;
60
+ }
61
+ return { transport, smtpOpts: cachedSmtpOpts ?? smtpOpts };
62
+ };
63
+ const resolveFrom = (from, smtpOpts) => {
64
+ const resolved = from ?? smtpOpts.from;
65
+ if (!resolved) {
66
+ throw new Error('Missing from address. Set SMTP_FROM or pass from in send().');
67
+ }
68
+ return resolved;
69
+ };
70
+ export const send = async (options, smtpOverrides) => {
71
+ if (!options.to) {
72
+ throw new Error('Missing "to"');
73
+ }
74
+ if (!options.subject) {
75
+ throw new Error('Missing "subject"');
76
+ }
77
+ if (!options.html && !options.text) {
78
+ throw new Error('Missing "html" or "text"');
79
+ }
80
+ const { transport: mailer, smtpOpts } = getTransport(smtpOverrides);
81
+ const mailOptions = {
82
+ to: options.to,
83
+ subject: options.subject,
84
+ html: options.html,
85
+ text: options.text,
86
+ from: resolveFrom(options.from, smtpOpts),
87
+ cc: options.cc,
88
+ bcc: options.bcc,
89
+ replyTo: options.replyTo ?? smtpOpts.replyTo,
90
+ headers: options.headers,
91
+ attachments: options.attachments
92
+ };
93
+ return mailer.sendMail(mailOptions);
94
+ };
95
+ export const resetTransport = () => {
96
+ transport = undefined;
97
+ cachedSmtpOpts = undefined;
98
+ };
package/index.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ import { SendMailOptions } from 'nodemailer';
2
+ import SMTPTransport from 'nodemailer/lib/smtp-transport';
3
+ import { SmtpOptions } from '@pgpmjs/types';
4
+ type SendInput = {
5
+ to: string | string[];
6
+ subject: string;
7
+ html?: string;
8
+ text?: string;
9
+ from?: string;
10
+ cc?: string | string[];
11
+ bcc?: string | string[];
12
+ replyTo?: string;
13
+ headers?: Record<string, string>;
14
+ attachments?: SendMailOptions['attachments'];
15
+ };
16
+ export declare const send: (options: SendInput, smtpOverrides?: SmtpOptions) => Promise<SMTPTransport.SentMessageInfo>;
17
+ export declare const resetTransport: () => void;
18
+ export type { SendInput as SendOptions };
package/index.js ADDED
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.resetTransport = exports.send = void 0;
7
+ const nodemailer_1 = __importDefault(require("nodemailer"));
8
+ const env_1 = require("@pgpmjs/env");
9
+ const buildTransportOptions = (smtpOpts) => {
10
+ const { host, port, secure, user, pass, requireTLS, tlsRejectUnauthorized, pool, maxConnections, maxMessages, name, logger, debug } = smtpOpts;
11
+ if (!host) {
12
+ throw new Error('Missing SMTP_HOST');
13
+ }
14
+ const resolvedPort = port ?? (secure ? 465 : 587);
15
+ const resolvedSecure = secure ?? resolvedPort === 465;
16
+ const auth = user
17
+ ? {
18
+ user,
19
+ pass: pass ?? ''
20
+ }
21
+ : undefined;
22
+ const options = {
23
+ host,
24
+ port: resolvedPort,
25
+ secure: resolvedSecure
26
+ };
27
+ if (auth) {
28
+ options.auth = auth;
29
+ }
30
+ if (requireTLS !== undefined) {
31
+ options.requireTLS = requireTLS;
32
+ }
33
+ if (tlsRejectUnauthorized !== undefined) {
34
+ options.tls = {
35
+ rejectUnauthorized: tlsRejectUnauthorized
36
+ };
37
+ }
38
+ if (pool !== undefined) {
39
+ options.pool = pool;
40
+ }
41
+ if (maxConnections !== undefined) {
42
+ options.maxConnections = maxConnections;
43
+ }
44
+ if (maxMessages !== undefined) {
45
+ options.maxMessages = maxMessages;
46
+ }
47
+ if (name) {
48
+ options.name = name;
49
+ }
50
+ if (logger !== undefined) {
51
+ options.logger = logger;
52
+ }
53
+ if (debug !== undefined) {
54
+ options.debug = debug;
55
+ }
56
+ return options;
57
+ };
58
+ let transport;
59
+ let cachedSmtpOpts;
60
+ const getTransport = (overrides) => {
61
+ const opts = (0, env_1.getEnvOptions)({ smtp: overrides });
62
+ const smtpOpts = opts.smtp ?? {};
63
+ if (!transport || overrides) {
64
+ transport = nodemailer_1.default.createTransport(buildTransportOptions(smtpOpts));
65
+ cachedSmtpOpts = smtpOpts;
66
+ }
67
+ return { transport, smtpOpts: cachedSmtpOpts ?? smtpOpts };
68
+ };
69
+ const resolveFrom = (from, smtpOpts) => {
70
+ const resolved = from ?? smtpOpts.from;
71
+ if (!resolved) {
72
+ throw new Error('Missing from address. Set SMTP_FROM or pass from in send().');
73
+ }
74
+ return resolved;
75
+ };
76
+ const send = async (options, smtpOverrides) => {
77
+ if (!options.to) {
78
+ throw new Error('Missing "to"');
79
+ }
80
+ if (!options.subject) {
81
+ throw new Error('Missing "subject"');
82
+ }
83
+ if (!options.html && !options.text) {
84
+ throw new Error('Missing "html" or "text"');
85
+ }
86
+ const { transport: mailer, smtpOpts } = getTransport(smtpOverrides);
87
+ const mailOptions = {
88
+ to: options.to,
89
+ subject: options.subject,
90
+ html: options.html,
91
+ text: options.text,
92
+ from: resolveFrom(options.from, smtpOpts),
93
+ cc: options.cc,
94
+ bcc: options.bcc,
95
+ replyTo: options.replyTo ?? smtpOpts.replyTo,
96
+ headers: options.headers,
97
+ attachments: options.attachments
98
+ };
99
+ return mailer.sendMail(mailOptions);
100
+ };
101
+ exports.send = send;
102
+ const resetTransport = () => {
103
+ transport = undefined;
104
+ cachedSmtpOpts = undefined;
105
+ };
106
+ exports.resetTransport = resetTransport;
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "simple-smtp-server",
3
+ "version": "0.2.0",
4
+ "author": "Constructive <developers@constructive.io>",
5
+ "description": "SMTP email sender for Constructive",
6
+ "main": "index.js",
7
+ "module": "esm/index.js",
8
+ "types": "index.d.ts",
9
+ "homepage": "https://github.com/constructive-io/constructive",
10
+ "license": "MIT",
11
+ "publishConfig": {
12
+ "access": "public",
13
+ "directory": "dist"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/constructive-io/constructive"
18
+ },
19
+ "bugs": {
20
+ "url": "https://github.com/constructive-io/constructive/issues"
21
+ },
22
+ "scripts": {
23
+ "clean": "makage clean",
24
+ "prepack": "npm run build",
25
+ "build": "makage build",
26
+ "build:dev": "makage build --dev",
27
+ "lint": "eslint . --fix",
28
+ "test": "jest --passWithNoTests",
29
+ "test:watch": "jest --watch",
30
+ "test:send": "ts-node __tests__/test-send.ts",
31
+ "test:send:dev": "ts-node __tests__/test-send.ts"
32
+ },
33
+ "dependencies": {
34
+ "@pgpmjs/env": "^2.10.0",
35
+ "@pgpmjs/types": "^2.15.0",
36
+ "nodemailer": "^6.9.13"
37
+ },
38
+ "devDependencies": {
39
+ "@types/nodemailer": "^7.0.5",
40
+ "@types/smtp-server": "^3.5.12",
41
+ "makage": "^0.1.10",
42
+ "smtp-server": "^3.14.0",
43
+ "ts-node": "^10.9.2"
44
+ },
45
+ "keywords": [
46
+ "smtp",
47
+ "email",
48
+ "nodemailer",
49
+ "postmaster"
50
+ ],
51
+ "gitHead": "481b3a50b4eec2da6b376c4cd1868065e1e28edb"
52
+ }