From fa786485c208635c8ccd7855b9f1198af2b13f92 Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Sat, 6 Apr 2024 11:09:32 +0300 Subject: [PATCH] email tweaks --- src/helpers/email.js | 43 +++++++++++++++++++++++++----- src/templates/emails/coverMe.hbs | 2 +- src/templates/emails/main.hbs | 2 +- src/templates/emails/newShifts.hbs | 2 +- 4 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/helpers/email.js b/src/helpers/email.js index 288dc4b..8f5d2ff 100644 --- a/src/helpers/email.js +++ b/src/helpers/email.js @@ -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 = '"Специално Свидетелстване София - тест" '; 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; diff --git a/src/templates/emails/coverMe.hbs b/src/templates/emails/coverMe.hbs index 8f238fe..f95ecd1 100644 --- a/src/templates/emails/coverMe.hbs +++ b/src/templates/emails/coverMe.hbs @@ -1,4 +1,4 @@ -{{!-- Subject: ССС: Нужен е заместник--}} +{{!--Subject: ССС: Нужен е заместник --}}

Търси се зместник за смяна на {{placeName}} за {{dateStr}}!

diff --git a/src/templates/emails/main.hbs b/src/templates/emails/main.hbs index d9959a4..1669557 100644 --- a/src/templates/emails/main.hbs +++ b/src/templates/emails/main.hbs @@ -5,7 +5,7 @@ - Email Template + ССС известия diff --git a/src/templates/emails/newShifts.hbs b/src/templates/emails/newShifts.hbs index 32a1508..5f52bc8 100644 --- a/src/templates/emails/newShifts.hbs +++ b/src/templates/emails/newShifts.hbs @@ -1,4 +1,4 @@ -{{!-- Subject: ССС: Нужен е заместник--}} +{{!-- Subject: ССС: Нови назначени смени--}}

Здравейте, {{publisherFirstName}} {{publisherLastName}}!