2023-01-24 13:43:10 +03:00
|
|
|
const validator = require('@tryghost/validator');
|
|
|
|
const logging = require('@tryghost/logging');
|
|
|
|
|
2022-11-23 13:33:44 +03:00
|
|
|
/**
|
|
|
|
* @typedef {object} EmailData
|
|
|
|
* @prop {string} html
|
|
|
|
* @prop {string} plaintext
|
|
|
|
* @prop {string} subject
|
|
|
|
* @prop {string} from
|
2022-11-30 13:51:58 +03:00
|
|
|
* @prop {string} emailId
|
2022-11-23 13:33:44 +03:00
|
|
|
* @prop {string} [replyTo]
|
|
|
|
* @prop {Recipient[]} recipients
|
2023-05-02 23:43:47 +03:00
|
|
|
* @prop {import("./EmailRenderer").ReplacementDefinition[]} replacementDefinitions
|
2022-11-30 13:51:58 +03:00
|
|
|
*
|
2022-11-23 13:33:44 +03:00
|
|
|
* @typedef {object} IEmailProviderService
|
|
|
|
* @prop {(emailData: EmailData, options: EmailSendingOptions) => Promise<EmailProviderSuccessResponse>} send
|
2022-12-01 15:43:49 +03:00
|
|
|
* @prop {() => number} getMaximumRecipients
|
2022-11-30 13:51:58 +03:00
|
|
|
*
|
2022-11-23 13:33:44 +03:00
|
|
|
* @typedef {object} Post
|
|
|
|
* @typedef {object} Newsletter
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2023-05-02 23:43:47 +03:00
|
|
|
* @typedef {import("./EmailRenderer")} EmailRenderer
|
|
|
|
* @typedef {import("./EmailRenderer").EmailBody} EmailBody
|
2022-11-23 13:33:44 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {object} EmailSendingOptions
|
|
|
|
* @prop {boolean} clickTrackingEnabled
|
|
|
|
* @prop {boolean} openTrackingEnabled
|
2023-02-07 13:01:49 +03:00
|
|
|
* @prop {{get(id: string): EmailBody | null, set(id: string, body: EmailBody): void}} [emailBodyCache]
|
2022-11-23 13:33:44 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2023-05-02 23:43:47 +03:00
|
|
|
* @typedef {import("./EmailRenderer").MemberLike} MemberLike
|
2022-11-23 13:33:44 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {object} Recipient
|
|
|
|
* @prop {string} email
|
|
|
|
* @prop {Replacement[]} replacements
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {object} Replacement
|
2022-11-29 13:27:17 +03:00
|
|
|
* @prop {string} id
|
|
|
|
* @prop {RegExp} token
|
2022-11-23 13:33:44 +03:00
|
|
|
* @prop {string} value
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {object} EmailProviderSuccessResponse
|
|
|
|
* @prop {string} id
|
|
|
|
*/
|
|
|
|
|
|
|
|
class SendingService {
|
|
|
|
#emailProvider;
|
|
|
|
#emailRenderer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {object} dependencies
|
|
|
|
* @param {IEmailProviderService} dependencies.emailProvider
|
|
|
|
* @param {EmailRenderer} dependencies.emailRenderer
|
|
|
|
*/
|
|
|
|
constructor({
|
|
|
|
emailProvider,
|
|
|
|
emailRenderer
|
|
|
|
}) {
|
|
|
|
this.#emailProvider = emailProvider;
|
|
|
|
this.#emailRenderer = emailRenderer;
|
|
|
|
}
|
|
|
|
|
2022-12-01 15:43:49 +03:00
|
|
|
getMaximumRecipients() {
|
|
|
|
return this.#emailProvider.getMaximumRecipients();
|
|
|
|
}
|
|
|
|
|
2022-11-30 13:51:58 +03:00
|
|
|
/**
|
2022-11-23 13:33:44 +03:00
|
|
|
* Send a given post, rendered for a given newsletter and segment to the members provided in the list
|
|
|
|
* @param {object} data
|
|
|
|
* @param {Post} data.post
|
|
|
|
* @param {Newsletter} data.newsletter
|
|
|
|
* @param {string|null} data.segment
|
2022-11-30 13:51:58 +03:00
|
|
|
* @param {string|null} data.emailId
|
2022-11-23 13:33:44 +03:00
|
|
|
* @param {MemberLike[]} data.members
|
|
|
|
* @param {EmailSendingOptions} options
|
|
|
|
* @returns {Promise<EmailProviderSuccessResponse>}
|
|
|
|
*/
|
2022-11-30 13:51:58 +03:00
|
|
|
async send({post, newsletter, segment, members, emailId}, options) {
|
2023-02-07 13:01:49 +03:00
|
|
|
const cacheId = emailId + '-' + (segment ?? 'null');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {EmailBody | null}
|
|
|
|
*/
|
|
|
|
let emailBody = null;
|
|
|
|
|
|
|
|
if (options.emailBodyCache) {
|
|
|
|
emailBody = options.emailBodyCache.get(cacheId);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!emailBody) {
|
|
|
|
emailBody = await this.#emailRenderer.renderBody(
|
|
|
|
post,
|
|
|
|
newsletter,
|
|
|
|
segment,
|
|
|
|
{
|
|
|
|
clickTrackingEnabled: !!options.clickTrackingEnabled
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if (options.emailBodyCache) {
|
|
|
|
options.emailBodyCache.set(cacheId, emailBody);
|
|
|
|
}
|
|
|
|
}
|
2022-11-23 13:33:44 +03:00
|
|
|
|
|
|
|
const recipients = this.buildRecipients(members, emailBody.replacements);
|
|
|
|
return await this.#emailProvider.send({
|
2022-11-29 13:27:17 +03:00
|
|
|
subject: this.#emailRenderer.getSubject(post),
|
2022-11-23 13:33:44 +03:00
|
|
|
from: this.#emailRenderer.getFromAddress(post, newsletter),
|
|
|
|
replyTo: this.#emailRenderer.getReplyToAddress(post, newsletter) ?? undefined,
|
|
|
|
html: emailBody.html,
|
|
|
|
plaintext: emailBody.plaintext,
|
2022-11-30 13:51:58 +03:00
|
|
|
recipients,
|
|
|
|
emailId: emailId,
|
|
|
|
replacementDefinitions: emailBody.replacements
|
2023-02-07 13:01:49 +03:00
|
|
|
}, {
|
|
|
|
clickTrackingEnabled: !!options.clickTrackingEnabled,
|
|
|
|
openTrackingEnabled: !!options.openTrackingEnabled
|
|
|
|
});
|
2022-11-23 13:33:44 +03:00
|
|
|
}
|
2022-11-30 13:51:58 +03:00
|
|
|
|
2022-11-23 13:33:44 +03:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @param {MemberLike[]} members
|
2023-05-02 23:43:47 +03:00
|
|
|
* @param {import("./EmailRenderer").ReplacementDefinition[]} replacementDefinitions
|
2022-11-23 13:33:44 +03:00
|
|
|
* @returns {Recipient[]}
|
|
|
|
*/
|
|
|
|
buildRecipients(members, replacementDefinitions) {
|
|
|
|
return members.map((member) => {
|
|
|
|
return {
|
2023-01-24 13:43:10 +03:00
|
|
|
email: member.email?.trim(),
|
2022-11-23 13:33:44 +03:00
|
|
|
replacements: replacementDefinitions.map((def) => {
|
|
|
|
return {
|
2022-11-29 13:27:17 +03:00
|
|
|
id: def.id,
|
2022-11-23 13:33:44 +03:00
|
|
|
token: def.token,
|
2023-02-13 17:49:43 +03:00
|
|
|
value: def.getValue(member) || ''
|
2022-11-23 13:33:44 +03:00
|
|
|
};
|
|
|
|
})
|
|
|
|
};
|
2023-01-24 13:43:10 +03:00
|
|
|
}).filter((recipient) => {
|
|
|
|
// Remove invalid recipient email addresses
|
|
|
|
const isValidRecipient = validator.isEmail(recipient.email, {legacy: false});
|
|
|
|
if (!isValidRecipient) {
|
|
|
|
logging.warn(`Removed recipient ${recipient.email} from list because it is not a valid email address`);
|
|
|
|
}
|
|
|
|
return isValidRecipient;
|
2022-11-23 13:33:44 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = SendingService;
|