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

View File

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

View File

@ -9,10 +9,6 @@ const envSchema = z.object({
KEYCLOAK_CLIENT_SECRET: z.string(),
KEYCLOAK_REALM: z.string(),
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_API_URL: z.string().url(),
ROCKET_CHAT_TOKEN: z.string(),

View File

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

View File

@ -20,25 +20,6 @@ const nextConfig = {
webpackBuildWorker: true,
parallelServerBuildTraces: 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,
}
};