Registry / communication / nodemailer-build-attachment

nodemailer-build-attachment

JSON →
library3.0.0jsnpmunverified

nodemailer-build-attachment is a utility package designed to simplify the creation and streaming of email attachments for use with Nodemailer. It builds attachment content, allowing input from various sources like file streams, and processes it into a format suitable for email transmission, typically a `base64` encoded string. The package is currently at version 3.0.0. Its release cadence is not explicitly stated in the provided documentation, and the latest major release dates back approximately five years. A key differentiator is its specialized focus on processing stream content for attachments, building upon the `buildmail` library for robust email part construction, rather than being a general email sending library itself.

npm install nodemailer-build-attachment
INSTALL
IMPORT
SIG · NODEMAILER-BUILD-A
N
nodemailer-build-attachment
communicationjavascriptv3.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

BuildAttachment
const BuildAttachment = require('nodemailer-build-attachment');
This package primarily exposes a class constructor via Node.js CommonJS `module.exports`. This is the standard and recommended way to import it.
BuildAttachment
import BuildAttachment from 'nodemailer-build-attachment';
import { BuildAttachment } from 'nodemailer-build-attachment';
While `nodemailer-build-attachment` was originally CommonJS-focused, Node.js allows default CJS exports to be imported as default ESM imports. Using named imports (`{ BuildAttachment }`) will likely not work as it is not a named export.

Demonstrates how to initialize the BuildAttachment class, set its content from a file stream, and build a base64 encoded attachment object ready for use with Nodemailer. It also shows basic error handling.

const BuildAttachment = require('nodemailer-build-attachment'); const fs = require('fs'); const path = require('path'); // Create a dummy file for demonstration purposes const filePath = path.join(__dirname, 'temp-attachment.txt'); fs.writeFileSync(filePath, 'This is the content of the attachment file generated by the example.'); const stream = fs.createReadStream(filePath); const options = { filename: 'document.txt', // Recommended: specify filename contentType: 'text/plain', // Recommended: specify content type contentTransferEncoding: 'base64' }; new BuildAttachment(options).setContent(stream).build((err, attachment) => { if (err) { console.error('Error building attachment:', err); return; } console.log('Attachment built successfully.'); console.log('Attachment content (base64):', attachment.content.toString().substring(0, 50) + '...'); // Truncate for display console.log('Attachment headers:', attachment.headers); // In a real Nodemailer scenario, 'attachment' would be used like this: // const nodemailer = require('nodemailer'); // const transporter = nodemailer.createTransport(...); // transporter.sendMail({ // from: 'sender@example.com', // to: 'recipient@example.com', // subject: 'Test Attachment', // attachments: [{ // filename: attachment.filename || options.filename, // content: attachment.content, // contentType: attachment.contentType // // ... other properties from attachment.headers as needed // }] // }).then(...); // Clean up the dummy file fs.unlinkSync(filePath); });
Debug
Known issues
gotchaThis library utilizes a callback-based asynchronous API, common in older Node.js patterns. Modern Node.js applications frequently use Promises or async/await. Direct integration with Promise-based workflows requires manual promisification of the `.build` method.
fix
Wrap the `.build` method in a Promise for easier integration: `const buildPromise = new Promise((resolve, reject) => new BuildAttachment(options).setContent(stream).build((err, att) => err ? reject(err) : resolve(att)));`
affects: >=3.0.0
gotchaWhen providing a Node.js `Stream` as attachment content, it is crucial to implement proper error handling for the stream itself. An unhandled error in the input stream can prevent the attachment from being built and might lead to application crashes if not caught.
fix
Always attach an error listener to your input stream: `stream.on('error', (err) => console.error('Error reading attachment stream:', err));` and ensure the `.build` callback also handles potential errors.
affects: >=3.0.0
gotchaThe latest major version (3.0.0) of this package was released approximately five years ago (based on a current date of April 2026). While it may still function correctly, its compatibility with the latest versions of `Nodemailer` (which is now at v8+) has not been verified through recent updates or official statements. There might be subtle incompatibilities or missing features.
fix
Thoroughly test `nodemailer-build-attachment` with your specific `Nodemailer` version. If compatibility issues or feature gaps arise, consider using `Nodemailer`'s built-in attachment options or creating a custom attachment processing logic.
affects: >=3.0.0
gotchaUsing large `Buffer` or `String` values directly with `setContent()` instead of `Stream` objects can lead to high memory consumption, especially in server environments. While the example uses a stream, this is a common pitfall.
fix
For attachments that are files or large data, always use a readable stream (e.g., `fs.createReadStream`) with `setContent()` to minimize memory footprint and enable efficient processing.
affects: >=3.0.0
Errors
Common errors & fixes
TypeError: callback is not a function
The `.build()` method expects a Node.js-style callback function `(err, attachment) => {}` as its argument, but it was either omitted, `null`, or an invalid type.
fix
Ensure the `.build()` method is called with a valid function as its final argument: `build((err, attachment) => { /* handle result */ });`
UnhandledPromiseRejectionWarning: Error: read EPIPE
This error typically indicates an issue with the underlying stream provided to `setContent()`, such as the stream being closed prematurely, an error occurring during file read, or the file not existing.
fix
Implement robust error handling for the input stream (`stream.on('error', ...)`), verify the file path or stream source, and ensure the stream remains open and readable until the attachment is fully processed.
Attachment content is empty or malformed in the sent email.
This often happens if the input stream is empty, closed incorrectly, or the `contentTransferEncoding` option is mismatched with the actual content type or expectation of the email client.
fix
Double-check the source stream for content, ensure `contentTransferEncoding` is set appropriately (e.g., `'base64'` for most binary data), and verify that the `filename` and `contentType` options are correctly set for the recipient's email client to interpret the attachment.
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies
buildmailrequiredCore library for building MIME messages, upon which this attachment builder is based.
Agent activity
15 hits · last 30 days
node
14
OpenAI (training)
1
Resources
nodemailer-build-attachment — npm install nodemailer-build-attachment · libregistry