• Stars
    star
    396
  • Rank 108,801 (Top 3 %)
  • Language
    JavaScript
  • License
    Other
  • Created almost 13 years ago
  • Updated almost 10 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple SMTP server/client module

simplesmtp

DEPRECATION NOTICE

This module is deprecated. For SMTP servers use smtp-server, for SMTP clients use smtp-connection. Alternatively, for full featured SMTP server applications, you should use Haraka.


Simplesmtp is a module written for Node v0.6 and slightly updated for Node v0.8. It does not use Node v0.10 streams and probably is going to have a rocky future with Node v0.12. I do not have time to keep it up to date, the thing probably needs a major rewrite for Node v0.12.

Should be fine though for integration testing purposes.

Info

This is a module to easily create custom SMTP servers and clients - use SMTP as a first class protocol in Node.JS!

Build Status NPM version

Version warning!

If you are using node v0.6, then the last usable version of simplesmtp is v0.2.7

Current version of simplesmtp is fully supported for Node v0.8+

Λ‡## SMTP Server

Simple SMTP server

For a simple inbound only, no authentication SMTP server you can use

simplesmtp.createSimpleServer([options], requestListener).listen(port);

Example

simplesmtp.createSimpleServer({SMTPBanner:"My Server"}, function(req){
    req.pipe(process.stdout);
    req.accept();
}).listen(port);

Properties

  • req.from - From address
  • req.to - an array of To addresses
  • req.host - hostname reported by the client
  • req.remodeAddress - client IP address

Methods

  • req.accept ([id]) - Accept the message with the selected ID
  • req.reject ([message]) - Reject the message with the selected message
  • req.pipe (stream) - Pipe the incoming data to a writable stream

Events

  • 'data' (chunk) - A chunk (Buffer) of the message.
  • 'end' - The message has been transferred

Advanced SMTP server

Usage

Create a new SMTP server instance with

var smtp = simplesmtp.createServer([options]);

And start listening on selected port

smtp.listen(25, [function(err){}]);

SMTP options can include the following:

  • name - the hostname of the server, will be used for informational messages
  • debug - if set to true, print out messages about the connection
  • timeout - client timeout in milliseconds, defaults to 60 000 (60 sec.)
  • secureConnection - start a server on secure connection
  • SMTPBanner - greeting banner that is sent to the client on connection
  • requireAuthentication - if set to true, require that the client must authenticate itself
  • enableAuthentication - if set to true, client may authenticate itself but don't have to (as opposed to requireAuthentication that explicitly requires clients to authenticate themselves)
  • maxSize - maximum size of an e-mail in bytes (currently informational only)
  • credentials - TLS credentials ({key:'', cert:'', ca:['']}) for the server
  • authMethods - allowed authentication methods, defaults to ["PLAIN", "LOGIN"]
  • disableEHLO - if set to true, support HELO command only
  • ignoreTLS - if set to true, allow client do not use STARTTLS
  • disableDNSValidation - if set, do not validate sender domains
  • disableSTARTTLS - if set, do not use STARTTLS

Example

var simplesmtp = require("simplesmtp"),
    fs = require("fs");

var smtp = simplesmtp.createServer();
smtp.listen(25);

smtp.on("startData", function(connection){
    console.log("Message from:", connection.from);
    console.log("Message to:", connection.to);
    connection.saveStream = fs.createWriteStream("/tmp/message.txt");
});

smtp.on("data", function(connection, chunk){
    connection.saveStream.write(chunk);
});

smtp.on("dataReady", function(connection, callback){
    connection.saveStream.end();
    console.log("Incoming message saved to /tmp/message.txt");
    callback(null, "ABC1"); // ABC1 is the queue id to be advertised to the client
    // callback(new Error("Rejected as spam!")); // reported back to the client
});

