diff --git a/ghost/magic-link/index.js b/ghost/magic-link/index.js index f0ec42deec..3f08b9edd3 100644 --- a/ghost/magic-link/index.js +++ b/ghost/magic-link/index.js @@ -42,6 +42,19 @@ function defaultGetHTML(url, type, email) { return `Click here to ${msg} This msg was sent to ${email}`; } +/** + * defaultGetSubject + * + * @param {string} type - The type of email to send e.g. signin, signup + * @returns {string} subject - The subject of an email to send + */ +function defaultGetSubject(type) { + if (type === 'signup') { + return `Signup!`; + } + return `Signin!`; +} + /** * MagicLink * @constructor @@ -53,6 +66,7 @@ function defaultGetHTML(url, type, email) { * @param {(token: JSONWebToken, type: string) => URL} options.getSigninURL * @param {typeof defaultGetText} [options.getText] * @param {typeof defaultGetHTML} [options.getHTML] + * @param {typeof defaultGetSubject} [options.getSubject] */ function MagicLink(options) { if (!options || !options.transporter || !options.publicKey || !options.privateKey || !options.getSigninURL) { @@ -64,6 +78,7 @@ function MagicLink(options) { this.getSigninURL = options.getSigninURL; this.getText = options.getText || defaultGetText; this.getHTML = options.getHTML || defaultGetHTML; + this.getSubject = options.getSubject || defaultGetSubject; } /** @@ -92,6 +107,7 @@ MagicLink.prototype.sendMagicLink = async function sendMagicLink(options) { const info = await this.transporter.sendMail({ to: options.email, + subject: this.getSubject(type), text: this.getText(url, type, options.email), html: this.getHTML(url, type, options.email) }); diff --git a/ghost/magic-link/test/index.test.js b/ghost/magic-link/test/index.test.js index 6e2a183e83..828d364105 100644 --- a/ghost/magic-link/test/index.test.js +++ b/ghost/magic-link/test/index.test.js @@ -29,6 +29,7 @@ describe('MagicLink', function () { getSigninURL: sandbox.stub().returns('FAKEURL'), getText: sandbox.stub().returns('SOMETEXT'), getHTML: sandbox.stub().returns('SOMEHTML'), + getSubject: sandbox.stub().returns('SOMESUBJECT'), transporter: { sendMail: sandbox.stub().resolves() } @@ -49,6 +50,7 @@ describe('MagicLink', function () { should.ok(options.transporter.sendMail.calledOnce); should.equal(options.transporter.sendMail.firstCall.args[0].to, args.email); + should.equal(options.transporter.sendMail.firstCall.args[0].subject, options.getSubject.firstCall.returnValue); should.equal(options.transporter.sendMail.firstCall.args[0].text, options.getText.firstCall.returnValue); should.equal(options.transporter.sendMail.firstCall.args[0].html, options.getHTML.firstCall.returnValue); });