@ -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* --}}/ ) ;
cons t subject = subjectMatch ? subjectMatch [ 1 ] . trim ( ) : 'Default Subject ' ;
cons t textVersion = textMatch ? textMatch [ 1 ] . trim ( ) : null ;
le t subject = subjectMatch ? subjectMatch [ 1 ] . trim ( ) : 'С С С : Известие ' ;
le t 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
cons t text = textVersion || html . replace ( /<[^>]*>?/gm , '' ) ; // Simple regex to strip HTML tags. Might need refinement.
le t 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 ;