mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
Email that comes back is handled in WeKan instead of landing in the administrator's own inbox. Admin Panel -> E-mail -> Bounces: hard bounces (a mistyped invited address), soft bounces, automatic replies and spam complaints, each with actions done after a count - list it, stop emailing the address, tell who invited it, delete the invitation, ignore - as listmonk and Discourse do it, and the list of addresses that bounced, which an admin clears to email them again. Amazon SES Bounce and Complaint notifications and mail SES receives arrive through SNS at /api/inbound-email/ses, signature-checked against Amazon's certificate and accepted only from the topics the Admin Panel lists. Delivery status notifications and out-of-office replies arriving by IMAP, a provider webhook or a Sandstorm grain are bounces too, never cards. Admin Panel -> Problems -> Email shows what went wrong sending or receiving: a send the mail server refused or could not reach, an IMAP mailbox that cannot be read (never with its password), an unreadable email, an email for a board that no longer takes email. Attempts stay in Problems -> Security. The inbound email endpoints no longer need the whole REST API turned on. Runtime tests run WeKan's own transports against local SMTP and IMAP servers over TCP, a real Sandstorm Maildir, the webhook over HTTP and signed SNS messages; UI tests drive the Email and Chat views, the email-to-card settings and the Bounces page. They found and this fixes: every whole-message parse failed in the server bundle (nodemailer's addressparser resolved to its ESM build), the Chat view's drag and drop never started (dragscroll took the mouse) and dropped a card into the target's previous list, and the Chat panes did not follow a change to a right-to-left language. Thanks to xet7 ! Fixes #794, Fixes #3948.
162 lines
7.0 KiB
JavaScript
162 lines
7.0 KiB
JavaScript
// Email to card and reply-by-email from IMAP mailboxes (#794): the Admin
|
|
// Panel names the mailboxes (Notification Settings -> Inbound email), and
|
|
// WeKan polls each one - Gmail, Outlook/Microsoft 365, Fastmail, Dovecot,
|
|
// Exchange, anything that speaks IMAP - with imapflow, parses each new
|
|
// message with mailparser, and hands it to the same receiveEmail() the
|
|
// webhook uses (server/routes/inboundEmail.js).
|
|
//
|
|
// Only one WeKan process polls a mailbox: a lease document per account in
|
|
// `inbound_mail_state`, taken with an atomic update and renewed each poll, so
|
|
// several replicas behind one database do not each make the card. The same
|
|
// document remembers the mailbox's UIDVALIDITY and the last UID read, so a
|
|
// message is read once even when it is not for WeKan (a shared mailbox) and
|
|
// is left as it was; a message that was for WeKan is marked seen, or deleted
|
|
// when the account says so.
|
|
//
|
|
// Not on Sandstorm: a grain has no network, and gets its mail through its own
|
|
// address instead (server/sandstormEmail.js).
|
|
import { Meteor } from 'meteor/meteor';
|
|
import { Mongo } from 'meteor/mongo';
|
|
import { Random } from 'meteor/random';
|
|
|
|
const { simpleParser } = require('mailparser');
|
|
const { fromParsedMail } = require('/server/lib/inboundEmailPayload');
|
|
|
|
const State = new Mongo.Collection('inbound_mail_state');
|
|
const OWNER = Random.id();
|
|
const INTERVAL_MS = Math.max(15, Number(process.env.INBOUND_IMAP_POLL_SECONDS) || 60) * 1000;
|
|
const LEASE_MS = INTERVAL_MS * 3;
|
|
const MAX_PER_POLL = 50;
|
|
const MAX_MESSAGE_BYTES = Number(process.env.INBOUND_EMAIL_MAX_BYTES) || 30 * 1024 * 1024;
|
|
|
|
let timer = null;
|
|
|
|
async function takeLease(accountId) {
|
|
const now = new Date();
|
|
try {
|
|
const result = await State.rawCollection().findOneAndUpdate(
|
|
{ _id: accountId, $or: [{ owner: OWNER }, { leaseUntil: { $lt: now } }, { leaseUntil: { $exists: false } }] },
|
|
{ $set: { owner: OWNER, leaseUntil: new Date(now.getTime() + LEASE_MS) } },
|
|
{ upsert: true, returnDocument: 'after' },
|
|
);
|
|
const doc = result && (result.value !== undefined ? result.value : result);
|
|
return doc && doc.owner === OWNER ? doc : null;
|
|
} catch (e) {
|
|
// Another process holds it: the upsert collided with its document.
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async function pollAccount(account, password) {
|
|
const state = await takeLease(account.id);
|
|
if (!state) return;
|
|
const { ImapFlow } = require('imapflow');
|
|
const client = new ImapFlow({
|
|
host: account.host,
|
|
port: account.port,
|
|
secure: account.secure,
|
|
auth: { user: account.username, pass: password },
|
|
logger: false,
|
|
});
|
|
const { receiveEmail } = require('/server/routes/inboundEmail');
|
|
await client.connect();
|
|
try {
|
|
const lock = await client.getMailboxLock(account.mailbox || 'INBOX');
|
|
try {
|
|
const uidValidity = String(client.mailbox.uidValidity);
|
|
// A new UIDVALIDITY means the old UIDs mean nothing: start from what is
|
|
// unseen now rather than reading the whole mailbox again.
|
|
let lastUid = state.uidValidity === uidValidity ? Number(state.lastUid) || 0 : null;
|
|
const range = lastUid === null ? { seen: false } : { uid: `${lastUid + 1}:*` };
|
|
const uids = ((await client.search(range, { uid: true })) || [])
|
|
.filter(uid => lastUid === null || uid > lastUid)
|
|
.sort((a, b) => a - b)
|
|
.slice(0, MAX_PER_POLL);
|
|
for (const uid of uids) {
|
|
const msg = await client.fetchOne(String(uid), { source: true, size: true }, { uid: true });
|
|
let handled = false;
|
|
if (msg && msg.source && (!msg.size || msg.size <= MAX_MESSAGE_BYTES)) {
|
|
try {
|
|
const message = fromParsedMail(await simpleParser(msg.source, { skipImageLinks: true, skipTextLinks: true }));
|
|
const result = await receiveEmail(message, { source: `imap:${account.id}` });
|
|
handled = result.handled === true;
|
|
} catch (e) {
|
|
console.warn(`inbound IMAP ${account.id}: message ${uid} not processed:`, e && e.message);
|
|
require('/server/lib/mailProblems').recordMailProblem('receive-unreadable', {
|
|
source: `imap:${account.id}`, detail: `message ${uid} in ${account.mailbox}: ${e && e.message}` });
|
|
}
|
|
}
|
|
if (handled) {
|
|
if (account.after === 'delete') await client.messageDelete(String(uid), { uid: true });
|
|
else await client.messageFlagsAdd(String(uid), ['\\Seen'], { uid: true });
|
|
}
|
|
lastUid = uid;
|
|
await State.updateAsync(account.id, { $set: { uidValidity, lastUid, lastPollAt: new Date(), lastError: null } });
|
|
}
|
|
if (lastUid === null) {
|
|
// Nothing unseen on a fresh mailbox: remember where it ends.
|
|
lastUid = Math.max(0, (client.mailbox.uidNext || 1) - 1);
|
|
}
|
|
await State.updateAsync(account.id, { $set: { uidValidity, lastUid, lastPollAt: new Date(), lastError: null } });
|
|
} finally {
|
|
lock.release();
|
|
}
|
|
} finally {
|
|
await client.logout().catch(() => {});
|
|
}
|
|
}
|
|
|
|
// One poll of every mailbox. A poll already running is waited for and then
|
|
// followed by a fresh one, so a caller (the Admin Panel's Save, a test) always
|
|
// gets a poll that started after it asked.
|
|
let inFlight = null;
|
|
export async function pollAll() {
|
|
while (inFlight) await inFlight.catch(() => {});
|
|
inFlight = pollAllOnce();
|
|
try {
|
|
return await inFlight;
|
|
} finally {
|
|
inFlight = null;
|
|
}
|
|
}
|
|
|
|
async function pollAllOnce() {
|
|
const { inboundMailSettings } = require('/server/emailToCard');
|
|
const inboundMail = await inboundMailSettings();
|
|
if (inboundMail.enabled !== true) return;
|
|
for (const account of inboundMail.accounts || []) {
|
|
const password = inboundMail.passwords && inboundMail.passwords[account.id];
|
|
if (!account.enabled || !password) continue;
|
|
try {
|
|
await pollAccount(account, password);
|
|
} catch (e) {
|
|
const error = String((e && (e.responseText || e.message)) || e).slice(0, 500);
|
|
console.warn(`inbound IMAP ${account.id}:`, error);
|
|
require('/server/lib/mailProblems').recordMailProblem('imap-failed', {
|
|
source: `imap:${account.id}`,
|
|
detail: `${account.label || account.id} (${account.username}@${account.host}:${account.port}): ${error}`,
|
|
});
|
|
try { await State.updateAsync(account.id, { $set: { lastError: error, lastPollAt: new Date() } }); } catch (x) { /* best effort */ }
|
|
}
|
|
}
|
|
}
|
|
|
|
function start() {
|
|
if (timer || Meteor.settings?.public?.sandstorm || process.env.INBOUND_IMAP_DISABLED === 'true') return;
|
|
timer = Meteor.setInterval(() => { pollAll().catch(e => console.warn('inbound IMAP:', e && e.message)); }, INTERVAL_MS);
|
|
}
|
|
|
|
export function restart() {
|
|
if (timer) { Meteor.clearInterval(timer); timer = null; }
|
|
start();
|
|
pollAll().catch(e => console.warn('inbound IMAP:', e && e.message));
|
|
}
|
|
|
|
// For Admin Panel: when each mailbox was last read, and its last error.
|
|
export async function imapAccountStatus() {
|
|
const rows = await State.find({}, { fields: { lastPollAt: 1, lastError: 1 } }).fetchAsync();
|
|
return Object.fromEntries(rows.map(r => [r._id, { lastPollAt: r.lastPollAt || null, lastError: r.lastError || null }]));
|
|
}
|
|
|
|
Meteor.startup(start);
|