I am working on an email web app where I try to replace certain keywords (first name, last name, email) with the actual properties of the user. Currently, I am looping through a list of recipients and editing the email content to be personalized with keywords replaced.
My issue: The forEach loop jumps past the promise used before I can use the regex expression to replace the keywords. How can I pause the loop to ensure all keywords are replaced before proceeding to the next iteration?
recipientList.forEach(function (recipient) {
let setContent = new Promise((resolve,reject) =>{
personalizedContent = replaceAll(emailContent, '[First Name]', firstName);
personalizedContent = replaceAll(emailContent, '[Last Name]', lastName);
personalizedContent = replaceAll(emailContent, '[Email]', recipient.EmailAddress.Address);
resolve(personalizedContent);
})
setContent.then((personalizedContent)=>{
var message = {
"Message": {
"Subject": subject,
"Body": {
"ContentType": "html",
"Content": personalizedContent
},
"ToRecipients": [recipient],
"Attachments": []
},
"SaveToSentItems": "true"
};
postEmail(accessToken,message);
})
});