Skip to main content

Overview

bun-smtp is designed as a drop-in replacement for the smtp-server npm package. Most servers can migrate by simply changing the import and updating the onData callback to work with Web Streams.
bun-smtp maintains API compatibility with smtp-server to make migration seamless. All constructor options, callback signatures, and session properties remain the same.

Installation

Import Statement

Key Differences

1. onData Stream Type

This is the primary change required when migrating. smtp-server passes a Node.js Readable stream, while bun-smtp passes a Web ReadableStream<Uint8Array>.

Discarding the Stream

To discard the message body without reading it:

Stream Properties

Both implementations provide byteLength and sizeExceeded properties:
  • stream.byteLength is set after the stream closes
  • stream.sizeExceeded becomes true when the message exceeds options.size

2. Logger Option Removed

smtp-server accepts a logger option (bunyan-compatible). bun-smtp does not support this option.

3. onSecure Socket Type

The socket argument in onSecure is a Bun Socket, not a Node.js tls.TLSSocket. TLS details are available on session.tlsOptions.

What Stays the Same

Everything else is a direct drop-in replacement:
1

Constructor Options

All options have the same names, types, and default values:
2

Callbacks

All callback signatures remain unchanged:
  • onConnect(session, callback)
  • onAuth(auth, session, callback)
  • onMailFrom(address, session, callback)
  • onRcptTo(address, session, callback)
  • onData(stream, session, callback) — only the stream type changes
  • onClose(session)
3

Auth Objects

Auth object shapes for all methods are identical:
  • PLAIN/LOGIN: { method, username, password }
  • CRAM-MD5: { method, username, challenge, challengeResponse, validatePassword() }
  • XOAUTH2: { method, username, accessToken }
4

Session & Envelope

SMTPSession and SMTPEnvelope structures are identical:
5

Error Handling

Custom SMTP error codes work the same way:
6

TLS Options

All TLS options are identical:
  • key, cert, ca
  • sniOptions
  • requestCert, rejectUnauthorized
  • minVersion, maxVersion
7

Server Methods

  • server.listen(port, callback)
  • server.close(callback)
  • server.updateSecureContext(options)
  • Events: "listening", "close", "error", "connect"

Migration Checklist

1

Update dependencies

2

Update imports

3

Update onData callback

Convert Node.js stream handlers to Web Streams:
4

Remove logger option

If you’re using the logger option, remove it and add logging to individual callbacks.
5

Update onSecure if used

If you’re using onSecure, update TLS property access:
6

Test thoroughly

Test all authentication methods, TLS configurations, and message handling scenarios.

Complete Migration Example

Performance Benefits

By switching to bun-smtp, you get:

Faster Startup

Bun’s fast runtime means your server starts instantly, perfect for serverless deployments.

Lower Memory

Native Web Streams reduce memory overhead compared to Node.js streams.

Built-in TypeScript

No need for @types packages — everything is fully typed out of the box.

Modern APIs

Web-standard APIs make your code more portable and future-proof.

Troubleshooting

This means you’re trying to use Node.js stream methods on a Web ReadableStream. Update your onData callback to use for await...of instead of .on() event listeners.
bun-smtp doesn’t support the logger option. Remove it from your configuration and add logging directly in your callbacks:
In onSecure, the socket is a Bun Socket, not a Node.js TLS socket. Use session.tlsOptions instead:

Need Help?

If you encounter issues during migration:
  • Check the API Reference for detailed documentation
  • Review the source code for implementation details
  • Open an issue on GitHub if you find a compatibility problem