Events

  • startData (connection) - DATA stream is opened by the client (connection is an object with from, to, host and remoteAddress properties)
  • data (connection, chunk) - e-mail data chunk is passed from the client
  • dataReady (connection, callback) - client is finished passing e-mail data, callback returns the queue id to the client
  • authorizeUser (connection, username, password, callback) - will be emitted if requireAuthentication option is set to true. callback has two parameters (err, success) where success is Boolean and should be true, if user is authenticated successfully
  • validateSender (connection, email, callback) - will be emitted if validateSender listener is set up
  • validateRecipient (connection, email, callback) - will be emitted it validataRecipients listener is set up
  • close (connection) - emitted when the connection to client is closed

SMTP Client

Usage

SMTP client can be created with simplesmtp.connect(port[,host][, options]) where

  • port is the port to connect to
  • host is the hostname to connect to (defaults to "localhost")
  • options is an optional options object (see below)

Connection options

The following connection options can be used with simplesmtp.connect:

  • secureConnection - use SSL
  • name - the name of the client server
  • auth - authentication object {user:"...", pass:"..."} or {XOAuthToken:"base64data"}
  • ignoreTLS - ignore server support for STARTTLS
  • tls - optional options object for tls.connect, also applies to STARTTLS. For example rejectUnauthorized is set to false by default. You can override this option by setting tls: {rejectUnauthorized: true}
  • debug - output client and server messages to console
  • logFile - optional filename where communication with remote server has to be logged
  • instanceId - unique instance id for debugging (will be output console with the messages)
  • localAddress - local interface to bind to for network connections (needs Node.js >= 0.11.3 for working with tls)
  • greetingTimeout (defaults to 10000) - Time to wait in ms until greeting message is received from the server
  • connectionTimeout (system default if not set) - Time to wait in ms until the socket is opened to the server
  • socketTimeout (defaults to 1 hour) - Time of inactivity until the connection is closed
  • rejectUnathorized (defaults to false) - if set to true accepts only valid server certificates. You can override this option with the tls option, this is just a shorthand
  • dsn - An object with methods success, failure and delay. If any of these are set to true, DSN will be used
  • enableDotEscaping set to true if you want to escape dots at the begining of each line. Defaults to false.

Connection events

Once a connection is set up the following events can be listened to:

  • 'idle' - the connection to the SMTP server has been successfully set up and the client is waiting for an envelope
  • 'message' - the envelope is passed successfully to the server and a message stream can be started
  • 'ready' (success) - the message was sent
  • 'rcptFailed' (addresses) - not all recipients were accepted (invalid addresses are included as an array)
  • 'error' (err, stage) - An error occurred. The connection is closed and an 'end' event is emitted shortly. Second argument indicates on which SMTP session stage an error occured.
  • 'end' - connection to the client is closed

Sending an envelope

When an 'idle' event is emitted, an envelope object can be sent to the server. This includes a string from and an array of strings to property.

Envelope can be sent with client.useEnvelope(envelope)

// run only once as 'idle' is emitted again after message delivery
client.once("idle", function(){
    client.useEnvelope({
        from: "[email protected]",
        to: ["[email protected]", "[email protected]"]
    });
});

The to part of the envelope includes all recipients from To:, Cc: and Bcc: fields.

If setting the envelope up fails, an error is emitted. If only some (not all) recipients are not accepted, the mail can still be sent but an rcptFailed event is emitted.

client.on("rcptFailed", function(addresses){
    console.log("The following addresses were rejected: ", addresses);
});

If the envelope is set up correctly a 'message' event is emitted.

Sending a message

