示例#1
0
    settings.list(['smtpHostname', 'smtpPort', 'smtpEncryption', 'smtpUser', 'smtpPass', 'smtpLog', 'smtpDisableAuth', 'smtpMaxConnections', 'smtpMaxMessages', 'smtpSelfSigned', 'pgpPrivateKey', 'pgpPassphrase'], (err, configItems) => {
        if (err) {
            return callback(err);
        }
        module.exports.transport = nodemailer.createTransport({
            pool: true,
            host: configItems.smtpHostname,
            port: Number(configItems.smtpPort) || false,
            secure: configItems.smtpEncryption === 'TLS',
            ignoreTLS: configItems.smtpEncryption === 'NONE',
            auth: configItems.smtpDisableAuth ? false : {
                user: configItems.smtpUser,
                pass: configItems.smtpPass
            },
            debug: !!configItems.smtpLog,
            logger: !configItems.smtpLog ? false : {
                debug: log.info.bind(log, 'Mail'),
                info: log.verbose.bind(log, 'Mail'),
                error: log.info.bind(log, 'Mail')
            },
            maxConnections: Number(configItems.smtpMaxConnections),
            maxMessages: Number(configItems.smtpMaxMessages),
            tls: {
                rejectUnauthorized: !configItems.smtpSelfSigned
            }
        });
        module.exports.transport.use('stream', openpgpEncrypt({
            signingKey: configItems.pgpPrivateKey,
            passphrase: configItems.pgpPassphrase
        }));

        return callback(null, module.exports.transport);
    });
示例#2
0
    settings.list(['smtpHostname', 'smtpPort', 'smtpEncryption', 'smtpUser', 'smtpPass', 'smtpLog', 'smtpDisableAuth', 'smtpMaxConnections', 'smtpMaxMessages', 'smtpSelfSigned', 'pgpPrivateKey', 'pgpPassphrase', 'smtpThrottling'], (err, configItems) => {
        if (err) {
            return callback(err);
        }

        let oldListeners = [];
        if (module.exports.transport) {
            oldListeners = module.exports.transport.listeners('idle');
            module.exports.transport.removeAllListeners('idle');
            module.exports.transport.removeAllListeners('stream');
            module.exports.transport.checkThrottling = null;
        }

        module.exports.transport = nodemailer.createTransport({
            pool: true,
            host: configItems.smtpHostname,
            port: Number(configItems.smtpPort) || false,
            secure: configItems.smtpEncryption === 'TLS',
            ignoreTLS: configItems.smtpEncryption === 'NONE',
            auth: configItems.smtpDisableAuth ? false : {
                user: configItems.smtpUser,
                pass: configItems.smtpPass
            },
            debug: !!configItems.smtpLog,
            logger: !configItems.smtpLog ? false : {
                debug: log.verbose.bind(log, 'Mail'),
                info: log.info.bind(log, 'Mail'),
                error: log.error.bind(log, 'Mail')
            },
            maxConnections: Number(configItems.smtpMaxConnections),
            maxMessages: Number(configItems.smtpMaxMessages),
            tls: {
                rejectUnauthorized: !configItems.smtpSelfSigned
            }
        });
        module.exports.transport.use('stream', openpgpEncrypt({
            signingKey: configItems.pgpPrivateKey,
            passphrase: configItems.pgpPassphrase
        }));

        if (oldListeners.length) {
            log.info('Mail', 'Reattaching %s idle listeners', oldListeners.length);
            oldListeners.forEach(listener => module.exports.transport.on('idle', listener));
        }

        let throttling = Number(configItems.smtpThrottling) || 0;
        if (throttling) {
            // convert to messages/second
            throttling = 1 / (throttling / (3600 * 1000));
        }
        let lastCheck = Date.now();
        module.exports.transport.checkThrottling = function (next) {
            if (!throttling) {
                return next();
            }
            let nextCheck = Date.now();
            let checkDiff = (nextCheck - lastCheck);
            lastCheck = nextCheck;
            if (checkDiff < throttling) {
                log.verbose('Mail', 'Throttling next message in %s sec.', (throttling - checkDiff) / 1000);
                setTimeout(next, throttling - checkDiff);
            } else {
                next();
            }
        };

        caches.cache.delete('sender queue');
        return callback(null, module.exports.transport);
    });