Promisify smtp-server module
npm install smtp-server-as-promised  
This module provides promisified version ofsmtp-server module. The API is
the same as for smtp-server, except listen method which returnPromise
object and callback options which should be replaced with overriden method in
own subclass.
This module requires Node >= 6.
``shell`
npm install smtp-server-as-promised
_Additionally for Typescript:_
`shell`
npm install -D @types/node @types/nodemailer @types/smtp-server
smtp-server-as-promised can be used like standard smtp-server module:
`js
const {SMTPServerAsPromised} = require("smtp-server-as-promised")
class MySMTPServer extends SMTPServerAsPromised {}
`
_Typescript:_
`ts
import SMTPServerAsPromised from "smtp-server-as-promised"
// or
import {SMTPServerAsPromised} from "smtp-server-as-promised"
class MySMTPServer extends SMTPServerAsPromised {}
`
`js`
const server = new MySMTPServer(options)
Create new SMTPServerAsPromised instance.
_Example:_
`js`
const server = new MySMTPServer({
disabledCommands: ["AUTH"],
})
Options are the same as for original smtp-server constructor except callback
handlers that methods of this class should be used instead.
This method can be overriden in subclass.
_Example:_
`js[${session.id}] onConnect
class MySMTPServer extends SMTPServerAsPromised {
async onConnect(session) {
console.log()`
}
}
This method can be overriden in subclass.
_Example:_
`js`
class MySMTPServer extends SMTPServerAsPromised {
async 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.
This method can be overriden in subclass.
_Example:_
`js[${session.id}] onMailFrom ${from.address}
class MySMTPServer extends SMTPServerAsPromised {
async onMailFrom(from, session) {
console.log()`
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.
This method can be overriden in subclass.
_Example:_
`js[${session.id}] onRcptTo ${to.address}
class MySMTPServer extends SMTPServerAsPromised {
async onRcptTo(to, session) {
console.log()`
if (from.address.split("@")[1] === "spammer.com") {
throw new Error("we do not like spam!")
}
}
}
This method can be overriden in subclass.
_Example:_
`js[${session.id}] onData started
class MySMTPServer extends SMTPServerAsPromised {
async onData(stream, session) {
console.log()`
if (stream.sizeExceeded) throw new Error("Message too big")
stream.pipe(process.stdout)
}
}
stream object is astream.DuplexbyteLength
object with additional properties: and sizeExceeded.
The method blocks SMTP session until stream is finished. It breaks session ifstream is already finished.
If the method throws an error then the stream is silently consumed to
prevent SMTP stream to be blocked.
This method can be overriden in subclass.
_Example:_
`js`
class MySMTPServer extends SMTPServerAsPromised {
async onError(error) {
console.log("Server error:", error)
}
}
`js`
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:_
`jsListening on [${address.address}]:${address.port}
async function main() {
const address = await server.listen({port: 2525})
console.log()`
}
`js`
const promise = server.close()
Stop the server from accepting new connections.
_Example:_
`jsServer was stopped
async function main() {
// ...
await server.close()
console.log()`
}
`js`
server.updateSecureContext(options)
Update TLS secure context.
_Example:_
`js`
server.updateSecureContext({key: tlsKeyPem})
`js``
await connection.destroy()
Manually free resources taken by server.
Copyright (c) 2016-2019 Piotr Roszatycki