The Node.js ecosystem

A spreadsheet sitting in the IFS is only half a report. The other half is getting it in front of the person who asked for it, preferably before they've finished their first cup of tea. That's a nightly job every IBM i shop already understands: produce the file, send the mail, go back to sleep.

Nodemailer is the npm package I reach for. It talks SMTP, it handles HTML bodies and attachments, and it works perfectly from PASE.

Install it

We'll keep going in the report folder from the previous lesson, so the ingredients.xlsx file is sitting right there.

bash

cd ~/ingredients-report
npm install nodemailer

Current Nodemailer wants Node.js 20 or later. We're on nodejs24, so we're fine. (If node -v shows something older, swap with nodever 24 or call /QOpenSys/pkgs/lib/nodejs24/bin/node directly.)

Configuring SMTP - the modern way

Add the mail settings to the .env you already have. Do not put a password in a source file, and do not put one in a connection URL either - both end up in history files, job logs and git commits.

bash

# .env (append to the database settings from Lesson 6.3)
DB_USER=NICK
DB_PASS=changeme
DB_DSN=*LOCAL

SMTP_HOST=localhost
SMTP_PORT=25
SMTP_SECURE=false
# Leave USER and PASS empty if your local IBM i SMTP server does not require a login.
SMTP_USER=
SMTP_PASS=
MAIL_FROM="Ingredients App <ingredients@example.com>"
MAIL_TO=nick@example.com

Create the transporter with an options object, not a smtp://user:password@host URL. The URL form still parses, but a password in a URL is a bad habit, and the object form is what Nodemailer's own docs lead with.

javascript

// mail.js
require('dotenv').config();
const nodemailer = require('nodemailer');

function createTransporter() {
const options = {
host: process.env.SMTP_HOST || 'localhost',
port: Number(process.env.SMTP_PORT) || 25,
secure: process.env.SMTP_SECURE === 'true', // true = TLS from the start (port 465)
};

// Only add auth if you actually configured a user. Local IBM i SMTP often has none.
if (process.env.SMTP_USER) {
options.auth = {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS,
};
}

return nodemailer.createTransport(options);
}

module.exports = { createTransporter };

A few IBM i-shaped notes:

  • Local IBM i SMTP. If your shop already sends mail from the IBM i, you can often use SMTP_HOST=localhost and port 25. Start the server from a 5250 session with STRTCPSVR SERVER(*SMTP) if it isn't running. The rest (relay host, forwarding hub, whether the sender address has to exist in the directory) is whatever your system administrator already set on CHGSMTPA. If local SMTP isn't configured, this will fail with a connection or relay error - that's a mail-setup problem, not a Node.js one
  • A separate mail server. Point SMTP_HOST at your Exchange, Microsoft 365, Google Workspace or Linux postfix box, usually on port 587 with SMTP_SECURE=false (STARTTLS). Port 465 uses TLS from the start, so set SMTP_SECURE=true
  • Cloud providers and passwords. Many of them have turned off simple username-and-password SMTP. Microsoft 365 has been retiring basic authentication; Gmail wants an app password or OAuth2. If a bare user and password is rejected, that's the provider talking, and you'll need whatever they currently require (an app password is the usual first stop). Don't copy an old smtp://myUser:myPassword@smtp.office365.com snippet and expect it to work in 2026

You can test the connection before you send anything:

javascript

const transporter = createTransporter();
await transporter.verify();
console.log('SMTP is ready');

Building a message

A message is just a JavaScript object - think of it as a data structure with a field for each part of the email.

const message = {
  from: process.env.MAIL_FROM,
  to: process.env.MAIL_TO, // comma-separated for more than one recipient
  subject: 'Ingredients report',
  html: '<p>Good morning.</p><p>The latest <b>ingredients</b> report is attached.</p>',
  attachments: [
    { path: 'ingredients.xlsx' }, // IFS path, relative to the current working directory
  ],
};

useful fields:

nodemailer the message object

Sending it

sendMail returns a Promise, so we await it - no callback required, though the older function (error, info) form still works.

javascript

// send-report.js
require('dotenv').config();
const { createTransporter } = require('./mail');

