Sending Newsletters with dynamic content
Goal: Send a Newsletter that is based on a Mailing Template but also contains content that is dynamically gathered and rendered with CSE.
You can download the whole Kickstarter-based sample application from our GitLab repository: um-public/tutorial-dynamic-newsletter-content
Start a new Kickstarter project
umkickstarter -n de.pinuts.tutorial
cd umkickstarter
# Copy your partner license file to env/devel/cmsbs-conf/cmsbs.license
gradle setup run
Create Newsletter Template
Login to http://localhost:8080/cmsbs as admin
/ admin
and create a new Newsletter Template named offerlist-nl
with the following HTML body:
Dear {firstname},
your top offers for today:
{withcse:renderOfferList:}
Implement RenderEntryCallback
Implement a new RenderEntryCall method that will be called by the withcse directive from within the Newsletter Template just created.
This method should return true
to indicate that the string in this.value
should be rendered into the newsletter body.
cmsbs-conf/cse/plugins/de.pinuts.tutorial/callback/RenderEntryCallback.es6
:
/// <reference path="../../../.vscode.js"/>
import faker from "@de.pinuts.demodata/shared/faker.es6";
function renderOffers(offers) {
return `<ul>${offers.map(renderOffer).join('')}</ul>`;
}
function renderOffer(offer) {
return (
`<li style="margin-bottom: 8px">
<strong>${Strings.encodeXml(offer.productName)}</strong><br/>
${Strings.encodeXml(offer.price)}
</li>`);
}
function fakeOffers() {
const offers = [];
const num = faker.random.number({min: 0, max: 5});
for (let idx = 0; idx < num; idx++) {
offers.push({
productName: faker.commerce.productName(),
price: faker.commerce.price(10, 100, 2, '€')
});
}
return offers;
}
RenderEntryCallback.prototype.renderOfferList = function() {
const offers = fakeOffers();
this.templateContext.doSend = offers.length > 0;
this.value = renderOffers(offers);
return true;
}
Install DemoData plugin
To generate some random content we use the DemoData plugin. Include the dependency into your build.gradle
file and run gradle setup run
once more:
Send Newsletter manually
Now manually create a List named offerlist
and some Entries with email addresses that are subscribed to that list.
Send a Newsletter manually based on the template created earlier.
Goto Tools / Mail Log to verify that emails have been sent and actually do contain the dynamically generated content.
Send Newsletter programmatically
If you also want to start sending the above Newsletter programmatically – i.e. by a Cronjob – you can create an EventFile and trigger sending with the following code snippet.
cmsbs-conf/cse/plugins/de.pinuts.tutorial/shared/SendOfferListNewsletter.es6
:
Having done that you can create a CSE: Call Cronjob to periodically call de.pinuts.tutorial.sendNewsletter()
.