@remit/smtp-service 0.0.19 → 0.0.21
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/package.json +40 -40
- package/src/smtp-client.test.ts +38 -0
- package/src/smtp-client.ts +100 -64
package/package.json
CHANGED
|
@@ -1,42 +1,42 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
2
|
+
"name": "@remit/smtp-service",
|
|
3
|
+
"version": "0.0.21",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "src/index.ts",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./src/index.ts",
|
|
9
|
+
"default": "./src/index.ts"
|
|
10
|
+
},
|
|
11
|
+
"./message-builder": {
|
|
12
|
+
"types": "./src/message-builder.ts",
|
|
13
|
+
"default": "./src/message-builder.ts"
|
|
14
|
+
},
|
|
15
|
+
"./test-fake-smtp": {
|
|
16
|
+
"types": "./src/test-fake-smtp.ts",
|
|
17
|
+
"default": "./src/test-fake-smtp.ts"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"test:typecheck": "tsgo --noEmit",
|
|
22
|
+
"test:run": "node $NODE_TEST_FLAGS --env-file=../../localhost-test-unit.env --experimental-test-coverage --test-coverage-include='src/**' --test-coverage-exclude='src/**/*.test.ts' --test-coverage-exclude='src/**/*.test.tsx' --test-coverage-lines=95 --test 'src/**/*.test.ts'",
|
|
23
|
+
"test": "npm run test:typecheck && npm run test:run"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/node": "*"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"nodemailer": "^10.0.6",
|
|
30
|
+
"@remit/data-ports": "*",
|
|
31
|
+
"@types/nodemailer": "^8.0.1"
|
|
32
|
+
},
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public"
|
|
36
|
+
},
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "git+https://github.com/remit-mail/reader.git",
|
|
40
|
+
"directory": "packages/smtp-service"
|
|
41
|
+
}
|
|
42
42
|
}
|
package/src/smtp-client.test.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
SmtpConnectionError,
|
|
9
9
|
type SmtpCredentials,
|
|
10
10
|
sendMail,
|
|
11
|
+
verifySmtpConnection,
|
|
11
12
|
} from "./smtp-client.js";
|
|
12
13
|
import {
|
|
13
14
|
type FakeSmtp,
|
|
@@ -176,3 +177,40 @@ describe("sendMail", () => {
|
|
|
176
177
|
assert.ok(result.error);
|
|
177
178
|
});
|
|
178
179
|
});
|
|
180
|
+
|
|
181
|
+
describe("verifySmtpConnection", () => {
|
|
182
|
+
const servers: FakeSmtp[] = [];
|
|
183
|
+
const spawn = async (opts?: FakeSmtpOptions): Promise<FakeSmtp> => {
|
|
184
|
+
const server = await startFakeSmtp(opts);
|
|
185
|
+
servers.push(server);
|
|
186
|
+
return server;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
after(() => {
|
|
190
|
+
for (const server of servers) {
|
|
191
|
+
server.close();
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("succeeds when the server accepts the login", async () => {
|
|
196
|
+
const server = await spawn();
|
|
197
|
+
|
|
198
|
+
const result = await verifySmtpConnection(configFor(server.port));
|
|
199
|
+
|
|
200
|
+
assert.deepEqual(result, { success: true });
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("reports a rejected login as an authentication failure", async () => {
|
|
204
|
+
const server = await spawn({
|
|
205
|
+
authResponse: "535 5.7.8 Authentication credentials invalid\r\n",
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
const result = await verifySmtpConnection(configFor(server.port));
|
|
209
|
+
|
|
210
|
+
assert.deepEqual(result, {
|
|
211
|
+
success: false,
|
|
212
|
+
error:
|
|
213
|
+
"SMTP authentication failed: 535 5.7.8 Authentication credentials invalid",
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
});
|
package/src/smtp-client.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { TestResult } from "@remit/data-ports";
|
|
1
2
|
import nodemailer from "nodemailer";
|
|
2
3
|
import type { SMTPTransportGetSocket } from "nodemailer/lib/smtp-transport";
|
|
3
4
|
import { type MailMessage, toNodemailerOptions } from "./message-builder.js";
|
|
@@ -89,15 +90,26 @@ const buildSmtpAuth = (
|
|
|
89
90
|
throw new Error(`Unknown credential kind: ${JSON.stringify(_exhaustive)}`);
|
|
90
91
|
};
|
|
91
92
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
93
|
+
type SmtpFailure = Error & {
|
|
94
|
+
responseCode?: number;
|
|
95
|
+
code?: string;
|
|
96
|
+
command?: string;
|
|
97
|
+
response?: string;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const NETWORK_ERROR_CODES = new Set([
|
|
101
|
+
"ECONNREFUSED",
|
|
102
|
+
"ETIMEDOUT",
|
|
103
|
+
"ENOTFOUND",
|
|
104
|
+
"ECONNRESET",
|
|
105
|
+
"EHOSTUNREACH",
|
|
106
|
+
]);
|
|
107
|
+
|
|
108
|
+
const VERIFY_TIMEOUT_MS = 12_000;
|
|
109
|
+
|
|
110
|
+
const transporterFor = (config: SmtpConfig, socketTimeout: number) => {
|
|
99
111
|
const timeout = config.connectionTimeout ?? 30_000;
|
|
100
|
-
|
|
112
|
+
return nodemailer.createTransport({
|
|
101
113
|
host: config.host,
|
|
102
114
|
port: config.port,
|
|
103
115
|
secure: config.secure,
|
|
@@ -105,9 +117,50 @@ export const sendMail = async (
|
|
|
105
117
|
tls: config.tls,
|
|
106
118
|
connectionTimeout: timeout,
|
|
107
119
|
greetingTimeout: timeout,
|
|
108
|
-
socketTimeout
|
|
120
|
+
socketTimeout,
|
|
109
121
|
getSocket: config.getSocket,
|
|
110
122
|
});
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const classifySmtpError = (error: SmtpFailure): SmtpConnectionError | null => {
|
|
126
|
+
const smtpCode = error.responseCode;
|
|
127
|
+
|
|
128
|
+
if (
|
|
129
|
+
error.code === "EAUTH" ||
|
|
130
|
+
(smtpCode !== undefined && smtpCode >= 500 && error.command === "AUTH")
|
|
131
|
+
) {
|
|
132
|
+
// The server's own words, when it gave any: a 535 reading
|
|
133
|
+
// "SmtpClientAuthentication is disabled for the Tenant" is not a
|
|
134
|
+
// failure signing in again clears, and the account card can only
|
|
135
|
+
// say so if the text survives the classification.
|
|
136
|
+
return new SmtpConnectionError(
|
|
137
|
+
"auth",
|
|
138
|
+
error.response
|
|
139
|
+
? `SMTP authentication failed: ${error.response.trim()}`
|
|
140
|
+
: "SMTP authentication failed",
|
|
141
|
+
error,
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (error.code !== undefined && NETWORK_ERROR_CODES.has(error.code)) {
|
|
146
|
+
return new SmtpConnectionError(
|
|
147
|
+
"network",
|
|
148
|
+
`SMTP connection failed: ${error.code}`,
|
|
149
|
+
error,
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return null;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Send email via SMTP using Nodemailer
|
|
158
|
+
*/
|
|
159
|
+
export const sendMail = async (
|
|
160
|
+
config: SmtpConfig,
|
|
161
|
+
message: MailMessage,
|
|
162
|
+
): Promise<SendResult> => {
|
|
163
|
+
const transporter = transporterFor(config, 300_000);
|
|
111
164
|
|
|
112
165
|
return transporter
|
|
113
166
|
.sendMail(toNodemailerOptions(message))
|
|
@@ -117,62 +170,45 @@ export const sendMail = async (
|
|
|
117
170
|
response: info.response,
|
|
118
171
|
isTransient: false,
|
|
119
172
|
}))
|
|
173
|
+
.catch((error: SmtpFailure) => {
|
|
174
|
+
const classified = classifySmtpError(error);
|
|
175
|
+
if (classified) {
|
|
176
|
+
throw classified;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// 4xx = transient (retry), 5xx = permanent (no retry)
|
|
180
|
+
const smtpCode = error.responseCode;
|
|
181
|
+
const isTransient =
|
|
182
|
+
smtpCode !== undefined && smtpCode >= 400 && smtpCode < 500;
|
|
183
|
+
return {
|
|
184
|
+
success: false,
|
|
185
|
+
error,
|
|
186
|
+
smtpCode,
|
|
187
|
+
isTransient,
|
|
188
|
+
};
|
|
189
|
+
})
|
|
190
|
+
.finally(() => transporter.close());
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
export const verifySmtpConnection = async (
|
|
194
|
+
config: SmtpConfig,
|
|
195
|
+
): Promise<TestResult> => {
|
|
196
|
+
const transporter = transporterFor(
|
|
197
|
+
{
|
|
198
|
+
...config,
|
|
199
|
+
connectionTimeout: config.connectionTimeout ?? VERIFY_TIMEOUT_MS,
|
|
200
|
+
},
|
|
201
|
+
VERIFY_TIMEOUT_MS,
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
return transporter
|
|
205
|
+
.verify()
|
|
206
|
+
.then((): TestResult => ({ success: true }))
|
|
120
207
|
.catch(
|
|
121
|
-
(
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
command?: string;
|
|
126
|
-
response?: string;
|
|
127
|
-
},
|
|
128
|
-
) => {
|
|
129
|
-
const smtpCode = error.responseCode;
|
|
130
|
-
|
|
131
|
-
// Classify auth errors — EAUTH or 5xx on AUTH command
|
|
132
|
-
if (
|
|
133
|
-
error.code === "EAUTH" ||
|
|
134
|
-
(smtpCode !== undefined &&
|
|
135
|
-
smtpCode >= 500 &&
|
|
136
|
-
error.command === "AUTH")
|
|
137
|
-
) {
|
|
138
|
-
// The server's own words, when it gave any: a 535 reading
|
|
139
|
-
// "SmtpClientAuthentication is disabled for the Tenant" is not a
|
|
140
|
-
// failure signing in again clears, and the account card can only
|
|
141
|
-
// say so if the text survives the classification.
|
|
142
|
-
throw new SmtpConnectionError(
|
|
143
|
-
"auth",
|
|
144
|
-
error.response
|
|
145
|
-
? `SMTP authentication failed: ${error.response.trim()}`
|
|
146
|
-
: "SMTP authentication failed",
|
|
147
|
-
error,
|
|
148
|
-
);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// Classify network errors
|
|
152
|
-
if (
|
|
153
|
-
error.code === "ECONNREFUSED" ||
|
|
154
|
-
error.code === "ETIMEDOUT" ||
|
|
155
|
-
error.code === "ENOTFOUND" ||
|
|
156
|
-
error.code === "ECONNRESET" ||
|
|
157
|
-
error.code === "EHOSTUNREACH"
|
|
158
|
-
) {
|
|
159
|
-
throw new SmtpConnectionError(
|
|
160
|
-
"network",
|
|
161
|
-
`SMTP connection failed: ${error.code}`,
|
|
162
|
-
error,
|
|
163
|
-
);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// 4xx = transient (retry), 5xx = permanent (no retry)
|
|
167
|
-
const isTransient =
|
|
168
|
-
smtpCode !== undefined && smtpCode >= 400 && smtpCode < 500;
|
|
169
|
-
return {
|
|
170
|
-
success: false,
|
|
171
|
-
error,
|
|
172
|
-
smtpCode,
|
|
173
|
-
isTransient,
|
|
174
|
-
};
|
|
175
|
-
},
|
|
208
|
+
(error: SmtpFailure): TestResult => ({
|
|
209
|
+
success: false,
|
|
210
|
+
error: (classifySmtpError(error) ?? error).message,
|
|
211
|
+
}),
|
|
176
212
|
)
|
|
177
213
|
.finally(() => transporter.close());
|
|
178
214
|
};
|