From b3a33760ac5f8367726b7441d38fc09a247c155b Mon Sep 17 00:00:00 2001 From: Elena Baidakova Date: Fri, 7 Oct 2022 14:31:05 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Improved=20preview=20text=20on?= =?UTF-8?q?=20member=20alert=20emails=20(#15543)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refs/closes TryGhost/Team#2014 - Updated preview text. - By default email client pulls text from email body for preview. Was added spaces to prevent such behaviour. --- .../lib/email-templates/new-free-signup.hbs | 7 ++++++- .../email-templates/new-paid-cancellation.hbs | 13 ++++++++++--- .../lib/email-templates/new-paid-started.hbs | 7 ++++++- .../lib/email-templates/partials/preview.hbs | 6 ++++++ ghost/staff-service/lib/emails.js | 15 ++++++++++++++- ghost/staff-service/test/staff-service.test.js | 17 ++++++++++++++++- 6 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 ghost/staff-service/lib/email-templates/partials/preview.hbs diff --git a/ghost/staff-service/lib/email-templates/new-free-signup.hbs b/ghost/staff-service/lib/email-templates/new-free-signup.hbs index f1338a862b..bc9061de48 100644 --- a/ghost/staff-service/lib/email-templates/new-free-signup.hbs +++ b/ghost/staff-service/lib/email-templates/new-free-signup.hbs @@ -111,7 +111,12 @@
- + {{#> preview}} + {{#*inline "content"}} + Congratulations! You have a new free member. + {{/inline}} + {{/preview}} + diff --git a/ghost/staff-service/lib/email-templates/new-paid-cancellation.hbs b/ghost/staff-service/lib/email-templates/new-paid-cancellation.hbs index d0bf6d78be..2daaf8e79d 100644 --- a/ghost/staff-service/lib/email-templates/new-paid-cancellation.hbs +++ b/ghost/staff-service/lib/email-templates/new-paid-cancellation.hbs @@ -111,9 +111,16 @@
- + {{#> preview}} + {{#*inline "content"}} + {{#if subscriptionData.cancellationReason}} + Reason: {{subscriptionData.cancellationReason}} + {{else}} + A paid member has just canceled their subscription. + {{/if}} + {{/inline}} + {{/preview}} +
diff --git a/ghost/staff-service/lib/email-templates/new-paid-started.hbs b/ghost/staff-service/lib/email-templates/new-paid-started.hbs index 6d37dd144c..309256b506 100644 --- a/ghost/staff-service/lib/email-templates/new-paid-started.hbs +++ b/ghost/staff-service/lib/email-templates/new-paid-started.hbs @@ -111,7 +111,12 @@
- + {{#> preview}} + {{#*inline "content"}} + {{tierData.name}}: {{tierData.details}} {{#if offerData}}- Offer: {{offerData.name}} - {{offerData.details}}{{/if}} + {{/inline}} + {{/preview}} +
diff --git a/ghost/staff-service/lib/email-templates/partials/preview.hbs b/ghost/staff-service/lib/email-templates/partials/preview.hbs new file mode 100644 index 0000000000..d734ac52ca --- /dev/null +++ b/ghost/staff-service/lib/email-templates/partials/preview.hbs @@ -0,0 +1,6 @@ + diff --git a/ghost/staff-service/lib/emails.js b/ghost/staff-service/lib/emails.js index 1382114456..8ac37d692b 100644 --- a/ghost/staff-service/lib/emails.js +++ b/ghost/staff-service/lib/emails.js @@ -1,6 +1,7 @@ -const {promises: fs} = require('fs'); +const {promises: fs, readFileSync} = require('fs'); const path = require('path'); const moment = require('moment'); +const glob = require('glob'); class StaffServiceEmails { constructor({logging, models, mailer, settingsHelpers, settingsCache, urlUtils}) { @@ -12,6 +13,7 @@ class StaffServiceEmails { this.urlUtils = urlUtils; this.Handlebars = require('handlebars'); + this.registerPartials(); } async notifyFreeMemberSignup(member, options) { @@ -271,6 +273,17 @@ class StaffServiceEmails { return this.mailer.send(msg); } + registerPartials() { + const rootDirname = './email-templates/partials/'; + const files = glob.sync('*.hbs', {cwd: path.join(__dirname, rootDirname)}); + files.forEach((fileName) => { + const name = fileName.replace(/.hbs$/, ''); + const filePath = path.join(__dirname, rootDirname, `${name}.hbs`); + const content = readFileSync(filePath, 'utf8'); + this.Handlebars.registerPartial(name, content); + }); + } + async renderEmailTemplate(templateName, data) { const htmlTemplateSource = await fs.readFile(path.join(__dirname, './email-templates/', `${templateName}.hbs`), 'utf8'); const htmlTemplate = this.Handlebars.compile(Buffer.from(htmlTemplateSource).toString()); diff --git a/ghost/staff-service/test/staff-service.test.js b/ghost/staff-service/test/staff-service.test.js index ae5e5abc29..a7d0516720 100644 --- a/ghost/staff-service/test/staff-service.test.js +++ b/ghost/staff-service/test/staff-service.test.js @@ -417,6 +417,11 @@ describe('StaffService', function () { mailStub.calledWith( sinon.match.has('html', 'Offer') ).should.be.false(); + + // check preview text + mailStub.calledWith( + sinon.match.has('html', sinon.match('Test Tier: $50.00/month')) + ).should.be.true(); }); it('sends paid subscription start alert with percent offer - first payment', async function () { @@ -434,6 +439,11 @@ describe('StaffService', function () { mailStub.calledWith( sinon.match.has('html', sinon.match('first payment')) ).should.be.true(); + + // check preview text + mailStub.calledWith( + sinon.match.has('html', sinon.match('Test Tier: $50.00/month - Offer: Half price - 50% off, first payment')) + ).should.be.true(); }); it('sends paid subscription start alert with fixed type offer - repeating duration', async function () { @@ -561,7 +571,7 @@ describe('StaffService', function () { ).should.be.false(); mailStub.calledWith( - sinon.match.has('html', sinon.match('Reason: Changed my mind! - ')) + sinon.match.has('html', sinon.match('Reason: Changed my mind!')) ).should.be.true(); mailStub.calledWith( sinon.match.has('html', sinon.match('Cancellation reason')) @@ -592,6 +602,11 @@ describe('StaffService', function () { mailStub.calledWith( sinon.match.has('html', sinon.match('Cancellation reason')) ).should.be.false(); + + // check preview text + mailStub.calledWith( + sinon.match.has('html', sinon.match('A paid member has just canceled their subscription.')) + ).should.be.true(); }); }); });