fedtools-utilities
Version:
Set of utilites for fedtools within nodejs
60 lines (57 loc) • 2.17 kB
JavaScript
var
_ = require('lodash'),
SMTPServer = require('smtp-server').SMTPServer,
portfinder = require('portfinder'),
DEFAULT_SMTP_PORT = 1024,
processData = function (stream, session, callback) {
stream.pipe(process.stdout);
stream.on('end', callback);
};
/**
* Very rudimentary SMTP server for quick and dirty localhost
* testing. The only options are verbose and port. There is no
* authentication support, no email routing verification, etc.
* Running this mini server simply prints out on the console
* emails that are being received. It does not forward them to
* a valid SMTP server afterward.
*
* @method smtpserver
* @param {Object} options Configuration object.
* @param {Boolean} options.verbose Set to false for hidding SMTP logging
* and only loggingemails themselves.
* (true by default)
* @param {Number} options.port The port to listen to (1024 by default,
* if the specified port is not available,
* the next available port will be used.)
* @param {Function} callback Callback to be called once the server is up and
* running. 1st argument is and error string or null
* on success. 2st argument is an object holding the
* actual port used and a handle on the internal
* server itself (see https://npmjs.com/smtp-server).
*/
exports.smtpserver = function (options, callback) {
var
server,
cfg = {
onData: processData,
authOptional: true
};
// testing if there are no options and just a callback
if (_.isFunction(options)) {
callback = options;
options = {};
}
cfg.logger = _.isBoolean(options.verbose) ? options.verbose : true;
portfinder.basePort = options.port || DEFAULT_SMTP_PORT;
portfinder.getPort(function (err, port) {
if (err) {
return callback(err);
}
server = new SMTPServer(cfg);
server.listen(port);
callback(null, {
server: server,
port: port
});
});
};