email tweaks

This commit is contained in:
Dobromir Popov
2024-04-06 11:09:32 +03:00
parent 09db5ca8b9
commit fa786485c2
4 changed files with 39 additions and 10 deletions

View File

@ -58,11 +58,36 @@ function setResult(result) {
exports.GetLastResult = function () {
return lastResult;
};
function normalizeEmailAddresses(to) {
// If 'to' is already a string, it could be a single email or a CSV of emails
if (typeof to === 'string') {
// Check if 'to' is a CSV string of emails; split it into an array if true
if (to.includes(',')) {
return to.split(/\s*,\s*/); // Split by comma and trim spaces around emails
}
// Otherwise, return it as a single-element array
return [to];
}
// If 'to' is an array, determine if it's an array of strings or objects
if (Array.isArray(to)) {
return to.map(item => {
// If the item is a string, return it directly
if (typeof item === 'string') return item;
// If the item is an object with name and email, format it
if (item.name && item.email) return `"${item.name}" <${item.email}>`;
// If the item is an object but doesn't match expected structure, stringify it
return JSON.stringify(item);
}).join(', ');
}
// Fallback for any other types (unlikely but safe)
return String(to);
}
exports.SendEmail = async function (to, subject, text, html, attachments = []) {
let sender = '"Специално Свидетелстване София - тест" <demo@mwitnessing.com>';
to = Array.isArray(to) ? to : [to];
const emailAddresses = to.map(item => `"${item.name}" <${item.email}>`).join(', ');
const emailAddresses = normalizeEmailAddresses(to)
const message = {
from: sender,
@ -103,15 +128,17 @@ exports.SendEmailHandlebars = async function (to, templateName, model, attachmen
const templateSource = fs.readFileSync(path.join(process.cwd(), 'src', 'templates', 'emails', `${templateName}.hbs`), 'utf8');
// Extract subject and optional text version from the template source
const subjectMatch = templateSource.match(/{{!-- Subject: (.*) --}}/);
const textMatch = templateSource.match(/{{!-- Text: ([\s\S]*?) --}}/);
const subjectMatch = templateSource.match(/{{!--\s*Subject:\s*(.*?)\s*--}}/);
const textMatch = templateSource.match(/{{!--\s*Text:\s*([\s\S]*?)\s*--}}/);
const subject = subjectMatch ? subjectMatch[1].trim() : 'Default Subject';
const textVersion = textMatch ? textMatch[1].trim() : null;
let subject = subjectMatch ? subjectMatch[1].trim() : 'ССС: Известие';
let textVersion = textMatch ? textMatch[1].trim() : null;
// Remove the subject and text annotations from the template source
const cleanTemplateSource = templateSource.replace(/{{!-- Subject: .* --}}/, '').replace(/{{!-- Text: [\s\S]*? --}}/, '');
// const cleanTemplateSource = templateSource
// .replace(/{{!--\s*Subject:.*?--}}\s*/, '')
// .replace(/{{!--\s*Text:.*?--}}\s*/, '');
// Compile the cleaned template
const template = Handlebars.compile(cleanTemplateSource);
@ -122,7 +149,9 @@ exports.SendEmailHandlebars = async function (to, templateName, model, attachmen
const html = mainTemplate({ body: templateHtml });
// Generate a plain text version if not explicitly provided
const text = textVersion || html.replace(/<[^>]*>?/gm, ''); // Simple regex to strip HTML tags. Might need refinement.
let text = textVersion || html.replace(/<[^>]*>?/gm, ''); // Simple regex to strip HTML tags. Might need refinement.
subject = Handlebars.compile(subject)(model);
text = Handlebars.compile(text)(model);
let results = this.SendEmail(to, subject, text, html, attachments);
return results;

View File

@ -1,4 +1,4 @@
{{!-- Subject: ССС: Нужен е заместник--}}
{{!--Subject: ССС: Нужен е заместник --}}
<section>
<h3>Търси се зместник за смяна на {{placeName}} за {{dateStr}}!</h3>

View File

@ -5,7 +5,7 @@
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>Email Template</title>
<title>ССС известия</title>
</head>
<body>

View File

@ -1,4 +1,4 @@
{{!-- Subject: ССС: Нужен е заместник--}}
{{!-- Subject: ССС: Нови назначени смени--}}
<section>
<h2>Здравейте, {{publisherFirstName}} {{publisherLastName}}!</h2>