node-smtp v0.1.0
  1. node-smtp(3)
  2. node-smtp(3)

node-smtp(3) -- SMTP server (and soon, client) library and daemon for node.js

Synopsis

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')
          message.accept()
       })      
    })
}).listen(25)

console.log("SMTP server running on port 25")

smtp.Server

Ths main SMTP server constructor. Usually instantiated with 'smtp.createServer'

Properties

Event: 'connection'

function(connection) { }

Emitted when each client connects

smtp.createServer([callback])

The 'callback' parameter will be passed each new connection

smtp.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:

Properties

Event: 'HELO' and 'EHLO'

function(helodata) {}

Emitted when a client sends the 'HELO' or 'EHLO' commands.

The 'helodata' object has the following properties:

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)

Event: 'MAIL FROM'

function(sender) {}

Emitted when a sender is specified by the client.

The 'sender' object has the following properties:

Event: 'RCPT TO'

function(recipient) {}

Emitted for each recipient the client specifies.

The 'recipient' object has the following properties:

Event: 'DATA'

function(MessageStream) {}

Emitted when the client begins sending message data.

Event: 'RSET'

function() {}

Emitted when the client issues a reset command

Event: 'QUIT'

function() {}

Emitted when the client quits, before the socket is closed

Event: 'EXPN' (work in progress)

Emitted when the client issues an expand aliases command

smtp.MessageStream()

An EventEmitter implementing the Readable Stream interface carrying the message data.

Properties

Event: accept

Emitted when the message is accepted.

Event: reject

Emitted when the message is rejected.

smtp.MessageStream.accept()

Accepts the message, so the SMTP daemon will return a 2xx response.

smtp.MessageStream.reject()

Rejects the message, so the SMTP daemon will return a 4xx/5xx response.