From a2ab9f7a207e91a14673bbe05b70fb48cc44cfb3 Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Wed, 6 Jul 2022 15:44:36 +0200 Subject: [PATCH] Added support for plaintext email templates in comments service refs https://github.com/TryGhost/Team/issues/1664 --- .../comments/emails/new-comment.txt.js | 18 ++++++++ core/server/services/comments/index.js | 2 + core/server/services/comments/service.js | 42 +++++++++++++------ 3 files changed, 50 insertions(+), 12 deletions(-) create mode 100644 core/server/services/comments/emails/new-comment.txt.js diff --git a/core/server/services/comments/emails/new-comment.txt.js b/core/server/services/comments/emails/new-comment.txt.js new file mode 100644 index 0000000000..233b2850e9 --- /dev/null +++ b/core/server/services/comments/emails/new-comment.txt.js @@ -0,0 +1,18 @@ +module.exports = function (data) { + return ` + Hey there! + + Tap the link below to complete the signup process for ${data.siteTitle}, and be automatically signed in: + + ${data.url} + + For your security, the link will expire in 24 hours time. + + See you soon! + + --- + + Sent to ${data.email} + If you did not make this request, you can simply delete this message. You will not be signed up, and no account will be created for you. + `; +}; diff --git a/core/server/services/comments/index.js b/core/server/services/comments/index.js index 8e3a7e15a1..fdb7d966be 100644 --- a/core/server/services/comments/index.js +++ b/core/server/services/comments/index.js @@ -3,6 +3,7 @@ class CommentsServiceWrapper { const CommentsService = require('./service'); const config = require('../../../shared/config'); + const logging = require('@tryghost/logging'); const models = require('../../models'); const {GhostMailer} = require('../mail'); const mailer = new GhostMailer(); @@ -11,6 +12,7 @@ class CommentsServiceWrapper { this.api = new CommentsService({ config, + logging, models, mailer, settingsCache, diff --git a/core/server/services/comments/service.js b/core/server/services/comments/service.js index 5cd8aadc2b..a65ec04fae 100644 --- a/core/server/services/comments/service.js +++ b/core/server/services/comments/service.js @@ -1,9 +1,11 @@ const {promises: fs} = require('fs'); const path = require('path'); +const {ghostMailer} = require('../newsletters'); class CommentsService { - constructor({config, models, mailer, settingsCache, urlUtils}) { + constructor({config, logging, models, mailer, settingsCache, urlUtils}) { this.config = config; + this.logging = logging; this.models = models; this.mailer = mailer; this.settingsCache = settingsCache; @@ -38,15 +40,28 @@ class CommentsService { return this.supportAddress || this.membersAddress; } + async sendMail(message) { + if (process.env.NODE_ENV !== 'production') { + this.logging.warn(message.text); + } + + let msg = Object.assign({ + from: this.notificationFromAddress, + forceTextContent: true + }, message); + + return ghostMailer.send(msg); + } + async sendNewCommentNotifications(comment) { - this.notifyPostAuthor(comment); + this.notifyPostAuthors(comment); if (comment.get('parent_id')) { this.notifyParentCommentAuthor(comment); } } - async notifyPostAuthor(comment) { + async notifyPostAuthors(comment) { const post = await this.models.Post.findOne({id: comment.get('post_id')}, {withRelated: ['authors']}); for (const author of post.related('authors')) { @@ -54,7 +69,6 @@ class CommentsService { continue; } - const from = this.notificationFromAddress; const to = author.get('email'); const subject = 'You have a new comment on one of your posts'; @@ -63,16 +77,16 @@ class CommentsService { siteUrl: this.urlUtils.getSiteUrl(), siteDomain: this.siteDomain, accentColor: this.settingsCache.get('accent_color'), - fromEmail: from + fromEmail: this.notificationFromAddress }; - const html = await this.renderEmailTemplate('new-comment', templateData); + const {html, text} = await this.renderEmailTemplate('new-comment', templateData); - this.mailer.send({ - from, + this.sendMail({ to, subject, html, + text, forceTextContent: true }); } @@ -86,11 +100,15 @@ class CommentsService { } } - async renderEmailTemplate(name, data) { - const templateSource = await fs.readFile(path.join(__dirname, './emails/', `${name}.hbs`), 'utf8'); - const template = this.Handlebars.compile(Buffer.from(templateSource).toString()); + async renderEmailTemplate(templateName, data) { + const htmlTemplateSource = await fs.readFile(path.join(__dirname, './emails/', `${templateName}.hbs`), 'utf8'); + const htmlTemplate = this.Handlebars.compile(Buffer.from(htmlTemplateSource).toString()); + const textTemplate = require(`./emails/${templateName}.txt.js`); - return template(data); + const html = htmlTemplate(data); + const text = textTemplate(data); + + return {html, text}; } }