From 8d5ef79e9cc4feca96424c46dfdc8b696e37d83d Mon Sep 17 00:00:00 2001 From: Dravere Date: Fri, 6 Oct 2017 13:36:43 +0200 Subject: [PATCH] Smtp emailer change without restart (#5957) * SMTP fallback transport setup during app startup and config update * Verbose logging message to see when SMTP fallback transport is setup --- src/emailer.js | 64 ++++++++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/src/emailer.js b/src/emailer.js index b1ed617445..38535367b9 100644 --- a/src/emailer.js +++ b/src/emailer.js @@ -36,44 +36,28 @@ Emailer.listServices = function (callback) { Emailer._defaultPayload = {}; -Emailer.registerApp = function (expressApp) { - app = expressApp; - - var logo = null; - if (meta.configs.hasOwnProperty('brand:emailLogo')) { - logo = (!meta.config['brand:emailLogo'].startsWith('http') ? nconf.get('url') : '') + meta.config['brand:emailLogo']; - } - - Emailer._defaultPayload = { - url: nconf.get('url'), - site_title: meta.config.title || 'NodeBB', - logo: { - src: logo, - height: meta.config['brand:emailLogo:height'], - width: meta.config['brand:emailLogo:width'], - }, - }; - +Emailer.setupFallbackTransport = function (config) { + winston.verbose('[emailer] Setting up SMTP fallback transport'); // Enable Gmail transport if enabled in ACP - if (parseInt(meta.config['email:smtpTransport:enabled'], 10) === 1) { + if (parseInt(config['email:smtpTransport:enabled'], 10) === 1) { var smtpOptions = {}; - if (meta.config['email:smtpTransport:user'] || meta.config['email:smtpTransport:pass']) { + if (config['email:smtpTransport:user'] || config['email:smtpTransport:pass']) { smtpOptions.auth = { - user: meta.config['email:smtpTransport:user'], - pass: meta.config['email:smtpTransport:pass'], + user: config['email:smtpTransport:user'], + pass: config['email:smtpTransport:pass'], }; } - if (meta.config['email:smtpTransport:service'] === 'nodebb-custom-smtp') { - smtpOptions.port = meta.config['email:smtpTransport:port']; - smtpOptions.host = meta.config['email:smtpTransport:host']; + if (config['email:smtpTransport:service'] === 'nodebb-custom-smtp') { + smtpOptions.port = config['email:smtpTransport:port']; + smtpOptions.host = config['email:smtpTransport:host']; - if (meta.config['email:smtpTransport:security'] === 'NONE') { + if (config['email:smtpTransport:security'] === 'NONE') { smtpOptions.secure = false; smtpOptions.requireTLS = false; smtpOptions.ignoreTLS = true; - } else if (meta.config['email:smtpTransport:security'] === 'STARTTLS') { + } else if (config['email:smtpTransport:security'] === 'STARTTLS') { smtpOptions.secure = false; smtpOptions.requireTLS = true; smtpOptions.ignoreTLS = false; @@ -84,7 +68,7 @@ Emailer.registerApp = function (expressApp) { smtpOptions.ignoreTLS = false; } } else { - smtpOptions.service = meta.config['email:smtpTransport:service']; + smtpOptions.service = config['email:smtpTransport:service']; } transports.smtp = nodemailer.createTransport(smtpOptions); @@ -92,10 +76,34 @@ Emailer.registerApp = function (expressApp) { } else { fallbackTransport = transports.sendmail; } +}; + +Emailer.registerApp = function (expressApp) { + app = expressApp; + + var logo = null; + if (meta.configs.hasOwnProperty('brand:emailLogo')) { + logo = (!meta.config['brand:emailLogo'].startsWith('http') ? nconf.get('url') : '') + meta.config['brand:emailLogo']; + } + + Emailer._defaultPayload = { + url: nconf.get('url'), + site_title: meta.config.title || 'NodeBB', + logo: { + src: logo, + height: meta.config['brand:emailLogo:height'], + width: meta.config['brand:emailLogo:width'], + }, + }; + + Emailer.setupFallbackTransport(meta.config); // Update default payload if new logo is uploaded pubsub.on('config:update', function (config) { if (config) { + if ('email:smtpTransport:enabled' in config) { + Emailer.setupFallbackTransport(config); + } Emailer._defaultPayload.logo.src = config['brand:emailLogo']; Emailer._defaultPayload.logo.height = config['brand:emailLogo:height']; Emailer._defaultPayload.logo.width = config['brand:emailLogo:width'];