smtp-server-as-promised

This module provides promisified version of
smtp-server
module. The API is
the same as for smtp-server
, except listen
method which return
Promise
object and callback options which are Promise
objects.
Requirements
This module requires Node >= 6.
Installation
npm install smtp-server-as-promised
Additionally for Typescript:
npm install -D @types/node @types/nodemailer
Transpiling this module with own settings in tsconfig.json
:
{
"compilerOptions": {
"paths": {
"smtp-server-as-promised": ["node_modules/smtp-server-as-promised/src/smtp-server-as-promised"]
}
}
}
Usage
smtp-server-as-promised
can be used like standard smtp-server
module:
const { SMTPServerAsPromised } = require('smtp-server-as-promised')
Typescript:
import SMTPServerAsPromised from 'smtp-server-as-promised'
constructor
const server = new SMTPServerAsPromised(options)
Create new SMTPServer instance.
Example:
const server = new SMTPServerAsPromised({
onConnect, onMailFrom, onData, onError
})
Options are the same as for original smtp-server
constructor. Callback
handlers are Promise
objects or async
functions:
onConnect
async function onConnect (session) {
console.log(`[${session.id}] onConnect`)
}
onAuth
async function onAuth (auth, session) {
if (auth.method === 'PLAIN' && auth.username === 'username' && auth.password === 'password') {
return {user: auth.username}
} else {
throw new Error('Invalid username or password')
}
}
This method must return the object with user
property.
onMailFrom
async function onMailFrom (from, session) {
console.log(`[${session.id}] onMailFrom ${from.address}`)
if (from.address.split('@')[1] === 'spammer.com') {
throw new Error('we do not like spam!')
}
}
An errors can be thrown and then are handled by server in response message.
onRcptTo
async function onRcptTo (to, session) {
console.log(`[${session.id}] onRcptTo ${to.address}`)
if (from.address.split('@')[1] === 'spammer.com') {
throw new Error('we do not like spam!')
}
}
onData
async function onData (stream, session) {
console.log(`[${session.id}] onData started`)
session.messageLength = 0
for (let chunk; (chunk = await stream.read());) {
console.log(`[${session.id}] onData got data chunk ${chunk.length} bytes`)
session.messageLength += chunk.length
}
console.log(`[${session.id}] onData finished after reading ${session.messageLength} bytes`)
}
stream
object is a standard
stream.Readable
object.
Warning: if an error occures in onData
handler, stream have to be consumed
before return from function.
Example:
const { NullWritable } = require('null-writable')
async function onData (stream, session) {
try {
throw new Error('Something bad happened')
} catch (e) {
stream.pipe(new NullWritable())
throw e
}
}
onError
async function onError (e) {
console.log('Server error:', e)
}
listen
const promise = server.listen(options)
Start the server instance. Argument is the same as for
net.listen
method. This method returns promise which resolves to address
value.
Example:
async function main () {
const address = await server.listen({ port: 2525 })
console.log(`Listening on [${address.address}]:${address.port}`)
}
close
const promise = server.close()
Stop the server from accepting new connections.
Example:
async function main () {
await server.close()
console.log(`Server was stopped`)
}
updateSecureContext
server.updateSecureContext(options)
Update TLS secure context.
Example:
server.updateSecureContext({ key: tlsKeyPem })
License
Copyright (c) 2016-2018 Piotr Roszatycki piotr.roszatycki@gmail.com
MIT