added personalization to email campaigns

This commit is contained in:
2ManyProjects 2025-05-01 22:22:58 -05:00
parent 66f0f53d1e
commit fdcf390d48
2 changed files with 43 additions and 6 deletions

View file

@ -540,7 +540,6 @@ router.post('/:id/send', async (req, res, next) => {
}).catch(err => { }).catch(err => {
console.error(`Error sending campaign ${id}:`, err); console.error(`Error sending campaign ${id}:`, err);
}); });
https://click.pstmrk.it/3/localhost%3A3000%2Fapi%2Fsubscribers%2Funsubscribe%3Ftoken%3De5b3b22c-ba7f-4684-afce-bd998d45f0b1/aODm/yg69AQ/AQ/030c5f0a-b979-4327-a121-9c91eeb9eb40/1/tDsgHryVEs
res.json({ res.json({
success: true, success: true,
message: `Campaign scheduled for sending to ${subscribers.length} recipients`, message: `Campaign scheduled for sending to ${subscribers.length} recipients`,
@ -580,12 +579,16 @@ async function sendCampaignEmails(campaignId, campaign, subscribers) {
// Send emails in parallel within each batch // Send emails in parallel within each batch
await Promise.all(batch.map(async (subscriber) => { await Promise.all(batch.map(async (subscriber) => {
try { try {
let personalizedContent = personalizeContent(campaign.content, subscriber);
let personalizedSubject = personalizeContent(campaign.subject, subscriber);
let personalizedPreheader = campaign.preheader ? personalizeContent(campaign.preheader, subscriber) : '';
await emailService.sendCampaignEmail({ await emailService.sendCampaignEmail({
to: subscriber.email, to: subscriber.email,
subject: campaign.subject, subject: personalizedSubject,
preheader: campaign.preheader || '', preheader: personalizedPreheader,
from: `${campaign.from_name} <${campaign.from_email}>`, from: `${campaign.from_name} <${campaign.from_email}>`,
content: campaign.content, content: personalizedContent,
campaignId: campaignId, campaignId: campaignId,
subscriberId: subscriber.id subscriberId: subscriber.id
}); });
@ -1027,4 +1030,32 @@ async function sendCampaignEmails(campaignId, campaign, subscribers) {
}); });
return router; return router;
}; };
/**
* Replace personalization variables in content
*
* @param {string} content - The email content
* @param {Object} subscriber - The subscriber object
* @returns {string} - Content with variables replaced
*/
function personalizeContent(content, subscriber) {
if (!content) return '';
let personalized = content;
if (personalized.includes('{{first_name}}')) {
personalized = personalized.replace(/{{first_name}}/g, subscriber.first_name || '');
}
if (personalized.includes('{{last_name}}')) {
personalized = personalized.replace(/{{last_name}}/g, subscriber.last_name || '');
}
if (personalized.includes('{{email}}')) {
personalized = personalized.replace(/{{email}}/g, subscriber.email || '');
}
return personalized;
}

View file

@ -362,7 +362,13 @@ const EmailCampaignEditor = () => {
<li>Keep your message clear and concise</li> <li>Keep your message clear and concise</li>
<li>Include a strong call-to-action</li> <li>Include a strong call-to-action</li>
<li>Test your email on different devices before sending</li> <li>Test your email on different devices before sending</li>
<li>Personalize content where possible using variables like {`{{first_name}}`}</li> <li>Personalize content where possible using user variables (including the curly braces), currently we support:
<ol>
<li>{`{{first_name}}`}</li>
<li>{`{{last_name}}`}</li>
<li>{`{{email}}`}</li>
</ol>
</li>
<li>Add alt text to images for better accessibility</li> <li>Add alt text to images for better accessibility</li>
</ol> </ol>
</Box> </Box>