Skip to main content

Quickstart

Get your first SMTP server running with bun-smtp in just a few steps.

Installation

1

Install bun-smtp

Add bun-smtp to your project:
Make sure you have Bun 1.2.0 or higher installed. Check your version with bun --version.
2

Create your server file

Create a new file called server.ts:
server.ts
This example sets authOptional: true to accept unauthenticated connections. In production, you should implement proper authentication.
3

Run your server

Start the server with Bun:
You should see:
4

Test your server

Test your server using curl or telnet. Here’s an example using curl:
Your server should log the received chunks.

Understanding the Code

Let’s break down what’s happening in the example:

SMTPServer Options

The SMTPServer constructor accepts various options. The most important ones for getting started are:
  • authOptional: Set to true to allow unauthenticated connections
  • onData: Callback invoked when email data is ready to be processed

The onData Callback

The onData callback receives:
  • stream: A ReadableStream<Uint8Array> containing the email data
  • session: An SMTPSession object with connection and envelope information
  • callback: Call this with null on success, or an error object on failure
The stream must be consumed before calling the callback. Use a for await loop to read all chunks.

Starting the Server

The listen() method starts the server on the specified port. It returns a Promise that resolves when the server is ready.

Next Steps

Now that you have a basic server running, you can:

Add Authentication

Secure your server with SASL authentication

Enable TLS

Encrypt connections with STARTTLS or implicit TLS

Handle Messages

Save emails to a database or forward them

Configure Options

Explore all available server options

Common Patterns

Save Email to File

Access Envelope Information

Reject Invalid Senders

Always validate and sanitize email addresses and data before processing them to prevent security vulnerabilities.