async function main() {
const transporter = createTransporter();

const info = await transporter.sendMail({
from: process.env.MAIL_FROM,
to: process.env.MAIL_TO,
subject: 'Ingredients report',
html:
'<p>Good morning.</p>' +
'<p>The latest <b>ingredients</b> report from IBM i is attached.</p>',
attachments: [{ path: 'ingredients.xlsx' }],
});

console.log('Sent:', info.messageId);
}

main().catch((err) => {
console.error(err);
process.exit(1);
});

Run it after the report from our previous lesson and it should writte the file:

bash

node ingredients-report.js
node send-report.js

If it worked, info.messageId is the identifier the SMTP server gave the message. Then go and look in the inbox. If it didn't, the error message from Nodemailer is usually plain about whether it couldn't connect, couldn't authenticate, or the server refused the sender.

One program that does both

For a real scheduled job, I wouldn't leave this as two commands. Here's a single script that builds the spreadsheet and emails it. Save it as nightly-report.js:

// nightly-report.js
require('dotenv').config();

const odbc = require('odbc');
const ExcelJS = require('exceljs');
const { createTransporter } = require('./mail');

async function buildWorkbook(rows) {
const workbook = new ExcelJS.Workbook();
workbook.creator = 'Node.js on IBM i';
workbook.created = new Date();

const sheet = workbook.addWorksheet('Ingredients');
sheet.columns = [
{ header: 'Id', key: 'ID', width: 8, style: { numFmt: '0' } },
{ header: 'Name', key: 'NAME', width: 28 },
{ header: 'Category', key: 'CATEGORY', width: 14 },
{ header: 'Unit', key: 'UNIT', width: 8 },
{ header: 'Quantity', key: 'QUANTITY', width: 12, style: { numFmt: '#,##0.00' } },
{ header: 'Calories / 100', key: 'CALORIES', width: 16, style: { numFmt: '#,##0' } },
];
sheet.getRow(1).font = { name: 'Calibri', size: 11, bold: true };
for (const row of rows) {
sheet.addRow({
ID: Number(row.ID),
NAME: row.NAME,
CATEGORY: row.CATEGORY,
UNIT: row.UNIT,
QUANTITY: Number(row.QUANTITY),
CALORIES: Number(row.CALORIES),
});
}
return workbook;
}

async function main() {
const conn = await odbc.connect(
`DSN=${process.env.DB_DSN};UID=${process.env.DB_USER};PWD=${process.env.DB_PASS};CMT=0;`
);
const rows = await conn.query(
'SELECT ID, NAME, CATEGORY, UNIT, QUANTITY, CALORIES ' +
'FROM FOODLIB.INGREDIENTS ORDER BY ID'
);
await conn.close();

const outFile = 'ingredients.xlsx';
const workbook = await buildWorkbook(rows);
await workbook.xlsx.writeFile(outFile);

const transporter = createTransporter();
const info = await transporter.sendMail({
from: process.env.MAIL_FROM,
to: process.env.MAIL_TO,
subject: `Ingredients report - ${rows.length} items`,
html:
`<p>Good morning.</p>` +
`<p>Attached is the ingredients report (${rows.length} rows) ` +
`produced on IBM i by Node.js.</p>`,
attachments: [{ filename: 'ingredients.xlsx', path: outFile }],
});

console.log(`Wrote ${rows.length} rows and sent message ${info.messageId}`);
}

main().catch((err) => {
console.error(err);
process.exit(1);
});

And submitting this as an IBM-i batch job, using the nodejs24 binary so it doesn't depend on a login profile

SBMJOB CMD(QSH CMD('cd /home/NICK/ingredients-report && /QOpenSys/pkgs/lib/nodejs24/bin/node nightly-report.js')) JOB(INGRDMAIL) JOBQ(QSYSNOMAX)

Add that to your job scheduler, and the report goes out on its own. No green-screen SNDDST required - although if you are fond of SNDDST, I won't tell anyone.

Where we've landed

JavaScript is a popular, capable language, and with Node.js it runs on IBM i just as well as it does anywhere else. The real power is the ecosystem: small tools, each good at one job, that snap together. We've only opened a few of the boxes - Express, ExcelJS, Nodemailer, plus odbc and itoolkit from earlier - but that's already enough to serve a web app, call RPG, print a spreadsheet and post it to someone's inbox.

Go and build the next one. And yes, you can have another cup of tea first. You've earned it.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
>