mail page imap connection mime 5 bis rest 16 login page 6

This commit is contained in:
alma 2025-04-15 23:45:50 +02:00
parent a9221f2d65
commit 1ce37420bd

View File

@ -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<Imap> {
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<ImapMessage[]> {
}
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');