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
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
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
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:
You can test the connection before you send anything:
javascript
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.
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:
Sending it
sendMail returns a Promise, so we await it - no callback required, though the older function (error, info) form still works.
javascript
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 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:
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
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.

