0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00

Added support for plaintext email templates in comments service

refs https://github.com/TryGhost/Team/issues/1664
This commit is contained in:
Kevin Ansfield 2022-07-06 15:44:36 +02:00 committed by Simon Backx
parent a1b224bec8
commit a2ab9f7a20
3 changed files with 50 additions and 12 deletions

View file

@ -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.
`;
};

View file

@ -3,6 +3,7 @@ class CommentsServiceWrapper {
const CommentsService = require('./service'); const CommentsService = require('./service');
const config = require('../../../shared/config'); const config = require('../../../shared/config');
const logging = require('@tryghost/logging');
const models = require('../../models'); const models = require('../../models');
const {GhostMailer} = require('../mail'); const {GhostMailer} = require('../mail');
const mailer = new GhostMailer(); const mailer = new GhostMailer();
@ -11,6 +12,7 @@ class CommentsServiceWrapper {
this.api = new CommentsService({ this.api = new CommentsService({
config, config,
logging,
models, models,
mailer, mailer,
settingsCache, settingsCache,

View file

@ -1,9 +1,11 @@
const {promises: fs} = require('fs'); const {promises: fs} = require('fs');
const path = require('path'); const path = require('path');
const {ghostMailer} = require('../newsletters');
class CommentsService { class CommentsService {
constructor({config, models, mailer, settingsCache, urlUtils}) { constructor({config, logging, models, mailer, settingsCache, urlUtils}) {
this.config = config; this.config = config;
this.logging = logging;
this.models = models; this.models = models;
this.mailer = mailer; this.mailer = mailer;
this.settingsCache = settingsCache; this.settingsCache = settingsCache;
@ -38,15 +40,28 @@ class CommentsService {
return this.supportAddress || this.membersAddress; 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) { async sendNewCommentNotifications(comment) {
this.notifyPostAuthor(comment); this.notifyPostAuthors(comment);
if (comment.get('parent_id')) { if (comment.get('parent_id')) {
this.notifyParentCommentAuthor(comment); this.notifyParentCommentAuthor(comment);
} }
} }
async notifyPostAuthor(comment) { async notifyPostAuthors(comment) {
const post = await this.models.Post.findOne({id: comment.get('post_id')}, {withRelated: ['authors']}); const post = await this.models.Post.findOne({id: comment.get('post_id')}, {withRelated: ['authors']});
for (const author of post.related('authors')) { for (const author of post.related('authors')) {
@ -54,7 +69,6 @@ class CommentsService {
continue; continue;
} }
const from = this.notificationFromAddress;
const to = author.get('email'); const to = author.get('email');
const subject = 'You have a new comment on one of your posts'; const subject = 'You have a new comment on one of your posts';
@ -63,16 +77,16 @@ class CommentsService {
siteUrl: this.urlUtils.getSiteUrl(), siteUrl: this.urlUtils.getSiteUrl(),
siteDomain: this.siteDomain, siteDomain: this.siteDomain,
accentColor: this.settingsCache.get('accent_color'), 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({ this.sendMail({
from,
to, to,
subject, subject,
html, html,
text,
forceTextContent: true forceTextContent: true
}); });
} }
@ -86,11 +100,15 @@ class CommentsService {
} }
} }
async renderEmailTemplate(name, data) { async renderEmailTemplate(templateName, data) {
const templateSource = await fs.readFile(path.join(__dirname, './emails/', `${name}.hbs`), 'utf8'); const htmlTemplateSource = await fs.readFile(path.join(__dirname, './emails/', `${templateName}.hbs`), 'utf8');
const template = this.Handlebars.compile(Buffer.from(templateSource).toString()); 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};
} }
} }