Skip to content

Commit 044967e

Browse files
committed
fix(email): respect hosts file for SMTP connections
1 parent c04172a commit 044967e

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

next-env.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// <reference types="next" />
22
/// <reference types="next/image-types/global" />
3-
import "./.next/dev/types/routes.d.ts";
3+
import "./.next/types/routes.d.ts";
44

55
// NOTE: This file should not be edited
66
// see https://nextjs.org/docs/pages/api-reference/config/typescript for more information.

server/lib/email/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
import type { NotificationAgentEmail } from '@server/lib/settings';
22
import { getSettings } from '@server/lib/settings';
33
import Email from 'email-templates';
4+
import net from 'node:net';
45
import nodemailer from 'nodemailer';
6+
import type SMTPTransport from 'nodemailer/lib/smtp-transport';
57
import { URL } from 'url';
68
import { openpgpEncrypt } from './openpgpEncrypt';
79

10+
const getSocket: SMTPTransport.Options['getSocket'] = (options, callback) => {
11+
const socket = net.connect({
12+
host: options.host ?? 'localhost',
13+
port: options.port ?? 587,
14+
localAddress: options.localAddress,
15+
});
16+
const onError = (error: Error) => callback(error, undefined);
17+
const onConnect = () => {
18+
socket.removeListener('error', onError);
19+
callback(null, { connection: socket });
20+
};
21+
22+
socket.once('error', onError);
23+
socket.once('connect', onConnect);
24+
};
25+
826
class PreparedEmail extends Email {
927
public constructor(settings: NotificationAgentEmail, pgpKey?: string) {
1028
const { applicationUrl } = getSettings().main;
@@ -28,6 +46,7 @@ class PreparedEmail extends Email {
2846
pass: settings.options.authPass,
2947
}
3048
: undefined,
49+
getSocket: net.isIP(settings.options.smtpHost) ? undefined : getSocket,
3150
});
3251

3352
if (pgpKey) {

0 commit comments

Comments
 (0)