@nka212bg/backend-utils 0.1.6 → 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.
- package/mailSmsTools/index.js +24 -25
- package/misc.js +28 -0
- package/package.json +1 -1
package/mailSmsTools/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const { existsSync, readFileSync, writeFileSync } = require("fs");
|
|
2
|
+
const { strToEmailsArray, handlebars } = require("../misc");
|
|
2
3
|
const { location } = require("../location");
|
|
3
4
|
|
|
4
5
|
const nodemailer = require("nodemailer");
|
|
@@ -21,11 +22,11 @@ const mailTransport = nodemailer.createTransport({
|
|
|
21
22
|
},
|
|
22
23
|
});
|
|
23
24
|
|
|
24
|
-
exports.sendEmail = async ({ email, content, subject, attachments, unsubscribe, userIp } = {}) => {
|
|
25
|
+
exports.sendEmail = async ({ email, content, subject, attachments, unsubscribe, userIp, logoUrl } = {}) => {
|
|
25
26
|
if (!content) return { error: "missing_content" };
|
|
26
27
|
if (!subject || subject === "") subject = email_content.substring(0, 25) + "... ";
|
|
27
28
|
|
|
28
|
-
const emailArray =
|
|
29
|
+
const emailArray = strToEmailsArray(email || process.env.SMTP_USER);
|
|
29
30
|
if (!emailArray.length) {
|
|
30
31
|
return { error: "missing_email" };
|
|
31
32
|
//
|
|
@@ -54,9 +55,29 @@ exports.sendEmail = async ({ email, content, subject, attachments, unsubscribe,
|
|
|
54
55
|
return;
|
|
55
56
|
}
|
|
56
57
|
|
|
58
|
+
const html = `
|
|
59
|
+
<div style="padding: 20px">
|
|
60
|
+
<table border="0" cellpadding="0" cellspacing="0" width="100%" role="presentation" style="margin: 0; max-width: 500px; padding: 0; font-family: system-ui; font-size: 14px; background-color: transparent">
|
|
61
|
+
<tbody>
|
|
62
|
+
<tr>
|
|
63
|
+
<td style="padding-bottom: 6px">
|
|
64
|
+
<img src="${logoUrl || `${process.env.BASE_URL}/logo_black.png`}" style="height: 36px; background-size: contain; background-repeat: no-repeat" />
|
|
65
|
+
</td>
|
|
66
|
+
</tr>
|
|
67
|
+
<tr><td style="padding-bottom: 20px">${content}</td></tr>
|
|
68
|
+
<tr>
|
|
69
|
+
<td>
|
|
70
|
+
<a href="${process.env.BASE_URL}" target="_blank" style="text-decoration: none; color: gray; font-size: 12px">${new Date().getFullYear()} ${process.env.BASE_URL}</a>
|
|
71
|
+
</td>
|
|
72
|
+
</tr>
|
|
73
|
+
</tbody>
|
|
74
|
+
</table>
|
|
75
|
+
</div>`;
|
|
76
|
+
|
|
57
77
|
const mailOptions = {
|
|
58
78
|
from: process.env.SMTP_USER,
|
|
59
|
-
html:
|
|
79
|
+
// html: handlebars({ template: defaultTemplate, content, subject, logoUrl: `${process.env.BASE_URL}/logo_black.png` }),
|
|
80
|
+
html,
|
|
60
81
|
subject,
|
|
61
82
|
to: e,
|
|
62
83
|
attachments,
|
|
@@ -138,25 +159,3 @@ exports.subscriptionState = {
|
|
|
138
159
|
if (change) writeFileSync(emailListFilePath, JSON.stringify(EMAILS_LIST));
|
|
139
160
|
},
|
|
140
161
|
};
|
|
141
|
-
|
|
142
|
-
//HELPERS
|
|
143
|
-
exports.strToEmailsArray = (emailString) => {
|
|
144
|
-
try {
|
|
145
|
-
emailString = JSON.stringify(emailString);
|
|
146
|
-
} catch {}
|
|
147
|
-
|
|
148
|
-
return [...new Set(((emailString + "").match(/((?=\w)[^ \?'<>"\n\t]+)@(\w[^ \?'<>"\n\t]+)/gi) || []).map((e) => e.trim().toLowerCase()))].sort();
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
exports.handlebars = ({ template, ...params } = {}) => {
|
|
152
|
-
const placeholders = template.match(/(\{\{)(.*?)(\}\})/g);
|
|
153
|
-
|
|
154
|
-
for (const p in params) {
|
|
155
|
-
const placeholder = placeholders.find((e) => e.includes(p));
|
|
156
|
-
if (!placeholder) continue;
|
|
157
|
-
|
|
158
|
-
template = template.replace(RegExp(placeholder, "g"), params[p]);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
return template;
|
|
162
|
-
};
|
package/misc.js
CHANGED
|
@@ -462,3 +462,31 @@ exports.urlMetadata = async ({ url }) => {
|
|
|
462
462
|
return { error: String(error) };
|
|
463
463
|
}
|
|
464
464
|
};
|
|
465
|
+
|
|
466
|
+
exports.strToEmailsArray = (emailString) => {
|
|
467
|
+
try {
|
|
468
|
+
emailString = JSON.stringify(emailString);
|
|
469
|
+
} catch {}
|
|
470
|
+
|
|
471
|
+
// return [...new Set(((emailString + "").match(/((?=\w)[^ \?'<>"\n\t]+)@(\w[^ \?'<>"\n\t]+)/gi) || []).map((e) => e.trim().toLowerCase()))].sort();
|
|
472
|
+
return [
|
|
473
|
+
...new Set(
|
|
474
|
+
String(emailString)
|
|
475
|
+
.toLowerCase()
|
|
476
|
+
.match(/\w[\w\.-]+\w@\w+((\.\w+){1,})+\w+/gi)
|
|
477
|
+
),
|
|
478
|
+
].sort();
|
|
479
|
+
};
|
|
480
|
+
|
|
481
|
+
exports.handlebars = ({ template, ...params } = {}) => {
|
|
482
|
+
const placeholders = template.match(/(\{\{)(.*?)(\}\})/g);
|
|
483
|
+
|
|
484
|
+
for (const p in params) {
|
|
485
|
+
const placeholder = placeholders.find((e) => e.includes(p));
|
|
486
|
+
if (!placeholder) continue;
|
|
487
|
+
|
|
488
|
+
template = template.replace(RegExp(placeholder, "g"), params[p]);
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
return template;
|
|
492
|
+
};
|