Nodemailer

Send mail using server-side JavaScript

by Read Write Tools
Abstract
This plugin responds to properly prepared POSTs by sending a confirmation email to the designated recipient. The code is purposefully over-simplified, and is intended to be a starting point only, for developing a customized subscription list that fits your needs.

Motivation

There are many applications where sending an email in response to an HTTP POST is required. This module is a proof-of-concept plugin that demonstrates one way to send SMTP messages using the Nodemailer module.

Customization

This plugin is open-source and may be extended to provide additional features, such as:

  • Accepting additional form elements such as user profile data or answers to survey questions.
  • Customizing the outgoing emails with different messages based on form inputs.
  • Storing email addresses using a file or database.
  • Sending HTML instead of plain text.

Download

The plugin module is available from NPM. Before proceeding, you should already have Node.js and RWSERVE configured and tested.

This module should be installed on your web server in a well-defined place, so that it can be discovered by RWSERVE. The standard place for public domain plugins is /srv/rwserve-plugins.

cd /srv/rwserve-plugins
npm install rwserve-nodemailer

Configuration is Everything

Make the software available by declaring it in the plugins section of your configuration file. For detailed instructions on how to do this, refer to the plugins documentation on the Read Write Tools HTTP/2 Server website.

TL;DR

plugins {
rwserve-nodemailer {
location `/srv/rwserve-plugins/node_modules/rwserve-nodemailer/dist/index.js`
config {
transport {
host 127.0.0.1
port 25
authMethod PLAIN
connectionTimeout 2000
}
message-defaults {
from welcome.team@example.com
subject Welcome
text Thank you for signing up today.%0D%0AThis message confirms your request to join our mailing list.%0D%0ATogether we can make it happen!
}
}
}
router {
`customer-service/signup` *methods=POST *plugin=rwserve-nodemailer
}
}

How it works

The plugin receives POST requests originating from a standard HTML <form> , which uses content-type application/x-www-form-urlencoded. The request body should contain one form element, named recipient, which contains an email address.

An email envelope is prepared using the given recipient plus the values for from, subject, and text which are defined in the message-defaults section of the configuration file.

The email is queued for delivery using SMTP using the values for host, port, authMethod and connectionTimeout defined in the transport section of the configuration file. Values for authUser and authPassword should be entirely omitted unless required by your SMTP provider.

When communication with the SMTP server is complete, the last reply code received from the transport is placed in the special header nodemailer-smtp-reply. A value of 250 generally indicates success, and can be interpreted as "email was queued for delivery". Other values are generally failures. (Search for "smtp reply code" to help diagnose those problems.)

The HTTP request/response cycle completes with status code 200 and a response body with content-type application/json. The returned JSON payload contains:

  • accepted an array of addresses that were accepted by SMTP
  • rejected an array of addresses that were rejected by SMTP
  • response the last communication from the SMTP server with reply code
  • messageId a unique email identifier generated by SMTP for logging

Sample HTML

<form action='https://localhost:7443/customer-service/signup' method=POST>
<label>Email address <input type=text name=recipient /></label>
<input type=submit value=Submit />
</form>

CURL Test

You can also use CURL to send a request to the plugin like this:

curl https://localhost:7443/customer-service/signup -X POST -H content-type:application/x-www-form-urlencoded -H content-length:33 -d "recipient=friendly@mailinator.com"

Cookbook

A full configuration file with typical settings for a server running on localhost port 7443, is included in this NPM module at etc/nodemailer-config. To use this configuration file, adjust these variables if they don't match your server setup:

$PLUGIN-PATH='/srv/rwserve-plugins/node_modules/rwserve-nodemailer/dist/index.js'
$PRIVATE-KEY='/etc/pki/tls/private/localhost.key'
$CERTIFICATE='/etc/pki/tls/certs/localhost.crt'
$DOCUMENTS-PATH='/srv/rwserve/configuration-docs'
$RECIPIENT='welcome.team@example.com'

Deployment

Once you've tested the plugin and are ready to go live, adjust your production web server's configuration in /etc/rwserve/rwserve.conf and restart it using systemd. . .

[user@host ~]# systemctl restart rwserve

. . . then monitor its request/response activity with journald.

[user@host ~]# journalctl -u rwserve -ef

Prerequisites

This is a plugin for the Read Write Tools HTTP/2 Server, which works on Linux platforms.

Software Minimum Version Most Recent Version
Ubuntu 16 Xenial Xerus 16 Xenial Xerus
Debian 9 Stretch 10 Buster
openSUSE openSUSE 15.1 openSUSE 15.1
Fedora Fedora 27 Fedora 32
CentOS CentOS 7.4 CentOS 8.1
RHEL RHEL 7.8 RHEL 8.2
RWSERVE RWSERVE 1.0.1 RWSERVE 1.0.47
Node.js Node.js 10.3 Node.js 12.17

Review

Lessons
This plugin demonstrates a basic plugin that cooperates with other server software to perform non-HTTP related work. The important patterns to observe are:
  • The SMTP server transport is initialized in the startup method.
  • Once the email message is received by SMTP, control returns to the plugin.
  • The plugin does not wait around to learn whether or not actual delivery of the email occurred.
Find other plugins for the Read Write Tools HTTP/2 Server using npm with these keywords: rwserve, http2, plugins.

License

The rwserve-nodemailer plugin is licensed under the MIT License.

MIT License

Copyright © 2020 Read Write Tools.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Availability

Source code github
Package installation NPM
Documentation Read Write Hub

Nodemailer — Send mail using server-side JavaScript

🔎