Node.js based forum software built for the modern web
refactor: emailer.send and emailer.sendToEmail returns Boolean based on message being successfully sent
Julian Lamf0e32ff182c0
diff --git a/src/emailer.js b/src/emailer.jsindex 754b794c05..7b63d3cceb 100644--- a/src/emailer.js+++ b/src/emailer.js@@ -230,7 +230,7 @@ Emailer.send = async (template, uid, params) => {if (process.env.NODE_ENV === 'development') {winston.warn(`uid : ${uid} has no email, not sending "${template}" email.`);}- return;+ return false;}const allowedTpls = ['verify-email', 'welcome', 'registration_accepted', 'reset', 'reset_notify'];@@ -238,7 +238,7 @@ Emailer.send = async (template, uid, params) => {if (process.env.NODE_ENV === 'development') {winston.warn(`uid : ${uid} (${userData.email}) has not confirmed email, not sending "${template}" email.`);}- return;+ return false;}const userSettings = await User.getSettings(uid);// Combined passed-in payload with default values@@ -254,9 +254,10 @@ Emailer.send = async (template, uid, params) => {});if (result.cancel) {- return;+ return false;}- await Emailer.sendToEmail(template, userData.email, userSettings.userLang, params);++ return Emailer.sendToEmail(template, userData.email, userSettings.userLang, params);};Emailer.sendToEmail = async (template, email, language, params) => {@@ -331,13 +332,17 @@ Emailer.sendToEmail = async (template, email, language, params) => {} else {await Emailer.sendViaFallback(data);}++ return true;} catch (err) {if (err.code === 'ENOENT' && usingFallback) {Emailer.fallbackNotFound = true;- throw new Error('[[error:sendmail-not-found]]');+ winston.error(`[emailer/sendToEmail] ${await translator.translate('[[error:sendmail-not-found]]')}`);} else {- throw err;+ winston.error(`[emailer/sendToEmail] ${err.message || err.code || 'Unknown error while sending email.'}`);}++ return false;}};@@ -352,6 +357,7 @@ Emailer.sendViaFallback = async (data) => {winston.verbose(`[emailer] Sending email to uid ${data.uid} (${data.to})`);await Emailer.fallbackTransport.sendMail(data);+ return true;};Emailer.renderAndTranslate = async (template, params, lang) => {diff --git a/test/emailer.js b/test/emailer.jsindex ea19af58f4..190c6fd388 100644--- a/test/emailer.js+++ b/test/emailer.js@@ -45,7 +45,7 @@ describe('emailer', () => {// TODO: test sendmail here at some point- it('plugin hook should work', (done) => {+ it('plugin hook should work', async () => {const error = new Error();Plugins.hooks.register('emailer-test', {@@ -59,12 +59,10 @@ describe('emailer', () => {},});- Emailer.sendToEmail(template, email, language, params, (err) => {- assert.equal(err, error);+ const success = await Emailer.sendToEmail(template, email, language, params);+ assert.strictEqual(success, false);- Plugins.hooks.unregister('emailer-test', 'filter:email.send');- done();- });+ Plugins.hooks.unregister('emailer-test', 'filter:email.send');});it('should build custom template on config change', (done) => {