@zenith-open/zenithcms-plugin-email 1.0.0-beta.10
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/LICENSE +21 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +122 -0
- package/package.json +29 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Aman T Shekar
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ZenithPlugin } from '@zenith-open/zenithcms-types';
|
|
2
|
+
export interface EmailPluginConfig {
|
|
3
|
+
defaultFrom?: string;
|
|
4
|
+
resendKey?: string;
|
|
5
|
+
smtpHost?: string;
|
|
6
|
+
smtpPort?: number;
|
|
7
|
+
smtpUser?: string;
|
|
8
|
+
smtpPass?: string;
|
|
9
|
+
}
|
|
10
|
+
export declare const emailEnginePlugin: (config?: EmailPluginConfig) => ZenithPlugin;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.emailEnginePlugin = void 0;
|
|
7
|
+
const resend_1 = require("resend");
|
|
8
|
+
const nodemailer_1 = __importDefault(require("nodemailer"));
|
|
9
|
+
const emailEnginePlugin = (config) => {
|
|
10
|
+
return {
|
|
11
|
+
id: 'email-engine',
|
|
12
|
+
name: 'Email Engine',
|
|
13
|
+
description: 'Handles email delivery via SMTP or Resend.',
|
|
14
|
+
apply: () => { },
|
|
15
|
+
onReady: async (ctx) => {
|
|
16
|
+
const resolveConfig = async (overrideSettings, siteId) => {
|
|
17
|
+
const activeConfig = {
|
|
18
|
+
resendKey: process.env.RESEND_API_KEY || config?.resendKey,
|
|
19
|
+
smtpHost: process.env.SMTP_HOST || config?.smtpHost,
|
|
20
|
+
smtpPort: parseInt(process.env.SMTP_PORT || '') || config?.smtpPort || 587,
|
|
21
|
+
smtpUser: process.env.SMTP_USER || config?.smtpUser,
|
|
22
|
+
smtpPass: process.env.SMTP_PASS || config?.smtpPass,
|
|
23
|
+
fromEmail: process.env.EMAIL_FROM || config?.defaultFrom || 'Zenith CMS <noreply@zenith.local>'
|
|
24
|
+
};
|
|
25
|
+
if (overrideSettings) {
|
|
26
|
+
if (overrideSettings.smtpHost)
|
|
27
|
+
activeConfig.smtpHost = overrideSettings.smtpHost;
|
|
28
|
+
if (overrideSettings.smtpPort)
|
|
29
|
+
activeConfig.smtpPort = overrideSettings.smtpPort;
|
|
30
|
+
if (overrideSettings.smtpUser)
|
|
31
|
+
activeConfig.smtpUser = overrideSettings.smtpUser;
|
|
32
|
+
if (overrideSettings.smtpPass && overrideSettings.smtpPass !== '[MASKED_CREDENTIAL]')
|
|
33
|
+
activeConfig.smtpPass = overrideSettings.smtpPass;
|
|
34
|
+
if (overrideSettings.fromEmail)
|
|
35
|
+
activeConfig.fromEmail = overrideSettings.fromEmail;
|
|
36
|
+
return activeConfig;
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
const adapter = ctx.adapter;
|
|
40
|
+
if (adapter && adapter.findOne) {
|
|
41
|
+
const query = siteId ? { siteId } : {};
|
|
42
|
+
const settings = await adapter.findOne('z_settings', query);
|
|
43
|
+
if (settings) {
|
|
44
|
+
if (settings.smtpHost)
|
|
45
|
+
activeConfig.smtpHost = settings.smtpHost;
|
|
46
|
+
if (settings.smtpPort)
|
|
47
|
+
activeConfig.smtpPort = settings.smtpPort;
|
|
48
|
+
if (settings.smtpUser)
|
|
49
|
+
activeConfig.smtpUser = settings.smtpUser;
|
|
50
|
+
if (settings.smtpPass && settings.smtpPass !== '[MASKED_CREDENTIAL]')
|
|
51
|
+
activeConfig.smtpPass = settings.smtpPass;
|
|
52
|
+
if (settings.fromEmail)
|
|
53
|
+
activeConfig.fromEmail = settings.fromEmail;
|
|
54
|
+
if (settings.resendKey && settings.resendKey !== '[MASKED_CREDENTIAL]')
|
|
55
|
+
activeConfig.resendKey = settings.resendKey;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
catch (e) {
|
|
60
|
+
ctx.logger.warn('[Email Plugin] Failed to load email config from database settings');
|
|
61
|
+
}
|
|
62
|
+
return activeConfig;
|
|
63
|
+
};
|
|
64
|
+
const anyCtx = ctx;
|
|
65
|
+
if (anyCtx.eventHub) {
|
|
66
|
+
anyCtx.eventHub.on('email:send', async (payload) => {
|
|
67
|
+
const { options, overrideSettings, siteId } = payload;
|
|
68
|
+
const activeConfig = await resolveConfig(overrideSettings, siteId);
|
|
69
|
+
const from = options.from || activeConfig.fromEmail;
|
|
70
|
+
const to = Array.isArray(options.to) ? options.to : [options.to];
|
|
71
|
+
// 1. Try SMTP if configured
|
|
72
|
+
if (activeConfig.smtpHost) {
|
|
73
|
+
try {
|
|
74
|
+
const transporter = nodemailer_1.default.createTransport({
|
|
75
|
+
host: activeConfig.smtpHost,
|
|
76
|
+
port: activeConfig.smtpPort,
|
|
77
|
+
secure: activeConfig.smtpPort === 465,
|
|
78
|
+
auth: (activeConfig.smtpUser && activeConfig.smtpPass) ? {
|
|
79
|
+
user: activeConfig.smtpUser,
|
|
80
|
+
pass: activeConfig.smtpPass
|
|
81
|
+
} : undefined
|
|
82
|
+
});
|
|
83
|
+
await transporter.sendMail({
|
|
84
|
+
from,
|
|
85
|
+
to,
|
|
86
|
+
subject: options.subject,
|
|
87
|
+
html: options.html,
|
|
88
|
+
text: options.text,
|
|
89
|
+
});
|
|
90
|
+
ctx.logger.info(`[Email Plugin] Sent via SMTP to ${to.join(',')}`);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
catch (err) {
|
|
94
|
+
ctx.logger.error(`[Email Plugin] SMTP failed: ${err.message}`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
// 2. Try Resend if configured
|
|
98
|
+
if (activeConfig.resendKey) {
|
|
99
|
+
try {
|
|
100
|
+
const client = new resend_1.Resend(activeConfig.resendKey);
|
|
101
|
+
await client.emails.send({
|
|
102
|
+
from,
|
|
103
|
+
to,
|
|
104
|
+
subject: options.subject,
|
|
105
|
+
html: options.html,
|
|
106
|
+
text: options.text,
|
|
107
|
+
});
|
|
108
|
+
ctx.logger.info(`[Email Plugin] Sent via Resend to ${to.join(',')}`);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
catch (err) {
|
|
112
|
+
ctx.logger.error(`[Email Plugin] Resend failed: ${err.message}`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
ctx.logger.warn('[Email Plugin] No valid SMTP or Resend config found to dispatch email.');
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
ctx.logger.info('[Email Plugin] Initialized and listening for email events.');
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
};
|
|
122
|
+
exports.emailEnginePlugin = emailEnginePlugin;
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zenith-open/zenithcms-plugin-email",
|
|
3
|
+
"version": "1.0.0-beta.10",
|
|
4
|
+
"description": "Email Engine Plugin for Zenith CMS (Nodemailer & Resend)",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"nodemailer": "^6.9.13",
|
|
9
|
+
"resend": "^3.2.0"
|
|
10
|
+
},
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@types/nodemailer": "^6.4.14",
|
|
13
|
+
"typescript": "^5.4.5"
|
|
14
|
+
},
|
|
15
|
+
"peerDependencies": {
|
|
16
|
+
"@zenith-open/zenithcms-core": "1.0.0-beta.10",
|
|
17
|
+
"@zenith-open/zenithcms-types": "1.0.0-beta.10"
|
|
18
|
+
},
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"README.md"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsc"
|
|
28
|
+
}
|
|
29
|
+
}
|