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:
@@ -0,0 +1,30 @@
|
||||
const path = require('node:path');
|
||||
|
||||
function normalizeOutgoingAttachments(value) {
|
||||
if (value === undefined) return [];
|
||||
if (!Array.isArray(value)) throw new Error('The attachment list is invalid.');
|
||||
return value.map(file => {
|
||||
if (!file || typeof file !== 'object' || typeof file.contentBase64 !== 'string') {
|
||||
throw new Error('An attachment could not be read.');
|
||||
}
|
||||
const filename = path.basename(String(file.filename || 'attachment').replace(/\\/g, '/'))
|
||||
.replace(/[\r\n\0]/g, '')
|
||||
.slice(0, 255) || 'attachment';
|
||||
const contentType = String(file.contentType || 'application/octet-stream').trim().toLowerCase();
|
||||
if (!/^[a-z0-9!#$&^_.+-]+\/[a-z0-9!#$&^_.+-]+$/.test(contentType)) {
|
||||
throw new Error(`The content type for ${filename} is invalid.`);
|
||||
}
|
||||
const encoded = file.contentBase64;
|
||||
if (encoded.length % 4 !== 0 || !/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(encoded)) {
|
||||
throw new Error(`The contents of ${filename} are invalid.`);
|
||||
}
|
||||
const content = Buffer.from(encoded, 'base64');
|
||||
return { filename, contentType, contentBase64: encoded, size: content.length, content };
|
||||
});
|
||||
}
|
||||
|
||||
function serializeOutgoingAttachments(attachments) {
|
||||
return attachments.map(({ content, ...file }) => file);
|
||||
}
|
||||
|
||||
module.exports = { normalizeOutgoingAttachments, serializeOutgoingAttachments };
|
||||
+14
-2
@@ -9,6 +9,7 @@ const { createDAVClient } = require('tsdav');
|
||||
const ICAL = require('ical.js');
|
||||
const openpgp = require('openpgp');
|
||||
const { createKeyServerService, listServers, MAX_CUSTOM_SERVERS, normalizeCustomServer } = require('./pgp-keyservers.cjs');
|
||||
const { normalizeOutgoingAttachments, serializeOutgoingAttachments } = require('./mail-attachments.cjs');
|
||||
|
||||
const credentialsPath = () => path.join(app.getPath('userData'), 'mail-account.enc');
|
||||
const davCredentialsPath = () => path.join(app.getPath('userData'), 'dav-accounts.enc');
|
||||
@@ -1027,6 +1028,7 @@ ipcMain.handle('mail:save-draft', async (_event, input) => {
|
||||
cc: String(input?.cc || '').slice(0, 4000),
|
||||
subject: String(input?.subject || '').slice(0, 998),
|
||||
text: String(input?.text || '').slice(0, 500000),
|
||||
attachments: serializeOutgoingAttachments(normalizeOutgoingAttachments(input?.attachments)),
|
||||
updatedAt: Date.now()
|
||||
};
|
||||
await updateAccountStore(store => {
|
||||
@@ -1594,6 +1596,7 @@ ipcMain.handle('mail:sync-inbox', async () => {
|
||||
async function sendMailNow(account, message) {
|
||||
const to = String(message?.to || '').trim();
|
||||
if (!to) throw new Error('Add at least one recipient.');
|
||||
const attachments = normalizeOutgoingAttachments(message?.attachments);
|
||||
const transport = smtpTransport(account);
|
||||
const sentMessageId = String(message.messageId || `<${require('node:crypto').randomUUID()}@ep-mail.local>`);
|
||||
const fromName = String(account.displayName || message.displayName || account.email).replace(/[\r\n]+/g, ' ').trim().slice(0, 80) || account.email;
|
||||
@@ -1605,7 +1608,12 @@ async function sendMailNow(account, message) {
|
||||
cc: String(message.cc || '').trim() || undefined,
|
||||
subject: String(message.subject || '(no subject)').slice(0, 998),
|
||||
text: signature ? `${body}${body.trim() ? '\n\n' : ''}${signature}`.slice(0, 505000) : body,
|
||||
messageId: sentMessageId
|
||||
messageId: sentMessageId,
|
||||
attachments: attachments.map(file => ({
|
||||
filename: file.filename,
|
||||
contentType: file.contentType,
|
||||
content: file.content
|
||||
}))
|
||||
};
|
||||
const rawMessage = message.rawBase64 ? Buffer.from(String(message.rawBase64), 'base64') : null;
|
||||
try {
|
||||
@@ -1644,6 +1652,8 @@ ipcMain.handle('mail:send', async (event, message) => {
|
||||
if (!account) throw new Error('Add a mail account before sending.');
|
||||
const pgpOptions = message?.pgp && (message.pgp.encrypt || message.pgp.sign) ? message.pgp : null;
|
||||
const queuedMessage = { ...message, messageId: message?.messageId || `<${randomUUID()}@ep-mail.local>` };
|
||||
const attachments = normalizeOutgoingAttachments(message?.attachments);
|
||||
queuedMessage.attachments = serializeOutgoingAttachments(attachments);
|
||||
delete queuedMessage.pgp;
|
||||
if (!pgpOptions?.encrypt) {
|
||||
const recipients = [queuedMessage.to, queuedMessage.cc].filter(Boolean);
|
||||
@@ -1677,11 +1687,13 @@ ipcMain.handle('mail:send', async (event, message) => {
|
||||
messageId: queuedMessage.messageId,
|
||||
encrypt: Boolean(pgpOptions.encrypt),
|
||||
sign: Boolean(pgpOptions.sign),
|
||||
passphrase: typeof pgpOptions.passphrase === 'string' && pgpOptions.passphrase ? pgpOptions.passphrase : null
|
||||
passphrase: typeof pgpOptions.passphrase === 'string' && pgpOptions.passphrase ? pgpOptions.passphrase : null,
|
||||
attachments: attachments.map(({ filename, contentType, content }) => ({ filename, contentType, content }))
|
||||
});
|
||||
queuedMessage.rawBase64 = built.raw.toString('base64');
|
||||
queuedMessage.envelope = built.envelope;
|
||||
queuedMessage.text = '';
|
||||
queuedMessage.attachments = [];
|
||||
} catch (error) {
|
||||
if (error instanceof PgpError) return pgpFailure(error);
|
||||
throw error;
|
||||
|
||||
+18
-3
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user