An example of an SMTP server written with Node which dumps to the console any mail sent to it:
var smtp = require('smtp')
smtp.createServer(function(connection) {
connection.on('DATA', function(message) {
console.log('Message from ' + message.sender)
message.on('data', function(data) {
console.log("DATA: " + data)
})
message.on('end', function() {
console.log('EOT')
})
})
}).listen(25)
console.log("SMTP server running on port 25")
Ths main SMTP server constructor. Usually instantiated with
'smtp.createServer'
hostname
, the domain name to reply to aHELO
orEHLO
command with. Defaults tohostname.unconfigured
.maxSize
, the largest message acceptable. Defaults toundefined
.allowPipelining
, whether to allow theESMTP
PIPELINING
extension. Defaults totrue
.authMethods
, an array of authentication types to allow. Defaults toundefined
.
function(connection) { }
Emitted when each client connects
The 'callback'
parameter will be passed each new connection
This object is created internally and returned from the 'connection'
callback.
It is an EventEmitter that implements the Readable Stream
interface, as
well as the following events:
helo
, the an object in the form of{name: "name given at HELO time", extended: true}
sender
, the sender information given in theMAIL FROM:
command.receipients
, the recipients given byRCPT TO:
commands.state
, the current state of the connection. (Currently justwelcome
ordata
)currentMessage
, the EventEmitter that represents the current message stream, if the connection is in thedata
state.
function(helodata) {}
Emitted when a client sends the 'HELO'
or 'EHLO'
commands.
The 'helodata'
object has the following properties:
'name'
, the argument given to the command'valid'
, whether or not the command will be accepted. Can be set to false to reject the helo name. Defaults totrue
.'extended'
,true
if the client is using ESMTP
You will most likely want to use the same callback for both events:
var f = function(helodata) {
if(/invalid/.test(helodata.name)) helodata.valid = false
}
connection.on('HELO', f)
connection.on('EHLO', f)
function(sender) {}
Emitted when a sender is specified by the client.
The 'sender'
object has the following properties:
'address'
, the SMTP address specified'valid'
, whether the address will be accepted. Can be set tofalse
to reject a sender. Defaults totrue
.
function(recipient) {}
Emitted for each recipient the client specifies.
The 'recipient'
object has the following properties:
'address'
, the SMTP address specified'valid'
, whether the address will be accepted. Can be set tofalse
to reject a sender. Defaults totrue
.
function(MessageStream) {}
Emitted when the client begins sending message data.
function() {}
Emitted when the client issues a reset command
function() {}
Emitted when the client quits, before the socket is closed
Emitted when the client issues an expand aliases command
An EventEmitter
implementing the Readable Stream
interface carrying the
message data.
'sender'
, the SMTP sender object'receivers'
, an array of SMTP receiver objects'connection'
, the'smtp.Connection'
object'accepted'
, whether or not confirmation that the message has been received will be sent. Defaults tofalse
. Callaccept()
to accept the message orreject()
to reject it.
Emitted when the message is accepted.
Emitted when the message is rejected.
Accepts the message, so the SMTP daemon will return a 2xx response.
Rejects the message, so the SMTP daemon will return a 4xx/5xx response.