@zintrust/mail-nodemailer 0.1.8

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.
@@ -0,0 +1,31 @@
1
+ export type NodemailerMailConfig = {
2
+ driver: 'nodemailer';
3
+ host: string;
4
+ port: number;
5
+ username: string;
6
+ password: string;
7
+ secure: boolean | 'starttls';
8
+ };
9
+ export type MailAddress = {
10
+ email: string;
11
+ name?: string;
12
+ };
13
+ export type MailAttachment = {
14
+ filename: string;
15
+ content: Buffer;
16
+ };
17
+ export type MailMessage = {
18
+ to: string | string[];
19
+ from: MailAddress;
20
+ subject: string;
21
+ text: string;
22
+ html?: string;
23
+ attachments?: MailAttachment[];
24
+ };
25
+ export declare const NodemailerDriver: Readonly<{
26
+ send(config: NodemailerMailConfig, message: MailMessage): Promise<{
27
+ ok: boolean;
28
+ messageId?: string;
29
+ }>;
30
+ }>;
31
+ export default NodemailerDriver;
package/dist/index.js ADDED
@@ -0,0 +1,54 @@
1
+ import { ErrorFactory } from '@zintrust/core';
2
+ async function importNodemailer() {
3
+ return (await import('nodemailer'));
4
+ }
5
+ function normalizeRecipients(to) {
6
+ return Array.isArray(to) ? to : [to];
7
+ }
8
+ function formatFrom(from) {
9
+ const name = (from.name ?? '').trim();
10
+ return name === '' ? from.email : `${name} <${from.email}>`;
11
+ }
12
+ function mapSecure(value) {
13
+ if (value === 'starttls')
14
+ return { secure: false, requireTLS: true };
15
+ return { secure: Boolean(value) };
16
+ }
17
+ export const NodemailerDriver = Object.freeze({
18
+ async send(config, message) {
19
+ const host = (config.host ?? '').trim();
20
+ const port = Number(config.port);
21
+ if (host === '') {
22
+ throw ErrorFactory.createConfigError('Nodemailer: missing MAIL_HOST');
23
+ }
24
+ if (!Number.isFinite(port) || port <= 0) {
25
+ throw ErrorFactory.createConfigError('Nodemailer: invalid MAIL_PORT');
26
+ }
27
+ if (message.from.email.trim() === '') {
28
+ throw ErrorFactory.createConfigError('Mail: missing from.email');
29
+ }
30
+ const { createTransport } = await importNodemailer();
31
+ const tls = mapSecure(config.secure);
32
+ const authUser = (config.username ?? '').trim();
33
+ const authPass = (config.password ?? '').trim();
34
+ const transport = createTransport({
35
+ host,
36
+ port,
37
+ secure: tls.secure,
38
+ requireTLS: tls.requireTLS,
39
+ auth: authUser === '' ? undefined : { user: authUser, pass: authPass },
40
+ });
41
+ const info = await transport.sendMail({
42
+ from: formatFrom(message.from),
43
+ to: normalizeRecipients(message.to).join(','),
44
+ subject: message.subject,
45
+ text: message.text,
46
+ html: typeof message.html === 'string' && message.html !== '' ? message.html : undefined,
47
+ attachments: message.attachments?.map((a) => ({ filename: a.filename, content: a.content })) ??
48
+ undefined,
49
+ });
50
+ const messageId = typeof info?.messageId === 'string' ? info.messageId : undefined;
51
+ return { ok: true, messageId };
52
+ },
53
+ });
54
+ export default NodemailerDriver;
@@ -0,0 +1,8 @@
1
+ type Registry = {
2
+ register: (driver: string, handler: (cfg: unknown, msg: unknown) => Promise<{
3
+ ok: boolean;
4
+ messageId?: string;
5
+ }>) => void;
6
+ };
7
+ export declare function registerNodemailerDriver(registry: Registry): void;
8
+ export {};
@@ -0,0 +1,10 @@
1
+ import { NodemailerDriver } from './index.js';
2
+ export function registerNodemailerDriver(registry) {
3
+ registry.register('nodemailer', async (config, message) => {
4
+ return NodemailerDriver.send(config, message);
5
+ });
6
+ }
7
+ const core = (await import('@zintrust/core'));
8
+ if (core.MailDriverRegistry !== undefined) {
9
+ registerNodemailerDriver(core.MailDriverRegistry);
10
+ }
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@zintrust/mail-nodemailer",
3
+ "version": "0.1.8",
4
+ "private": false,
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "default": "./dist/index.js"
15
+ },
16
+ "./register": {
17
+ "types": "./dist/register.d.ts",
18
+ "default": "./dist/register.js"
19
+ }
20
+ },
21
+ "engines": {
22
+ "node": ">=20.0.0"
23
+ },
24
+ "peerDependencies": {
25
+ "@zintrust/core": "^0.1.8"
26
+ },
27
+ "publishConfig": {
28
+ "access": "public"
29
+ },
30
+ "dependencies": {
31
+ "nodemailer": "^7.0.11"
32
+ },
33
+ "scripts": {
34
+ "build": "tsc -p tsconfig.json",
35
+ "prepublishOnly": "npm run build"
36
+ }
37
+ }