Neah/lib/imap.ts
2025-04-24 16:43:07 +02:00

73 lines
2.0 KiB
TypeScript

import { ImapFlow } from 'imapflow';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
import { prisma } from '@/lib/prisma';
export async function getImapClient() {
const session = await getServerSession(authOptions);
if (!session?.user?.id) {
throw new Error('No authenticated user');
}
const credentials = await prisma.mailCredentials.findUnique({
where: {
userId: session.user.id
}
});
if (!credentials) {
throw new Error('No mail credentials found. Please configure your email account.');
}
return new ImapFlow({
host: credentials.host,
port: credentials.port,
secure: true,
auth: {
user: credentials.email,
pass: credentials.password,
},
logger: false,
tls: {
rejectUnauthorized: false
}
});
}
export async function moveEmails(emailIds: number[], targetFolder: string) {
const imap = await getImapClient();
const lock = await imap.getMailboxLock('INBOX');
try {
for (const id of emailIds) {
const message = await imap.fetchOne(id.toString(), { uid: true });
if (message) {
const uid = typeof message.uid === 'number' ? message.uid.toString() : message.uid;
await imap.messageMove(uid, targetFolder);
}
}
} finally {
lock.release();
}
}
export async function markAsRead(emailIds: (string | number)[], isRead: boolean) {
const imap = await getImapClient();
try {
await imap.connect();
const lock = await imap.getMailboxLock('INBOX');
try {
for (const id of emailIds) {
const stringId = typeof id === 'number' ? id.toString() : id;
const message = await imap.fetchOne(stringId, { uid: true });
if (message) {
const uid = typeof message.uid === 'number' ? message.uid.toString() : message.uid;
await imap.messageFlagsAdd(uid, isRead ? ['\\Seen'] : [], { uid: true });
}
}
} finally {
lock.release();
}
} finally {
await imap.logout();
}
}