Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added open events for transactional emails #22287

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ class NewslettersService {
* @param {Object} options.mail
* @param {Object} options.singleUseTokenProvider
* @param {Object} options.urlUtils
* @param {Object} options.settingsCache
* @param {ILimitService} options.limitService
* @param {Object} options.emailAddressService
* @param {Object} options.labs
*/
constructor({NewsletterModel, MemberModel, mail, singleUseTokenProvider, urlUtils, limitService, labs, emailAddressService}) {
constructor({NewsletterModel, MemberModel, mail, singleUseTokenProvider, urlUtils, settingsCache, limitService, labs, emailAddressService}) {
this.NewsletterModel = NewsletterModel;
this.MemberModel = MemberModel;
this.urlUtils = urlUtils;
Expand Down Expand Up @@ -84,6 +85,7 @@ class NewslettersService {
tokenProvider: singleUseTokenProvider,
getSigninURL,
getText,
getOpenTrackingEnabled: () => settingsCache.get('email_track_opens'),
getHTML,
getSubject,
sentry
Expand Down
2 changes: 2 additions & 0 deletions ghost/core/core/server/services/newsletters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const urlUtils = require('../../../shared/url-utils');
const limitService = require('../limits');
const labs = require('../../../shared/labs');
const emailAddressService = require('../email-address');
const settingsCache = require('../../../shared/settings-cache');

const MAGIC_LINK_TOKEN_VALIDITY = 24 * 60 * 60 * 1000;
const MAGIC_LINK_TOKEN_VALIDITY_AFTER_USAGE = 10 * 60 * 1000;
Expand All @@ -22,6 +23,7 @@ module.exports = new NewslettersService({
maxUsageCount: MAGIC_LINK_TOKEN_MAX_USAGE_COUNT
}),
urlUtils,
settingsCache,
limitService,
labs,
emailAddressService: emailAddressService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class SettingsBREADService {
transporter,
tokenProvider: singleUseTokenProvider,
getSigninURL,
getOpenTrackingEnabled: () => settingsCache.get('email_track_opens'),
getText,
getHTML,
getSubject,
Expand Down
43 changes: 42 additions & 1 deletion ghost/core/test/e2e-api/members/send-magic-link.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,5 +377,46 @@ describe('sendMagicLink', function () {
.expectStatus(201);
});
});
});

describe('Open tracking', function () {
it('Can enable open tracking', async function () {
settingsCache.set('email_track_opens', {value: true});

const email = '[email protected]';
await membersAgent.post('/api/send-magic-link')
.body({
email,
emailType: 'signup'
})
.expectEmptyBody()
.expectStatus(201);

// Check email is sent
mockManager.assert.sentEmail({
to: email,
subject: /Complete your sign up to Ghost!/,
'o:tracking-opens': true
});
});

it('Can disable open tracking', async function () {
settingsCache.set('email_track_opens', {value: false});

const email = '[email protected]';
await membersAgent.post('/api/send-magic-link')
.body({
email,
emailType: 'signup'
})
.expectEmptyBody()
.expectStatus(201);

// Check email is sent without open tracking enabled
// TODO: This does not validate that `o:tracking-opens` is unset
mockManager.assert.sentEmail({
to: email,
subject: /Complete your sign up to Ghost!/
});
});
});
});
12 changes: 10 additions & 2 deletions ghost/magic-link/lib/MagicLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class MagicLink {
* @param {MailTransporter} options.transporter
* @param {TokenProvider<Token, TokenData>} options.tokenProvider
* @param {(token: Token, type: string, referrer?: string) => URL} options.getSigninURL
* @param {() => Promise<boolean>} options.getOpenTrackingEnabled
* @param {typeof defaultGetText} [options.getText]
* @param {typeof defaultGetHTML} [options.getHTML]
* @param {typeof defaultGetSubject} [options.getSubject]
Expand All @@ -42,6 +43,7 @@ class MagicLink {
this.transporter = options.transporter;
this.tokenProvider = options.tokenProvider;
this.getSigninURL = options.getSigninURL;
this.getOpenTrackingEnabled = options.getOpenTrackingEnabled;
this.getText = options.getText || defaultGetText;
this.getHTML = options.getHTML || defaultGetHTML;
this.getSubject = options.getSubject || defaultGetSubject;
Expand Down Expand Up @@ -73,12 +75,18 @@ class MagicLink {

const url = this.getSigninURL(token, type, options.referrer);

const info = await this.transporter.sendMail({
const mailObject = {
to: options.email,
subject: this.getSubject(type),
text: this.getText(url, type, options.email),
html: this.getHTML(url, type, options.email)
});
};

if (await this.getOpenTrackingEnabled()) {
mailObject['o:tracking-opens'] = true;
}

const info = await this.transporter.sendMail(mailObject);
Comment on lines +78 to +89
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Missing null check for getOpenTrackingEnabled.

The code doesn't check if this.getOpenTrackingEnabled is defined before calling it, which could cause runtime errors if the function is not provided when instantiating MagicLink.

Add a null check before calling the function:

-        if (await this.getOpenTrackingEnabled()) {
+        if (this.getOpenTrackingEnabled && await this.getOpenTrackingEnabled()) {
            mailObject['o:tracking-opens'] = true;
        }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const mailObject = {
to: options.email,
subject: this.getSubject(type),
text: this.getText(url, type, options.email),
html: this.getHTML(url, type, options.email)
});
};
if (await this.getOpenTrackingEnabled()) {
mailObject['o:tracking-opens'] = true;
}
const info = await this.transporter.sendMail(mailObject);
const mailObject = {
to: options.email,
subject: this.getSubject(type),
text: this.getText(url, type, options.email),
html: this.getHTML(url, type, options.email)
};
- if (await this.getOpenTrackingEnabled()) {
+ if (this.getOpenTrackingEnabled && await this.getOpenTrackingEnabled()) {
mailObject['o:tracking-opens'] = true;
}
const info = await this.transporter.sendMail(mailObject);


return {token, info};
}
Expand Down
1 change: 1 addition & 0 deletions ghost/members-api/lib/members-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ module.exports = function MembersAPI({
tokenProvider,
getSigninURL,
getText,
getOpenTrackingEnabled: () => settingsCache.get('email_track_opens'),
getHTML,
getSubject,
sentry
Expand Down
Loading