Sending an Email from JavaScript Using Node.js or IO.js

1. If you have not already done so, download and install io.js (or its progenitor Node.js if preferred). As of this writing, the latest version of io.js is available for download at the URL https://iojs.org.

2. In any convenient location, create a new directory named “NodemailerTest”.

3. Open a console window (in administrator mode, if necessary) and enter the command “npm install nodemailer” at the prompt. Wait for the installation to complete.

4. Still in the console window, enter the command “npm install nodemailer-direct-transport” at the prompt and wait for the installation to complete. (This example uses direct transport, but be aware that other transport options are available, including such packages as “nodemailer-smtp-transport”. Indeed, since mail sent using direct transport is often interpreted as spam, the developer of Nodemailer recommends that SMTP transport be used in most cases. However, this depends on third-party services, and direct transport is sufficient for the task at hand.)

5. In the newly created NodemailerTest directory, create a new text file named “NodemailerTest.js”, containing the following text. Substitute valid email addresses in the strings that are assigned to the emailAddressFrom and emailAddressTo variables.

// This code is adapted from an example found at the URL
// http://www.nodemailer.com/

var nodemailer = require("nodemailer");

var transporter = nodemailer.createTransport();

var emailAddressFrom = "bob@phoneynet.wontfind.com";
var emailAddressTo = "nobody.nohow@null.net";

transporter.sendMail
(
	{
		from: emailAddressFrom,
		to: emailAddressTo,
		subject: "email test",
		text: "This is an email test."
	}
);

6. Still in the NodemailerTest directory, create a new text file named “NodemailerTest-Run.bat”, containing the following text.

iojs NodemailerTest.js
pause

7. Run the NodeMailerTest-Run.bat script (in administrator mode, if necessary). A console window will appear, and an email will be delivered to the specified address. As previously noted, emails delivered using direct transport are often interpreted as spam, so if you don’t see the message immediately, check the filter.

Advertisements
This entry was posted in Uncategorized and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s