I have a project in progress for an email web app where I am working on replacing certain keywords (first name, last name, email) with the actual properties of the user. As of now, I am going through a list of recipients and modifying the email content to make it personalized with the replaced keywords.
One challenge I'm facing: The forEach loop is skipping over the promise that I'm using before I can implement the regex expression to replace the keywords. How can I pause the loop to ensure all keywords are replaced before moving on 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);
})
});