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 Your server should log the received chunks.
curl or telnet. Here’s an example using curl:Understanding the Code
Let’s break down what’s happening in the example:SMTPServer Options
SMTPServer constructor accepts various options. The most important ones for getting started are:
authOptional: Set totrueto allow unauthenticated connectionsonData: Callback invoked when email data is ready to be processed
The onData Callback
onData callback receives:
stream: AReadableStream<Uint8Array>containing the email datasession: AnSMTPSessionobject with connection and envelope informationcallback: Call this withnullon success, or an error object on failure
Starting the Server
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