Lifecycle callbacks hook into each phase of an SMTP session. Set them as constructor options or override them on the server instance after construction. CallDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/puiusabin/bun-smtp/llms.txt
Use this file to discover all available pages before exploring further.
callback(null) to accept or callback(error) to reject. To send a custom SMTP error code, set error.responseCode:
onConnect
Called as soon as a client connects, before any SMTP dialogue. Use this to block connections by IP or apply rate limits.Parameters
Call with
null to accept the connection, or an Error to reject it.Example
onSecure
Called after a successful TLS handshake (both implicit TLS and STARTTLS). Use this to inspect client certificates.Parameters
The Bun socket object for this connection.
The session object. After TLS is established,
session.secure is true and session.tlsOptions contains cipher info.Call with
null to proceed, or an Error to close the connection.Example
onAuth
Called when a client sends AUTH. Theauth object varies by method — see the Authentication guide for details.
Parameters
Authentication credentials. Shape depends on the SASL method used.
The session object.
Call with
(null, response) to accept, or (error) to reject.AuthResponse
Stored on
session.user for the rest of the connection. Use this to track the authenticated user.Custom success message returned to the client.
Custom response code (default is
235).XOAUTH2 error challenge data.
Examples
onMailFrom
Called when the client sendsMAIL FROM. Use this to validate the sender address or enforce per-user sending policies.
Parameters
The session object.
session.envelope.mailFrom will be set to this address if you accept.Call with
null to accept, or an Error to reject.Example
onRcptTo
Called once perRCPT TO command. Reject unknown recipients here to avoid accepting mail you cannot deliver.
Parameters
The recipient address. Access DSN parameters via
address.dsn:The session object. Accepted recipients are added to
session.envelope.rcptTo.Call with
null to accept, or an Error to reject.Example
onRcptTo is called separately for each recipient. A message can have multiple recipients if all are accepted.onData
Called when the client begins sending the message body.stream is a ReadableStream<Uint8Array>. You must consume it completely before calling callback.
Parameters
A
ReadableStream<Uint8Array> containing the message data. Has two extra properties set after the stream closes:The session object. Access sender and recipients via
session.envelope.Call after fully consuming the stream.
- First argument:
nullfor success,Errorto reject - Second argument (optional): Custom success message, or an array of per-recipient responses for LMTP
Examples
onClose
Called when a connection closes, regardless of reason. No callback — return value is ignored. Use this for cleanup or logging.Parameters
The session object for the closed connection.
Example
onClose is always called, even if the connection was rejected during onConnect or terminated due to an error.