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 // 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) => { return new Promise((resolve, reject) => {
console.log('Creating new IMAP connection with config:', { const imap = new Imap({
user: imapConfig.user, user: config.user,
host: imapConfig.host, password: config.password,
port: imapConfig.port, host: config.host,
tls: imapConfig.tls 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', () => resolve(imap));
imap.once('ready', () => {
console.log('IMAP connection established successfully');
resolve(imap);
});
imap.once('error', (err: ImapError) => { imap.once('error', (err: ImapError) => {
console.error('IMAP connection error:', err); console.error('IMAP connection error:', err);
console.error('Error details:', {
type: err.type,
textCode: err.textCode,
source: err.source
});
reject(err); reject(err);
}); });
imap.once('end', () => console.log('[connection] Ended'));
imap.once('close', () => console.log('[connection] Closed'));
imap.once('end', () => { imap.connect();
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);
}
}); });
} }
@ -229,36 +217,34 @@ function fetchMessages(imap: Imap, box: string): Promise<ImapMessage[]> {
} }
export async function GET() { export async function GET() {
console.log('Starting email fetch process...');
try { try {
// Get stored credentials
const credentials = getStoredCredentials(); const credentials = getStoredCredentials();
if (!credentials) { if (!credentials) {
return NextResponse.json( return NextResponse.json(
{ error: 'No credentials found', details: 'Please login first' }, { error: 'No IMAP credentials found. Please configure your email account.' },
{ status: 401 } { status: 401 }
); );
} }
const imapConfig = { console.log('Starting email fetch process...');
user: credentials.email, console.log('IMAP Configuration:', {
password: credentials.password, user: credentials.user,
host: credentials.host, host: credentials.host,
port: credentials.port, port: credentials.port,
tls: true, tls: true,
debug: console.log hasPassword: !!credentials.password
}; });
console.log('IMAP Configuration:', { // Create IMAP connection
user: imapConfig.user, const imap = await createImapConnection({
host: imapConfig.host, user: credentials.user,
port: imapConfig.port, password: credentials.password,
tls: imapConfig.tls, host: credentials.host,
hasPassword: !!imapConfig.password port: parseInt(credentials.port),
tls: true
}); });
const imap = new Imap(imapConfig);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
imap.once('ready', () => { imap.once('ready', () => {
console.log('IMAP connection ready'); console.log('IMAP connection ready');