Add outgoing mail attachments

Support multiple attachments in desktop and Android compose, persist them in drafts and offline queues, and include them in desktop OpenPGP protection. Update docs and tests.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-10-10 21:05:32 +03:00
co-authored by Copilot
parent acba5d641e
commit 5cd8d545e9
13 changed files with 335 additions and 20 deletions
+18 -3
View File
@@ -1,6 +1,7 @@
const openpgp = require('openpgp');
const { simpleParser } = require('mailparser');
const addressParser = require('nodemailer/lib/addressparser');
const MailComposer = require('nodemailer/lib/mail-composer');
const { randomBytes } = require('node:crypto');
const ARMORED_MESSAGE = /-----BEGIN PGP MESSAGE-----[\s\S]+?-----END PGP MESSAGE-----/;
@@ -131,7 +132,21 @@ function parseAddresses(value) {
return addressParser(String(value || '')).filter(entry => entry.address || entry.name);
}
function innerEntity(text) {
async function innerEntity(text, attachments) {
if (attachments.length) {
return new Promise((resolve, reject) => {
new MailComposer({
text: String(text || ''),
attachments: attachments.map(file => ({
filename: file.filename,
contentType: file.contentType,
content: file.content
}))
}).compile().build((error, message) => error
? reject(error)
: resolve(Buffer.from(message.toString('latin1').replace(/\r?\n/g, '\r\n'), 'latin1')));
});
}
const body = Buffer.from(String(text || ''), 'utf8');
return Buffer.from(`Content-Type: text/plain; charset=utf-8\r\nContent-Transfer-Encoding: base64\r\n\r\n${chunkBase64(body)}\r\n`, 'latin1');
}
@@ -252,7 +267,7 @@ function createPgpMail({ keyStore, cache }) {
},
// Builds a complete RFC 3156 message. Returns { raw, envelope }.
async build({ from, to, cc, subject, text, messageId, date = new Date(), encrypt, sign, passphrase, signerFingerprint }) {
async build({ from, to, cc, subject, text, messageId, date = new Date(), encrypt, sign, passphrase, signerFingerprint, attachments = [] }) {
const toList = parseAddresses(to);
const ccList = parseAddresses(cc);
if (!toList.length) throw new Error('Add at least one recipient.');
@@ -284,7 +299,7 @@ function createPgpMail({ keyStore, cache }) {
`Message-ID: ${String(messageId).replace(/[\r\n<>\s]/g, '').replace(/^/, '<').replace(/$/, '>')}`,
'MIME-Version: 1.0'
];
const inner = innerEntity(text);
const inner = await innerEntity(text, attachments);
const mark = boundary();
let body;
let contentType;