mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
An email to a member's personal board address, board+<token>@<domain>, becomes a card on that board, as Trello's email-to-board does (#794, #3948). The subject is the title; the body (quoted replies stripped, HTML as text) is the description, with a From line; attachments are attached (no SVG, as for uploads). The author is the address's member, never the From address, and they must still have write access when the email arrives. A reply whose In-Reply-To/References matches a card's thread becomes a comment on it. Notification Settings, at three levels: the Admin Panel turns it on and holds the domain, the provider webhook secret and the IMAP mailboxes with their passwords (never published, never sent back to a browser); a board admin turns the board on and picks the list and swimlane; board and member choose which parts of an email are used. The member sees and replaces their own address. It arrives by POST /api/inbound-email (Mailgun, SendGrid, Postmark, JSON, forms, multipart with attachments, raw MIME), by IMAP polling with imapflow and mailparser under a database lease so one replica polls, or on Sandstorm through the grain's own address: the bridge's Maildir is read, a member's board token in the subject routes it, and WeKan's outgoing email goes through HackSessionContext.send, since a grain has no network. The email and hack-session capnp schemas are generated with the other Sandstorm bindings. Unknown addresses, wrong webhook secrets and over-limit addresses are logged under authn.inbound-email. Rate limit per address, size limits, Message-ID deduplication. The new strings are translated in every locale. Thanks to xet7 !
95 lines
3.5 KiB
JavaScript
95 lines
3.5 KiB
JavaScript
// Read POST /api/inbound-email into the message shape of
|
|
// server/lib/inboundEmailPayload.js, whatever the provider sends:
|
|
// - JSON or a URL-encoded form (Postmark; Mailgun and SendGrid without
|
|
// attachments) - already parsed by server/apiMiddleware.js;
|
|
// - multipart/form-data (Mailgun and SendGrid with attachments) - read here
|
|
// with @fastify/busboy, under size and count limits;
|
|
// - the whole message: SendGrid's "raw" mode (an `email` field), a relay's
|
|
// `raw`/`mime` field, or a body of type message/rfc822 - parsed with
|
|
// mailparser.
|
|
const Busboy = require('@fastify/busboy');
|
|
const { simpleParser } = require('mailparser');
|
|
const { fromProviderFields, fromParsedMail } = require('./inboundEmailPayload');
|
|
|
|
const MAX_BYTES = Number(process.env.INBOUND_EMAIL_MAX_BYTES) || 30 * 1024 * 1024;
|
|
|
|
function contentType(req) {
|
|
return String((req.headers && req.headers['content-type']) || '').toLowerCase();
|
|
}
|
|
|
|
function readMultipart(req) {
|
|
return new Promise((resolve, reject) => {
|
|
const fields = {};
|
|
const files = [];
|
|
let total = 0;
|
|
let busboy;
|
|
try {
|
|
busboy = new Busboy({
|
|
headers: req.headers,
|
|
limits: { fileSize: MAX_BYTES, files: 30, fields: 200, fieldSize: MAX_BYTES, parts: 230 },
|
|
});
|
|
} catch (e) {
|
|
reject(e);
|
|
return;
|
|
}
|
|
busboy.on('field', (name, value) => {
|
|
total += Buffer.byteLength(value || '');
|
|
if (total > MAX_BYTES) { reject(new Error('too-large')); return; }
|
|
fields[name] = value;
|
|
});
|
|
busboy.on('file', (fieldname, stream, filename, encoding, mimetype) => {
|
|
const chunks = [];
|
|
stream.on('data', chunk => {
|
|
total += chunk.length;
|
|
if (total > MAX_BYTES) { stream.resume(); return; }
|
|
chunks.push(chunk);
|
|
});
|
|
stream.on('end', () => {
|
|
if (!stream.truncated && total <= MAX_BYTES) {
|
|
files.push({ fieldname, filename, contentType: mimetype, content: Buffer.concat(chunks) });
|
|
}
|
|
});
|
|
});
|
|
busboy.on('error', reject);
|
|
busboy.on('finish', () => (total > MAX_BYTES ? reject(new Error('too-large')) : resolve({ fields, files })));
|
|
req.pipe(busboy);
|
|
});
|
|
}
|
|
|
|
function readRaw(req) {
|
|
return new Promise((resolve, reject) => {
|
|
const chunks = [];
|
|
let total = 0;
|
|
req.on('data', chunk => {
|
|
total += chunk.length;
|
|
if (total > MAX_BYTES) { reject(new Error('too-large')); req.destroy(); return; }
|
|
chunks.push(chunk);
|
|
});
|
|
req.on('end', () => resolve(Buffer.concat(chunks)));
|
|
req.on('error', reject);
|
|
});
|
|
}
|
|
|
|
async function parseWhole(source) {
|
|
return fromParsedMail(await simpleParser(source, { skipImageLinks: true, skipTextLinks: true }));
|
|
}
|
|
|
|
const RAW_FIELDS = ['email', 'raw', 'mime', 'body-mime'];
|
|
|
|
async function readInboundMessage(req) {
|
|
const type = contentType(req);
|
|
if (type.startsWith('multipart/form-data')) {
|
|
const { fields, files } = await readMultipart(req);
|
|
const raw = RAW_FIELDS.map(name => fields[name]).find(v => typeof v === 'string' && v);
|
|
return { message: raw ? await parseWhole(raw) : fromProviderFields(fields, files), fields };
|
|
}
|
|
if (type.startsWith('message/rfc822')) {
|
|
return { message: await parseWhole(await readRaw(req)), fields: {} };
|
|
}
|
|
const body = req.body && typeof req.body === 'object' ? req.body : {};
|
|
const raw = RAW_FIELDS.map(name => body[name]).find(v => typeof v === 'string' && v);
|
|
return { message: raw ? await parseWhole(raw) : fromProviderFields(body), fields: body };
|
|
}
|
|
|
|
module.exports = { readInboundMessage, MAX_BYTES };
|