solve mail backend 10

This commit is contained in:
alma 2025-04-17 14:09:45 +02:00
parent bb26e61fa6
commit 43abed23ab
5 changed files with 29 additions and 32 deletions

View File

@ -44,7 +44,7 @@ export async function POST(request: Request) {
// Test IMAP connection // Test IMAP connection
const client = new ImapFlow({ const client = new ImapFlow({
host, host: host,
port: parseInt(port), port: parseInt(port),
secure: true, secure: true,
auth: { auth: {
@ -52,7 +52,10 @@ export async function POST(request: Request) {
pass: password, pass: password,
}, },
logger: false, logger: false,
emitLogs: false emitLogs: false,
tls: {
rejectUnauthorized: false // Allow self-signed certificates
}
}); });
try { try {

View File

@ -38,7 +38,10 @@ export async function GET() {
pass: credentials.password, pass: credentials.password,
}, },
logger: false, logger: false,
emitLogs: false emitLogs: false,
tls: {
rejectUnauthorized: false // Allow self-signed certificates
}
}); });
await client.connect(); await client.connect();

View File

@ -9,10 +9,6 @@ const envSchema = z.object({
KEYCLOAK_CLIENT_SECRET: z.string(), KEYCLOAK_CLIENT_SECRET: z.string(),
KEYCLOAK_REALM: z.string(), KEYCLOAK_REALM: z.string(),
KEYCLOAK_ISSUER: z.string().url(), KEYCLOAK_ISSUER: z.string().url(),
IMAP_HOST: z.string(),
IMAP_PORT: z.string(),
IMAP_USER: z.string().email(),
IMAP_PASSWORD: z.string(),
LEANTIME_TOKEN: z.string(), LEANTIME_TOKEN: z.string(),
LEANTIME_API_URL: z.string().url(), LEANTIME_API_URL: z.string().url(),
ROCKET_CHAT_TOKEN: z.string(), ROCKET_CHAT_TOKEN: z.string(),

View File

@ -1,6 +1,7 @@
import { ImapFlow } from 'imapflow'; import { ImapFlow } from 'imapflow';
import { getServerSession } from 'next-auth'; import { getServerSession } from 'next-auth';
import { authOptions } from '@/app/api/auth/[...nextauth]/route'; import { authOptions } from '@/app/api/auth/[...nextauth]/route';
import { prisma } from '@/lib/prisma';
let client: ImapFlow | null = null; let client: ImapFlow | null = null;
@ -8,19 +9,32 @@ export async function getImapClient() {
if (client) return client; if (client) return client;
const session = await getServerSession(authOptions); const session = await getServerSession(authOptions);
if (!session?.user?.email) { if (!session?.user?.id) {
throw new Error('No authenticated user'); 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.');
}
client = new ImapFlow({ client = new ImapFlow({
host: process.env.IMAP_HOST || 'imap.gmail.com', host: credentials.host,
port: parseInt(process.env.IMAP_PORT || '993', 10), port: credentials.port,
secure: true, secure: true,
auth: { auth: {
user: session.user.email, user: credentials.email,
pass: session.user.accessToken pass: credentials.password,
}, },
logger: false logger: false,
tls: {
rejectUnauthorized: false
}
}); });
await client.connect(); await client.connect();

View File

@ -20,25 +20,6 @@ const nextConfig = {
webpackBuildWorker: true, webpackBuildWorker: true,
parallelServerBuildTraces: true, parallelServerBuildTraces: true,
parallelServerCompiles: true, parallelServerCompiles: true,
},
// Load environment variables
env: {
IMAP_USER: process.env.IMAP_USER,
IMAP_PASSWORD: process.env.IMAP_PASSWORD,
IMAP_HOST: process.env.IMAP_HOST,
IMAP_PORT: process.env.IMAP_PORT,
},
// Ensure environment variables are available in API routes
serverRuntimeConfig: {
IMAP_USER: process.env.IMAP_USER,
IMAP_PASSWORD: process.env.IMAP_PASSWORD,
IMAP_HOST: process.env.IMAP_HOST,
IMAP_PORT: process.env.IMAP_PORT,
},
// Make environment variables available to the client
publicRuntimeConfig: {
IMAP_HOST: process.env.IMAP_HOST,
IMAP_PORT: process.env.IMAP_PORT,
} }
}; };