@remit/smtp-service 0.0.17 → 0.0.19
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 +6 -2
- package/src/message-builder.ts +10 -10
- package/src/smtp-client.test.ts +33 -68
- package/src/smtp-client.ts +17 -5
- package/src/test-fake-smtp.ts +74 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@remit/smtp-service",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.19",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"exports": {
|
|
@@ -11,6 +11,10 @@
|
|
|
11
11
|
"./message-builder": {
|
|
12
12
|
"types": "./src/message-builder.ts",
|
|
13
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"
|
|
14
18
|
}
|
|
15
19
|
},
|
|
16
20
|
"scripts": {
|
|
@@ -24,7 +28,7 @@
|
|
|
24
28
|
"dependencies": {
|
|
25
29
|
"nodemailer": "^10.0.6",
|
|
26
30
|
"@remit/data-ports": "*",
|
|
27
|
-
"@types/nodemailer": "
|
|
31
|
+
"@types/nodemailer": "^8.0.1"
|
|
28
32
|
},
|
|
29
33
|
"license": "MIT",
|
|
30
34
|
"publishConfig": {
|
package/src/message-builder.ts
CHANGED
|
@@ -5,17 +5,17 @@ import nodemailer, { type SendMailOptions } from "nodemailer";
|
|
|
5
5
|
export interface MailMessage {
|
|
6
6
|
from: string;
|
|
7
7
|
to: string[];
|
|
8
|
-
cc?: string[];
|
|
9
|
-
bcc?: string[];
|
|
10
|
-
replyTo?: string;
|
|
11
|
-
subject?: string;
|
|
12
|
-
text?: string;
|
|
13
|
-
html?: string;
|
|
8
|
+
cc?: string[] | undefined;
|
|
9
|
+
bcc?: string[] | undefined;
|
|
10
|
+
replyTo?: string | undefined;
|
|
11
|
+
subject?: string | undefined;
|
|
12
|
+
text?: string | undefined;
|
|
13
|
+
html?: string | undefined;
|
|
14
14
|
messageId: string;
|
|
15
|
-
inReplyTo?: string;
|
|
16
|
-
references?: string;
|
|
17
|
-
date?: Date;
|
|
18
|
-
attachments?: MailAttachment[];
|
|
15
|
+
inReplyTo?: string | undefined;
|
|
16
|
+
references?: string | undefined;
|
|
17
|
+
date?: Date | undefined;
|
|
18
|
+
attachments?: MailAttachment[] | undefined;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
export interface MailAttachment {
|
package/src/smtp-client.test.ts
CHANGED
|
@@ -3,81 +3,23 @@ import net from "node:net";
|
|
|
3
3
|
import { after, describe, it } from "node:test";
|
|
4
4
|
import type { MailMessage } from "./message-builder.js";
|
|
5
5
|
import {
|
|
6
|
+
directSmtpSocket,
|
|
6
7
|
type SmtpConfig,
|
|
7
8
|
SmtpConnectionError,
|
|
8
9
|
type SmtpCredentials,
|
|
9
10
|
sendMail,
|
|
10
11
|
} from "./smtp-client.js";
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
port: number;
|
|
20
|
-
authCommands: string[];
|
|
21
|
-
close: () => void;
|
|
12
|
+
import {
|
|
13
|
+
type FakeSmtp,
|
|
14
|
+
type FakeSmtpOptions,
|
|
15
|
+
startFakeSmtp,
|
|
16
|
+
} from "./test-fake-smtp.js";
|
|
17
|
+
|
|
18
|
+
interface SocketRequest {
|
|
19
|
+
host: string | undefined;
|
|
20
|
+
port: string | number | undefined;
|
|
22
21
|
}
|
|
23
22
|
|
|
24
|
-
const startFakeSmtp = (opts: FakeSmtpOptions = {}): Promise<FakeSmtp> =>
|
|
25
|
-
new Promise((resolve) => {
|
|
26
|
-
const authCommands: string[] = [];
|
|
27
|
-
const server = net.createServer((socket) => {
|
|
28
|
-
if (opts.dropAfterGreeting) {
|
|
29
|
-
socket.destroy();
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
socket.write("220 fake ESMTP\r\n");
|
|
33
|
-
let inData = false;
|
|
34
|
-
socket.on("data", (buffer) => {
|
|
35
|
-
const line = buffer.toString();
|
|
36
|
-
if (inData) {
|
|
37
|
-
if (line.includes("\r\n.\r\n")) {
|
|
38
|
-
inData = false;
|
|
39
|
-
socket.write(opts.dataFinalResponse ?? "250 2.0.0 Ok\r\n");
|
|
40
|
-
}
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
const command = line.slice(0, 4).toUpperCase();
|
|
44
|
-
if (command.startsWith("EHLO") || command.startsWith("HELO")) {
|
|
45
|
-
socket.write("250-fake\r\n250 AUTH PLAIN LOGIN XOAUTH2\r\n");
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
if (command.startsWith("AUTH")) {
|
|
49
|
-
authCommands.push(line.trim());
|
|
50
|
-
socket.write(opts.authResponse ?? "235 2.7.0 Accepted\r\n");
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
if (command.startsWith("MAIL") || command.startsWith("RCPT")) {
|
|
54
|
-
socket.write("250 2.1.0 Ok\r\n");
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
if (command.startsWith("DATA")) {
|
|
58
|
-
socket.write("354 End data\r\n");
|
|
59
|
-
inData = true;
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
if (command.startsWith("QUIT")) {
|
|
63
|
-
socket.write("221 Bye\r\n");
|
|
64
|
-
socket.end();
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
socket.write("250 Ok\r\n");
|
|
68
|
-
});
|
|
69
|
-
});
|
|
70
|
-
server.listen(0, "127.0.0.1", () => {
|
|
71
|
-
const address = server.address();
|
|
72
|
-
const port = typeof address === "object" && address ? address.port : 0;
|
|
73
|
-
resolve({
|
|
74
|
-
port,
|
|
75
|
-
authCommands,
|
|
76
|
-
close: () => server.close(),
|
|
77
|
-
});
|
|
78
|
-
});
|
|
79
|
-
});
|
|
80
|
-
|
|
81
23
|
const configFor = (
|
|
82
24
|
port: number,
|
|
83
25
|
credentials: SmtpCredentials = { kind: "password", password: "secret" },
|
|
@@ -88,6 +30,7 @@ const configFor = (
|
|
|
88
30
|
user: "user@example.com",
|
|
89
31
|
credentials,
|
|
90
32
|
connectionTimeout: 2000,
|
|
33
|
+
getSocket: directSmtpSocket,
|
|
91
34
|
});
|
|
92
35
|
|
|
93
36
|
const testMessage: MailMessage = {
|
|
@@ -121,6 +64,28 @@ describe("sendMail", () => {
|
|
|
121
64
|
assert.ok(result.response);
|
|
122
65
|
});
|
|
123
66
|
|
|
67
|
+
it("talks to the server over the socket the host's factory opens", async () => {
|
|
68
|
+
const server = await spawn();
|
|
69
|
+
const requests: SocketRequest[] = [];
|
|
70
|
+
const result = await sendMail(
|
|
71
|
+
{
|
|
72
|
+
...configFor(server.port),
|
|
73
|
+
host: "smtp.invalid",
|
|
74
|
+
port: 2525,
|
|
75
|
+
getSocket: (request, callback) => {
|
|
76
|
+
requests.push({ host: request.host, port: request.port });
|
|
77
|
+
const connection = net.connect(server.port, "127.0.0.1", () =>
|
|
78
|
+
callback(null, { connection }),
|
|
79
|
+
);
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
testMessage,
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
assert.equal(result.success, true);
|
|
86
|
+
assert.deepEqual(requests, [{ host: "smtp.invalid", port: 2525 }]);
|
|
87
|
+
});
|
|
88
|
+
|
|
124
89
|
it("sends attachments through the transport", async () => {
|
|
125
90
|
const server = await spawn();
|
|
126
91
|
const result = await sendMail(configFor(server.port), {
|
package/src/smtp-client.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import nodemailer from "nodemailer";
|
|
2
|
+
import type { SMTPTransportGetSocket } from "nodemailer/lib/smtp-transport";
|
|
2
3
|
import { type MailMessage, toNodemailerOptions } from "./message-builder.js";
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -31,7 +32,9 @@ export class SmtpConnectionError extends Error {
|
|
|
31
32
|
}
|
|
32
33
|
}
|
|
33
34
|
|
|
34
|
-
export
|
|
35
|
+
export type SmtpGetSocket = SMTPTransportGetSocket;
|
|
36
|
+
|
|
37
|
+
export interface SmtpServerConfig {
|
|
35
38
|
host: string;
|
|
36
39
|
port: number;
|
|
37
40
|
secure: boolean; // true for TLS (465), false for STARTTLS (587)
|
|
@@ -43,12 +46,20 @@ export interface SmtpConfig {
|
|
|
43
46
|
connectionTimeout?: number; // milliseconds, default 30000
|
|
44
47
|
}
|
|
45
48
|
|
|
49
|
+
export const directSmtpSocket: SmtpGetSocket = (_request, callback) => {
|
|
50
|
+
queueMicrotask(() => callback(null, false));
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export interface SmtpConfig extends SmtpServerConfig {
|
|
54
|
+
getSocket: SmtpGetSocket;
|
|
55
|
+
}
|
|
56
|
+
|
|
46
57
|
export interface SendResult {
|
|
47
58
|
success: boolean;
|
|
48
|
-
messageId?: string;
|
|
49
|
-
response?: string;
|
|
50
|
-
error?: Error;
|
|
51
|
-
smtpCode?: number;
|
|
59
|
+
messageId?: string | undefined;
|
|
60
|
+
response?: string | undefined;
|
|
61
|
+
error?: Error | undefined;
|
|
62
|
+
smtpCode?: number | undefined;
|
|
52
63
|
isTransient: boolean;
|
|
53
64
|
}
|
|
54
65
|
|
|
@@ -95,6 +106,7 @@ export const sendMail = async (
|
|
|
95
106
|
connectionTimeout: timeout,
|
|
96
107
|
greetingTimeout: timeout,
|
|
97
108
|
socketTimeout: 300_000,
|
|
109
|
+
getSocket: config.getSocket,
|
|
98
110
|
});
|
|
99
111
|
|
|
100
112
|
return transporter
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import net from "node:net";
|
|
2
|
+
|
|
3
|
+
export interface FakeSmtpOptions {
|
|
4
|
+
authResponse?: string;
|
|
5
|
+
dataFinalResponse?: string;
|
|
6
|
+
dropAfterGreeting?: boolean;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface FakeSmtp {
|
|
10
|
+
port: number;
|
|
11
|
+
authCommands: string[];
|
|
12
|
+
received: string[];
|
|
13
|
+
close: () => void;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const startFakeSmtp = (opts: FakeSmtpOptions = {}): Promise<FakeSmtp> =>
|
|
17
|
+
new Promise((resolve) => {
|
|
18
|
+
const authCommands: string[] = [];
|
|
19
|
+
const received: string[] = [];
|
|
20
|
+
const server = net.createServer((socket) => {
|
|
21
|
+
if (opts.dropAfterGreeting) {
|
|
22
|
+
socket.destroy();
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
socket.write("220 fake ESMTP\r\n");
|
|
26
|
+
let inData = false;
|
|
27
|
+
socket.on("data", (buffer) => {
|
|
28
|
+
const line = buffer.toString();
|
|
29
|
+
if (inData) {
|
|
30
|
+
received.push(line);
|
|
31
|
+
if (line.includes("\r\n.\r\n")) {
|
|
32
|
+
inData = false;
|
|
33
|
+
socket.write(opts.dataFinalResponse ?? "250 2.0.0 Ok\r\n");
|
|
34
|
+
}
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const command = line.slice(0, 4).toUpperCase();
|
|
38
|
+
if (command.startsWith("EHLO") || command.startsWith("HELO")) {
|
|
39
|
+
socket.write("250-fake\r\n250 AUTH PLAIN LOGIN XOAUTH2\r\n");
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (command.startsWith("AUTH")) {
|
|
43
|
+
authCommands.push(line.trim());
|
|
44
|
+
socket.write(opts.authResponse ?? "235 2.7.0 Accepted\r\n");
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (command.startsWith("MAIL") || command.startsWith("RCPT")) {
|
|
48
|
+
socket.write("250 2.1.0 Ok\r\n");
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (command.startsWith("DATA")) {
|
|
52
|
+
socket.write("354 End data\r\n");
|
|
53
|
+
inData = true;
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (command.startsWith("QUIT")) {
|
|
57
|
+
socket.write("221 Bye\r\n");
|
|
58
|
+
socket.end();
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
socket.write("250 Ok\r\n");
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
server.listen(0, "127.0.0.1", () => {
|
|
65
|
+
const address = server.address();
|
|
66
|
+
const port = typeof address === "object" && address ? address.port : 0;
|
|
67
|
+
resolve({
|
|
68
|
+
port,
|
|
69
|
+
authCommands,
|
|
70
|
+
received,
|
|
71
|
+
close: () => server.close(),
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
});
|