Skip to content

Commit b801c48

Browse files
andris9claude
andcommitted
fix: include icalEvent path/href content in the application/ics attachment
getAttachments() appended the icalEvent copy without mapping path/href/data uri values into a content entry the way regular attachments are mapped, so _createContentNode fell back to an empty body and the application/ics attachment part was sent with zero-length content (the text/calendar alternative part was populated correctly). The normalization now lives in a shared _getIcalEvent() helper used by both getAlternatives() and getAttachments(); the two copies of the event share a single content object marked with the existing _resolve mechanism, so a file is read (or an URL fetched) only once per message. Reported in nodemailer-upstream-bugs-2026-06-10 (issue 2). Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 203c8ec commit b801c48

2 files changed

Lines changed: 156 additions & 32 deletions

File tree

‎lib/mail-composer/index.js‎

Lines changed: 65 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class MailComposer {
8383
* @returns {Object} An object of arrays (`related` and `attached`)
8484
*/
8585
getAttachments(findRelated) {
86-
let icalEvent, eventObject;
86+
let eventObject;
8787
const attachments = [].concat(this.mail.attachments || []).map((attachment, i) => {
8888
if (/^data:/i.test(attachment.path || attachment.href)) {
8989
attachment = this._processDataUrl(attachment);
@@ -160,18 +160,7 @@ class MailComposer {
160160
});
161161

162162
if (this.mail.icalEvent) {
163-
if (
164-
typeof this.mail.icalEvent === 'object' &&
165-
(this.mail.icalEvent.content || this.mail.icalEvent.path || this.mail.icalEvent.href || this.mail.icalEvent.raw)
166-
) {
167-
icalEvent = this.mail.icalEvent;
168-
} else {
169-
icalEvent = {
170-
content: this.mail.icalEvent
171-
};
172-
}
173-
174-
eventObject = Object.assign({}, icalEvent);
163+
eventObject = Object.assign({}, this._getIcalEvent());
175164

176165
eventObject.contentType = 'application/ics';
177166
if (!eventObject.headers) {
@@ -195,14 +184,75 @@ class MailComposer {
195184
};
196185
}
197186

187+
/**
188+
* Returns the icalEvent value with `path`/`href`/data uri input normalized into
189+
* a `content` entry, the same way as for regular attachments. The same event is
190+
* included twice (as a text/calendar alternative and as an application/ics
191+
* attachment), so the shared content object is marked to be resolved just once
192+
* and the buffered result is reused by the second node.
193+
*
194+
* @returns {Object} Normalized icalEvent data
195+
*/
196+
_getIcalEvent() {
197+
if (!this._icalEvent) {
198+
let icalEvent;
199+
if (
200+
typeof this.mail.icalEvent === 'object' &&
201+
(this.mail.icalEvent.content || this.mail.icalEvent.path || this.mail.icalEvent.href || this.mail.icalEvent.raw)
202+
) {
203+
icalEvent = Object.assign({}, this.mail.icalEvent);
204+
} else {
205+
icalEvent = {
206+
content: this.mail.icalEvent
207+
};
208+
}
209+
210+
if (/^data:/i.test(icalEvent.path || icalEvent.href)) {
211+
icalEvent = this._processDataUrl(icalEvent);
212+
}
213+
214+
if (/^https?:\/\//i.test(icalEvent.path)) {
215+
icalEvent.href = icalEvent.path;
216+
icalEvent.path = undefined;
217+
}
218+
219+
if (!icalEvent.raw) {
220+
// map file path and URL values into `content`, otherwise the content
221+
// nodes would render an empty body
222+
if (icalEvent.path) {
223+
icalEvent.content = {
224+
path: icalEvent.path
225+
};
226+
icalEvent.path = undefined;
227+
} else if (icalEvent.href) {
228+
icalEvent.content = {
229+
href: icalEvent.href,
230+
httpHeaders: icalEvent.httpHeaders
231+
};
232+
icalEvent.href = undefined;
233+
}
234+
}
235+
236+
if (icalEvent.content && typeof icalEvent.content === 'object') {
237+
// we are going to have the same attachment twice, so mark this to be
238+
// resolved just once
239+
icalEvent.content._resolve = true;
240+
}
241+
242+
this._icalEvent = icalEvent;
243+
}
244+
245+
return this._icalEvent;
246+
}
247+
198248
/**
199249
* List alternatives. Resulting objects can be used as input for MimeNode nodes
200250
*
201251
* @returns {Array} An array of alternative elements. Includes the `text` and `html` values as well
202252
*/
203253
getAlternatives() {
204254
const alternatives = [];
205-
let text, html, watchHtml, amp, icalEvent, eventObject;
255+
let text, html, watchHtml, amp, eventObject;
206256

207257
if (this.mail.text) {
208258
if (
@@ -248,24 +298,7 @@ class MailComposer {
248298

249299
// NB! when including attachments with a calendar alternative you might end up in a blank screen on some clients
250300
if (this.mail.icalEvent) {
251-
if (
252-
typeof this.mail.icalEvent === 'object' &&
253-
(this.mail.icalEvent.content || this.mail.icalEvent.path || this.mail.icalEvent.href || this.mail.icalEvent.raw)
254-
) {
255-
icalEvent = this.mail.icalEvent;
256-
} else {
257-
icalEvent = {
258-
content: this.mail.icalEvent
259-
};
260-
}
261-
262-
eventObject = Object.assign({}, icalEvent);
263-
264-
if (eventObject.content && typeof eventObject.content === 'object') {
265-
// we are going to have the same attachment twice, so mark this to be
266-
// resolved just once
267-
eventObject.content._resolve = true;
268-
}
301+
eventObject = Object.assign({}, this._getIcalEvent());
269302

270303
eventObject.filename = false;
271304
eventObject.contentType =

‎test/mail-composer/mail-composer-test.js‎

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

33
const { describe, it } = require('node:test');
44
const assert = require('node:assert/strict');
5+
const http = require('node:http');
56
const MailComposer = require('../../lib/mail-composer');
67

78
describe('MailComposer unit tests', () => {
@@ -1007,6 +1008,96 @@ describe('MailComposer unit tests', () => {
10071008
});
10081009
});
10091010

1011+
it('should load icalEvent from file for both alternative and attachment parts', (t, done) => {
1012+
let data = {
1013+
text: 'def',
1014+
icalEvent: {
1015+
path: __dirname + '/fixtures/attachment.bin'
1016+
}
1017+
};
1018+
1019+
let mail = new MailComposer(data).compile();
1020+
mail.build((err, message) => {
1021+
assert.ok(!err);
1022+
let msg = message.toString();
1023+
let icsPart = msg.substring(msg.indexOf('Content-Type: application/ics'));
1024+
assert.ok(icsPart.includes('name=invite.ics'));
1025+
// base64 of the fixture content must be present in the attachment copy
1026+
assert.ok(icsPart.includes('w7VrdmEK'), 'application/ics part should not be empty');
1027+
done();
1028+
});
1029+
});
1030+
1031+
it('should load icalEvent from data uri for both alternative and attachment parts', (t, done) => {
1032+
let data = {
1033+
text: 'def',
1034+
icalEvent: {
1035+
path: 'data:text/calendar;base64,dGVzdA=='
1036+
}
1037+
};
1038+
1039+
let mail = new MailComposer(data).compile();
1040+
mail.build((err, message) => {
1041+
assert.ok(!err);
1042+
let msg = message.toString();
1043+
let icsPart = msg.substring(msg.indexOf('Content-Type: application/ics'));
1044+
assert.ok(icsPart.includes('dGVzdA=='), 'application/ics part should not be empty');
1045+
done();
1046+
});
1047+
});
1048+
1049+
it('should load icalEvent from an url for both alternative and attachment parts', (t, done) => {
1050+
let requestCount = 0;
1051+
let server = http.createServer((req, res) => {
1052+
requestCount++;
1053+
res.writeHead(200, { 'Content-Type': 'text/calendar' });
1054+
res.end('BEGIN:VCALENDAR');
1055+
});
1056+
1057+
server.listen(0, () => {
1058+
let data = {
1059+
text: 'def',
1060+
icalEvent: {
1061+
href: 'http://localhost:' + server.address().port + '/invite.ics'
1062+
}
1063+
};
1064+
1065+
let mail = new MailComposer(data).compile();
1066+
mail.build((err, message) => {
1067+
server.close(() => {
1068+
assert.ok(!err);
1069+
let msg = message.toString();
1070+
let icsPart = msg.substring(msg.indexOf('Content-Type: application/ics'));
1071+
assert.ok(
1072+
icsPart.includes(Buffer.from('BEGIN:VCALENDAR').toString('base64')),
1073+
'application/ics part should not be empty'
1074+
);
1075+
// the text/calendar alternative shares the resolved value
1076+
assert.ok(msg.indexOf('Content-Type: text/calendar') >= 0);
1077+
assert.strictEqual(requestCount, 1, 'the event url should be fetched only once');
1078+
done();
1079+
});
1080+
});
1081+
});
1082+
});
1083+
1084+
it('should not load icalEvent from file if disableFileAccess is set', (t, done) => {
1085+
let data = {
1086+
text: 'def',
1087+
icalEvent: {
1088+
path: __dirname + '/fixtures/attachment.bin'
1089+
},
1090+
disableFileAccess: true
1091+
};
1092+
1093+
let mail = new MailComposer(data).compile();
1094+
mail.build((err, message) => {
1095+
assert.ok(err);
1096+
assert.ok(!message);
1097+
done();
1098+
});
1099+
});
1100+
10101101
it('should use load attachment from file', (t, done) => {
10111102
let data = {
10121103
text: 'abc',

0 commit comments

Comments
 (0)