When 'message' event is emitted, it is possible to send mail. To do this you can pipe directly a message source (for example an .eml file) to the client or alternatively you can send the message with client.write calls (you also need to call client.end() once the message is completed.

If you are piping a stream to the client, do not leave the 'end' event out, this is needed to complete the message sequence by the client.

client.on("message", function(){
    fs.createReadStream("test.eml").pipe(client);
});

Once the message is delivered a 'ready' event is emitted. The event has an parameter which indicates if the message was transmitted( (true) or not (false) and another which includes the last received data from the server.

client.on("ready", function(success, response){
    if(success){
        console.log("The message was transmitted successfully with "+response);
    }
});

XOAUTH

simplesmtp supports XOAUTH2 and XOAUTH authentication.

XOAUTH2

To use this feature you can set XOAuth2 param as an auth option

var mailOptions = {
    ...,
    auth:{
        XOAuth2: {
            user: "[email protected]",
            clientId: "8819981768.apps.googleusercontent.com",
            clientSecret: "{client_secret}",
            refreshToken: "1/xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI",
            accessToken: "vF9dft4qmTc2Nvb3RlckBhdHRhdmlzdGEuY29tCg==",
            timeout: 3600
        }
    }
}

accessToken and timeout values are optional. If login fails a new access token is generated automatically.

XOAUTH

To use this feature you can set XOAuthToken param as an auth option

var mailOptions = {
    ...,
    auth:{
        XOAuthToken: "R0VUIGh0dHBzOi8vbWFpbC5nb29...."
    }
}

Alternatively it is also possible to use XOAuthToken generators (supported by Nodemailer) - this needs to be an object with a mandatory method generate that takes a callback function for generating a XOAUTH token string. This is better for generating tokens only when needed - there is no need to calculate unique token for every e-mail request, since a lot of these might share the same connection and thus the cleint needs not to re-authenticate itself with another token.

var XOGen = {
    token: "abc",
    generate: function(callback){
        if(1 != 1){
            return callback(new Error("Tokens can't be generated in strange environments"));
        }
        callback(null, new Buffer(this.token, "utf-8").toString("base64"));
    }
}

var mailOptions = {
    ...,
    auth:{
        XOAuthToken: XOGen
    }
}

Error types

Emitted errors include the reason for failing in the name property

  • UnknowAuthError - the client tried to authenticate but the method was not supported
  • AuthError - the username/password used were rejected
  • TLSError - STARTTLS failed
  • SenderError - the sender e-mail address was rejected
  • RecipientError - all recipients were rejected (if only some of the recipients are rejected, a 'rcptFailed' event is raised instead

There's also an additional property in the error object called data that includes the last response received from the server (if available for the current error type).

About reusing the connection

You can reuse the same connection several times but you can't send a mail through the same connection concurrently. So if you catch and 'idle' event lock the connection to a message process and unlock after 'ready'.

On 'error' events you should reschedule the message and on 'end' events you should recreate the connection.

Closing the client

By default the client tries to keep the connection up. If you want to close it, run client.quit() - this sends a QUIT command to the server and closes the connection

client.quit();

SMTP Client Connection pool

simplesmtp has the option for connection pooling if you want to reuse a bulk of connections.

Usage

Create a connection pool of SMTP clients with

simplesmtp.createClientPool(port[,host][, options])

where

  • port is the port to connect to
  • host is the hostname to connect to (defaults to "localhost")
  • options is an optional options object (see below)

Connection options

The following connection options can be used with simplesmtp.connect:

  • secureConnection - use SSL
  • name - the name of the client server
  • auth - authentication object {user:"...", pass:"..."} or {XOAuthToken:"base64data"}
  • ignoreTLS - ignore server support for STARTTLS
  • debug - output client and server messages to console
  • logFile - optional filename where communication with remote server has to be logged
  • maxConnections - how many connections to keep in the pool (defaults to 5)
  • localAddress - local interface to bind to for network connections (needs Node.js >= 0.11.3 for working with tls)
  • maxMessages - limit the count of messages to send through a single connection (no limit by default)

Send an e-mail

E-mails can be sent through the pool with

pool.sendMail(mail[, callback])

where

  • mail is a MailComposer compatible object
  • callback (error, responseObj) - is the callback function to run after the message is delivered or an error occured. responseObj may include failedRecipients which is an array with e-mail addresses that were rejected and message which is the last response from the server.

Errors

In addition to SMTP client errors another error name is used

  • DeliveryError - used if the message was not accepted by the SMTP server

License

MIT

More Repositories

1

jStorage

jStorage is a simple key/value database to store data on browser side
JavaScript
1,544
star
2

node-markdown

Parse Markdown syntax with Node.JS
JavaScript
193
star
3

fetch

Fetch URL contents with Node.js
JavaScript
175
star
4

mobileconfig

Create and sign iOS mobileconfig configuration files
JavaScript
164
star
5

highlight

Syntax highlighter for Node.JS
JavaScript
98
star
6

node-gearman

JavaScript
96
star
7

stylus-sprite

Generate sprites with Stylus
JavaScript
90
star
8

encoding

Encode/decode strings with iconv-lite
JavaScript
87
star
9

mimelib

Mime related functions for Node.JS
JavaScript
86
star
10

NodePie

RSS/Atom parser for Node.JS
JavaScript
68
star
11

xoauth2

XOAuth2 token generation with node.js
JavaScript
66
star
12

n3

pop3 server for node.js
JavaScript
63
star
13

hoodiecrow

Scriptable IMAP server for client integration testing
JavaScript
60
star
14

nodemailer-html-to-text

Nodemailer plugin to generate text content from HTML
JavaScript
47
star
15

nodemailer-markdown

Adds `markdown` property to Nodemailer e-mail data
JavaScript
37
star
16

pubsubhubbub

PubSubHubbub subscriber
JavaScript
34
star
17

nodemailer-stub-transport

Stub transport for testing Nodemailer e-mails
26
star
18

node-jsonrpc

JSON-RPC handler for Node.JS
JavaScript
22
star
19

mailsplit

Split email messages into an object stream
JavaScript
22
star
20

ID-AJAX

ID kaardi ja Mobiil-ID autentimine/allkirjastamine AJAX kΓ€skudega
PHP
19
star
21

gearnode

Gearman worker and client for Node.js
JavaScript
19
star
22

dkim-signer

Sign messages with DKIM
JavaScript
18
star
23

GAE-IP-TO-COUNTRY

Converts an IP to country name (Google App Engine module)
PHP
18
star
24

rai

Request-Answer-Interface
JavaScript
16
star
25

resolver

Resolve URLs with node.js
JavaScript
15
star
26

PhoneNumber.js-for-the-Web

This is a fork of Phonenumber.js to be used in the web
JavaScript
14
star
27

DOMCached

Development has been ceased in favor of jStorage http://github.com/andris9/jStorage !! DOMCached is a memcached like JavaScript caching system for browser side data caching
14
star
28

directmail

Send e-mail messages directly to recipient without relaying, sendmail alternative
JavaScript
14
star
29

torfetch

JavaScript
12
star
30

node-fortumo

unofficial fortumo sms payment bindings for node.js
JavaScript
11
star
31

disposable

Web app for creating disposable e-mail inboxes
JavaScript
11
star
32

pass

Node.js Apache htpasswd password generator/validator
JavaScript
10
star
33

nodemailer-pickup-transport

Nodemailer transport for storing emails to pickup folders
10
star
34

cipherstream

Wrapper to create encryption/decryption Streams
JavaScript
8
star
35

postiindeks

Eesti postiindeksid
JavaScript
8
star
36

imap-autoconfig

Discover IMAP settings for an user
JavaScript
7
star
37

mailuploader

Parse and POST e-mail contents
JavaScript
7
star
38

feedster

Easy RSS feed generation in Node.js
JavaScript
6
star
39

public-address

Resolve current public IP address and hostname
JavaScript
6
star
40

blocks

Blocks
JavaScript
5
star
41

ariregister

Get information about Estonian companies
JavaScript
5
star
42

grid-fs

Convenience wrappers around Mongodb GridFS
JavaScript
4
star
43

postfix-parser

Parse email messages from Postfix queue files
JavaScript
4
star
44

Pangalink.net

Pangalink.net lΓ€htekood
CSS
4
star
45

Chameleon

JavaScript
4
star
46

luukere

Generate Node.js server application skeletons that can be compiled into Debian .deb packages
JavaScript
4
star
47

zmta-xauth-ratelimit

Example plugin for SPF and rate limiting in Zone-MTA
JavaScript
4
star
48

artikliotsing

Eesti online meedia artiklite otsing ja indekseerimine
JavaScript
3
star
49

mime-model

Parse and compile mime trees
JavaScript
3
star
50

proxy-test-server

Proxy server for testing proxied connections
JavaScript
3
star
51

dnsmanager

JavaScript
3
star
52

php-sdk

PHP
3
star
53

statbot

JavaScript
3
star
54

node-pdoc

pdoc parser for node.js
JavaScript
3
star
55

j18s

Yet another JavaScript i18n library
JavaScript
3
star
56

detectfeed

JavaScript
3
star
57

php-rabbit

Simple PHP RabbitMQ client
PHP
3
star
58

tempfiles

JavaScript
3
star
59

node-mujax

download multiple files to disk, post to http
JavaScript
3
star
60

node-pubsub-client

nodejs pubsubhubbub client
JavaScript
2
star
61

chatjs

Simple LLama2 demo app
JavaScript
2
star
62

registry-manager

User management for Verdaccio
JavaScript
2
star
63

Evenlope

An e-mail server written in JavaScript (work in progress)
JavaScript
2
star
64

getattachments

Command to parse and store all nodes from an email as files
JavaScript
2
star
65

jstorage-app

jstorage homepage in google app engine
JavaScript
2
star
66

sandpress

Secure storage service
JavaScript
2
star
67

maildir-scan

Scan Maildir folder for messages
JavaScript
2
star
68

forward

example mail forwarder with ARC support
JavaScript
2
star
69

redis-stream-access

JavaScript
2
star
70

sanitizer

JavaScript
2
star
71

documentor-homepage

JavaScript
2
star
72

sanitize-worker

JavaScript
2
star
73

lemma

Find lemmas for estonian language words
JavaScript
2
star
74

imap-auth-test

JavaScript
2
star
75

documentor-worker

JavaScript
2
star
76

nodemailer-hashcash

Generate Hashcash headers for Nodemailer messages
2
star
77

zonemta-delivery-counters

Delivery counters plugin for ZoneMTA
JavaScript
2
star
78

webgettext

Parse and generate gettext files in the browser
JavaScript
1
star
79

inbox-test

test inbox with travis
JavaScript
1
star
80

wp-sendmail

Sendmail replacement for WordPress
JavaScript
1
star
81

otse-elust

Simple filestorage on top of Google App Engine for otseelust.com
Python
1
star
82

zmta-locks

JavaScript
1
star
83

eslint-config-nodemailer

JavaScript
1
star
84

remoteaddress

Service for checking public IP address and hostname
JavaScript
1
star
85

zone-logs

JavaScript
1
star
86

random-message

Split large mbox files and return random messages for testing
JavaScript
1
star
87

bfclient

JavaScript
1
star
88

zonemta-onion

Tor plugin for ZoneMTA
JavaScript
1
star
89

ethereal-www

CSS
1
star
90

map-router

Fast exact-match router for Express.js
JavaScript
1
star
91

nodemailer-homepage

Google App Engine based homepage for nodemailer.org
1
star
92

https-front

Simple HTTPS proxy
JavaScript
1
star
93

biskviit

Yet another node module for handling http cookies
JavaScript
1
star
94

bfserv

JavaScript
1
star
95

zonemta-limiter

Limiter plugin for ZoneMTA
JavaScript
1
star
96

mc

JavaScript
1
star
97

smtp-test-server

Test server for SMTP clients. Does not store messages, returns md5 hash instead.
JavaScript
1
star
98

avast-client

Node.js client for Avast Core Security Linux server daemon
JavaScript
1
star
99

leveldown-basho-andris

A fork of LevelDOWN to use basho-leveldb
C++
1
star
100

capabilitybot

Lists CAPABILITY info for configured hosts
JavaScript
1
star