Skip to content
Promisified version of smtp-server module
TypeScript JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples
src
test
.eslintignore
.eslintrc.yml
.gitignore
.markdownlint.json
.mocharc.yml
.npmignore
.nycrc.json
.prettierrc.yml
.travis.yml
CHANGELOG.md
LICENSE
README.md
index.js
package.json
renovate.json
tsconfig.json
tslint.yml

README.md

smtp-server-as-promised

Build Status Coverage Status npm

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 should be replaced with overriden method in own subclass.

Requirements

This module requires Node >= 6.

Installation

npm install smtp-server-as-promised

Additionally for Typescript:

npm install -D @types/node @types/nodemailer @types/smtp-server

Usage

smtp-server-as-promised can be used like standard smtp-server module:

const {SMTPServerAsPromised} = require("smtp-server-as-promised")

class MySMTPServer extends SMTPServerAsPromised {}

Typescript:

import SMTPServerAsPromised from "smtp-server-as-promised"
// or
import {SMTPServerAsPromised} from "smtp-server-as-promised"

class MySMTPServer extends SMTPServerAsPromised {}

constructor

const server = new MySMTPServer(options)

Create new SMTPServerAsPromised instance.

Example:

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.

onConnect

This method can be overriden in subclass.

Example:

class MySMTPServer extends SMTPServerAsPromised {
  async onConnect(session) {
    console.log(`[${session.id}] onConnect`)
  }
}

onAuth

This method can be overriden in subclass.

Example:

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.

onMailFrom

This method can be overriden in subclass.

Example:

class MySMTPServer extends SMTPServerAsPromised {
  async 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

This method can be overriden in subclass.

Example:

class MySMTPServer extends SMTPServerAsPromised {
  async 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

This method can be overriden in subclass.

Example:

class MySMTPServer extends SMTPServerAsPromised {
  async onData(stream, session) {
    console.log(`[${session.id}] onData started`)
    if (stream.sizeExceeded) throw new Error("Message too big")
    stream.pipe(process.stdout)
  }
}

stream object is a stream.Duplex object with additional properties: byteLength and sizeExceeded.

The method blocks SMTP session until stream is finished. It breaks session if stream is already finished.

If the method throws an error then the stream is silently consumed to prevent SMTP stream to be blocked.

onError

This method can be overriden in subclass.

Example:

class MySMTPServer extends SMTPServerAsPromised {
  async onError(error) {
    console.log("Server error:", error)
  }
}

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})

destroy

await connection.destroy()

Manually free resources taken by server.

License

Copyright (c) 2016-2019 Piotr Roszatycki piotr.roszatycki@gmail.com

MIT

You can’t perform that action at this time.