onConnect
Called when a new client connection is established, before the greeting is sent.Signature
Parameters
Session object containing connection information
Callback function
(err). Call with an Error to reject the connection.Example
The default implementation calls
callback() immediately.onSecure
Called after a TLS connection is established (either via STARTTLS or direct TLS).Signature
Parameters
The TLS socket object
Session object with TLS information
Callback function
(err). Call with an Error to close the connection.Example
The default implementation calls
callback() immediately.onAuth
Called when a client attempts authentication.Signature
Parameters
Authentication object containing:
Callback function
(err, response).- On success:
callback(null, { user: userData }) - On failure:
callback(new Error('Authentication failed'))
Response Object
User data to store in
session.user. Can be username, user ID, or user object.For XOAUTH2: Object with
status, schemes, and scope properties for continuation.Custom error message to return to client.
Examples
onMailFrom
Called for the MAIL FROM command to validate the sender address.Signature
Parameters
Address object containing:
Callback function
(err). Call with an Error to reject the sender.Error Properties
SMTP response code (e.g.,
550 for permanent failure, 450 for temporary)Examples
The default implementation accepts all sender addresses.
onRcptTo
Called for the RCPT TO command to validate each recipient address.Signature
Parameters
Address object (same structure as onMailFrom)
Session object including
session.envelope.mailFromCallback function
(err). Call with an Error to reject the recipient.Examples
The default implementation accepts all recipient addresses.
onData
Called for the DATA command to process the message content.Signature
Parameters
Readable stream of the message content
Session object with complete envelope information
Callback function
(err, message). Call when message processing is complete.err: Error object if processing failedmessage: Success message string (or array for LMTP)
Examples
onClose
Called when a connection is closed.Signature
Parameters
Session object for the closed connection
This method does not take a callback and is called asynchronously.
Example
The default implementation does nothing.
See Also
- Session Object - Complete session object structure
- Configuration Options - Server configuration
- Events - Server events