refactor: emailer.send and emailer.sendToEmail returns Boolean based on message being successfully sent

Note that of course this doesn't mean the message was _delivered_, just sent off.

This will stop issues where emailer errors crash NodeBB
isekai-main
Julian Lam 3 years ago
parent 680e36da67
commit f0e32ff182

@ -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) => {

@ -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) => {

Loading…
Cancel
Save