Skip to content

Commit cb4f904

Browse files
andris9claude
andcommitted
fix: apply the transport-level newline option in stream and sendmail transports
When newline handling moved into MimeNode in v6.6.0 the transport-level piping was removed, leaving the documented transport option a no-op: stream-transport only mentioned it in a log line and sendmail-transport ignored it entirely (the message-level option kept working). Restore the LeWindows/LeUnix transform in both transports, applied only when the option is set so default output is unchanged. Reported in nodemailer-upstream-bugs-2026-06-10 (issue 4). Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 77e5885 commit cb4f904

4 files changed

Lines changed: 163 additions & 2 deletions

File tree

‎lib/sendmail-transport/index.js‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const { spawn } = require('child_process');
44
const packageData = require('../../package.json');
55
const shared = require('../shared');
66
const errors = require('../errors');
7+
const LeWindows = require('../mime-node/le-windows');
8+
const LeUnix = require('../mime-node/le-unix');
79

810
/**
911
* Generates a Transport object for Sendmail
@@ -46,6 +48,8 @@ class SendmailTransport {
4648
this.args = options.args;
4749
}
4850
}
51+
52+
this.winbreak = ['win', 'windows', 'dos', '\r\n'].includes((options.newline || '').toString().toLowerCase());
4953
}
5054

5155
/**
@@ -178,7 +182,15 @@ class SendmailTransport {
178182
);
179183

180184
const sourceStream = mail.message.createReadStream();
181-
sourceStream.once('error', err => {
185+
let stream = sourceStream;
186+
if (this.options.newline) {
187+
// apply the transport-level line ending transform; the message-level
188+
// `newline` option is handled by MimeNode in createReadStream()
189+
stream = sourceStream.pipe(this.winbreak ? new LeWindows() : new LeUnix());
190+
sourceStream.once('error', err => stream.emit('error', err));
191+
}
192+
193+
stream.once('error', err => {
182194
this.logger.error(
183195
{
184196
err,
@@ -193,7 +205,7 @@ class SendmailTransport {
193205
callback(err);
194206
});
195207

196-
sourceStream.pipe(sendmail.stdin);
208+
stream.pipe(sendmail.stdin);
197209
} else {
198210
const err = new Error('sendmail was not found');
199211
err.code = errors.ESENDMAIL;

‎lib/stream-transport/index.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
const packageData = require('../../package.json');
44
const shared = require('../shared');
5+
const LeWindows = require('../mime-node/le-windows');
6+
const LeUnix = require('../mime-node/le-unix');
57

68
/**
79
* Generates a Transport object for streaming
@@ -63,6 +65,13 @@ class StreamTransport {
6365

6466
try {
6567
stream = mail.message.createReadStream();
68+
if (this.options.newline) {
69+
// apply the transport-level line ending transform; the message-level
70+
// `newline` option is handled by MimeNode in createReadStream()
71+
const sourceStream = stream;
72+
stream = sourceStream.pipe(this.winbreak ? new LeWindows() : new LeUnix());
73+
sourceStream.once('error', err => stream.emit('error', err));
74+
}
6675
} catch (E) {
6776
this.logger.error(
6877
{

‎test/sendmail/sendmail-test.js‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,86 @@ describe('Sendmail Transport Tests', () => {
8080
);
8181
});
8282

83+
// Regression tests: the transport-level `newline` option must transform the
84+
// message piped to the sendmail binary
85+
it('Should apply transport-level windows newlines', (t, done) => {
86+
let client = new SendmailTransport({ newline: 'windows' });
87+
88+
let stubbedSpawn = new EventEmitter();
89+
stubbedSpawn.stdin = new PassThrough();
90+
stubbedSpawn.stdout = new PassThrough();
91+
92+
let output = '';
93+
stubbedSpawn.stdin.on('data', chunk => {
94+
output += chunk.toString();
95+
});
96+
97+
stubbedSpawn.stdin.on('end', () => {
98+
stubbedSpawn.emit('close', 0);
99+
stubbedSpawn.emit('exit', 0);
100+
});
101+
102+
t.mock.method(client, '_spawn', () => stubbedSpawn);
103+
104+
client.send(
105+
{
106+
data: {},
107+
message: new MockBuilder(
108+
{
109+
110+
111+
},
112+
'message\nline 2'
113+
)
114+
},
115+
err => {
116+
assert.ok(!err);
117+
assert.strictEqual(output, 'message\r\nline 2');
118+
t.mock.restoreAll();
119+
done();
120+
}
121+
);
122+
});
123+
124+
it('Should apply transport-level unix newlines', (t, done) => {
125+
let client = new SendmailTransport({ newline: 'unix' });
126+
127+
let stubbedSpawn = new EventEmitter();
128+
stubbedSpawn.stdin = new PassThrough();
129+
stubbedSpawn.stdout = new PassThrough();
130+
131+
let output = '';
132+
stubbedSpawn.stdin.on('data', chunk => {
133+
output += chunk.toString();
134+
});
135+
136+
stubbedSpawn.stdin.on('end', () => {
137+
stubbedSpawn.emit('close', 0);
138+
stubbedSpawn.emit('exit', 0);
139+
});
140+
141+
t.mock.method(client, '_spawn', () => stubbedSpawn);
142+
143+
client.send(
144+
{
145+
data: {},
146+
message: new MockBuilder(
147+
{
148+
149+
150+
},
151+
'message\r\nline 2'
152+
)
153+
},
154+
err => {
155+
assert.ok(!err);
156+
assert.strictEqual(output, 'message\nline 2');
157+
t.mock.restoreAll();
158+
done();
159+
}
160+
);
161+
});
162+
83163
it('Should reject message', (t, done) => {
84164
let client = new SendmailTransport();
85165

‎test/stream-transport/stream-transport-test.js‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,66 @@ describe('Stream Transport Tests', { timeout: 10000 }, () => {
119119
});
120120
});
121121

122+
// Regression tests: the transport-level `newline` option must transform the
123+
// generated stream even when the message itself sets no `newline` value
124+
describe('Transport-level newline option', () => {
125+
it('Should apply windows newlines to the output stream', (t, done) => {
126+
let client = new StreamTransport({
127+
newline: 'windows'
128+
});
129+
let chunks = [],
130+
message = new Array(100).join('teretere\nvana kere\n');
131+
132+
client.send(
133+
{
134+
data: {},
135+
message: new MailComposer({
136+
137+
138+
raw: Buffer.from(message)
139+
}).compile()
140+
},
141+
(err, info) => {
142+
assert.ok(!err);
143+
144+
info.message.on('data', chunk => {
145+
chunks.push(chunk);
146+
});
147+
148+
info.message.on('end', () => {
149+
let body = Buffer.concat(chunks);
150+
assert.strictEqual(body.toString(), message.replace(/\n/g, '\r\n'));
151+
done();
152+
});
153+
}
154+
);
155+
});
156+
157+
it('Should apply unix newlines to the output buffer', (t, done) => {
158+
let client = new StreamTransport({
159+
newline: 'unix',
160+
buffer: true
161+
});
162+
let message = new Array(100).join('teretere\r\nvana kere\r\n');
163+
164+
client.send(
165+
{
166+
data: {},
167+
message: new MailComposer({
168+
169+
170+
raw: Buffer.from(message)
171+
}).compile()
172+
},
173+
(err, info) => {
174+
assert.ok(!err);
175+
assert.strictEqual(info.message.toString(), message.replace(/\r\n/g, '\n'));
176+
done();
177+
}
178+
);
179+
});
180+
});
181+
122182
describe('Send as buffer', () => {
123183
it('Should send mail using unix newlines', (t, done) => {
124184
let client = new StreamTransport({

0 commit comments

Comments
 (0)