From 1ce37420bdfaff592e8aaf2d785a88a22ccb4244 Mon Sep 17 00:00:00 2001 From: alma Date: Tue, 15 Apr 2025 23:45:50 +0200 Subject: [PATCH] mail page imap connection mime 5 bis rest 16 login page 6 --- app/api/mail/route.ts | 78 ++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 46 deletions(-) diff --git a/app/api/mail/route.ts b/app/api/mail/route.ts index d16a1c5..b6942f9 100644 --- a/app/api/mail/route.ts +++ b/app/api/mail/route.ts @@ -121,43 +121,31 @@ function parseEmailHeaders(buffer: string): EmailHeaders { } // Helper function to create a promise-based IMAP connection -function createImapConnection(imapConfig: Imap.Config) { +function createImapConnection(config: any): Promise { return new Promise((resolve, reject) => { - console.log('Creating new IMAP connection with config:', { - user: imapConfig.user, - host: imapConfig.host, - port: imapConfig.port, - tls: imapConfig.tls + const imap = new Imap({ + user: config.user, + password: config.password, + host: config.host, + port: config.port, + tls: true, + tlsOptions: { rejectUnauthorized: false }, + authTimeout: 10000, + connTimeout: 10000, + debug: console.log, + autotls: 'always', + keepalive: true }); - const imap = new Imap(imapConfig); - - imap.once('ready', () => { - console.log('IMAP connection established successfully'); - resolve(imap); - }); - + imap.once('ready', () => resolve(imap)); imap.once('error', (err: ImapError) => { console.error('IMAP connection error:', err); - console.error('Error details:', { - type: err.type, - textCode: err.textCode, - source: err.source - }); reject(err); }); + imap.once('end', () => console.log('[connection] Ended')); + imap.once('close', () => console.log('[connection] Closed')); - imap.once('end', () => { - console.log('IMAP connection ended'); - }); - - try { - console.log('Attempting to connect to IMAP server...'); - imap.connect(); - } catch (err) { - console.error('Error during IMAP connection:', err); - reject(err); - } + imap.connect(); }); } @@ -229,36 +217,34 @@ function fetchMessages(imap: Imap, box: string): Promise { } export async function GET() { - console.log('Starting email fetch process...'); - try { + // Get stored credentials const credentials = getStoredCredentials(); if (!credentials) { return NextResponse.json( - { error: 'No credentials found', details: 'Please login first' }, + { error: 'No IMAP credentials found. Please configure your email account.' }, { status: 401 } ); } - const imapConfig = { - user: credentials.email, - password: credentials.password, + console.log('Starting email fetch process...'); + console.log('IMAP Configuration:', { + user: credentials.user, host: credentials.host, port: credentials.port, tls: true, - debug: console.log - }; - - console.log('IMAP Configuration:', { - user: imapConfig.user, - host: imapConfig.host, - port: imapConfig.port, - tls: imapConfig.tls, - hasPassword: !!imapConfig.password + hasPassword: !!credentials.password + }); + + // Create IMAP connection + const imap = await createImapConnection({ + user: credentials.user, + password: credentials.password, + host: credentials.host, + port: parseInt(credentials.port), + tls: true }); - const imap = new Imap(imapConfig); - return new Promise((resolve, reject) => { imap.once('ready', () => { console.log('IMAP connection ready');