Release 0.2.0
This commit is contained in:
1 parent
7a49e66c37
commit
08e8875cb5
38 files changed
+2645
-93
No files matched your search
@@ -0,0 +1,49 @@
|
||||
import { mkdir, writeFile } from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import nodemailer from 'nodemailer';
|
||||
import { getMailConfig, getServerPaths } from './config.js';
|
||||
|
||||
export type MailMessage = {
|
||||
to: string;
|
||||
subject: string;
|
||||
text: string;
|
||||
};
|
||||
|
||||
export async function sendMail(message: MailMessage): Promise<'local' | 'smtp'> {
|
||||
const config = getMailConfig();
|
||||
if (config.transport === 'local') {
|
||||
const outbox = getServerPaths().mailOutbox;
|
||||
await mkdir(outbox, { recursive: true, mode: 0o700 });
|
||||
const filename = `${new Date().toISOString().replaceAll(':', '-')}-${randomUUID()}.json`;
|
||||
await writeFile(
|
||||
path.join(outbox, filename),
|
||||
JSON.stringify({ from: config.from, ...message }, null, 2),
|
||||
{ encoding: 'utf8', mode: 0o600 }
|
||||
);
|
||||
return 'local';
|
||||
}
|
||||
|
||||
if (
|
||||
!config.host ||
|
||||
!config.from ||
|
||||
!Number.isInteger(config.port) ||
|
||||
config.port < 1 ||
|
||||
config.port > 65_535 ||
|
||||
Boolean(config.user) !== Boolean(config.password)
|
||||
) {
|
||||
throw new Error('Die SMTP-Konfiguration ist unvollständig.');
|
||||
}
|
||||
const transporter = nodemailer.createTransport({
|
||||
host: config.host,
|
||||
port: config.port,
|
||||
secure: config.secure,
|
||||
requireTLS: !config.secure,
|
||||
connectionTimeout: 10_000,
|
||||
greetingTimeout: 10_000,
|
||||
socketTimeout: 20_000,
|
||||
auth: config.user ? { user: config.user, pass: config.password } : undefined
|
||||
});
|
||||
await transporter.sendMail({ from: config.from, ...message });
|
||||
return 'smtp';
|
||||
}
|
||||
Reference in new issue
Block a user