mail page imap connection mime 5 bis rest 11

This commit is contained in:
alma 2025-04-15 23:10:36 +02:00
parent 814ec3849b
commit 39a448917f

View File

@ -12,11 +12,16 @@ console.log('IMAP Configuration:', {
const imapConfig: Imap.Config = {
user: process.env.IMAP_USER as string,
password: encodeURIComponent(process.env.IMAP_PASSWORD as string),
password: process.env.IMAP_PASSWORD as string,
host: process.env.IMAP_HOST || 'mail.infomaniak.com',
port: parseInt(process.env.IMAP_PORT || '993', 10),
tls: true,
tlsOptions: { rejectUnauthorized: false }
tlsOptions: {
rejectUnauthorized: false,
servername: process.env.IMAP_HOST || 'mail.infomaniak.com'
},
authTimeout: 10000,
connTimeout: 10000
};
// Validate IMAP configuration
@ -47,9 +52,27 @@ function createImapConnection() {
return new Promise((resolve, reject) => {
const imap = new Imap(imapConfig);
imap.once('ready', () => resolve(imap));
imap.once('error', (err: Error) => reject(err));
imap.connect();
imap.once('ready', () => {
console.log('IMAP connection established successfully');
resolve(imap);
});
imap.once('error', (err: Error) => {
console.error('IMAP connection error:', err);
reject(err);
});
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);
}
});
}
@ -122,9 +145,25 @@ function fetchMessages(imap: Imap, box: string): Promise<ImapMessage[]> {
export async function GET(request: Request) {
try {
console.log('Starting email fetch process...');
// Validate IMAP configuration
if (!imapConfig.user || !imapConfig.password) {
console.error('IMAP configuration error:', {
user: imapConfig.user,
hasPassword: !!imapConfig.password
});
throw new Error('IMAP credentials are not properly configured. Please check your .env file.');
}
console.log('Creating IMAP connection...');
const imap = await createImapConnection() as Imap;
console.log('Fetching messages...');
const messages = await fetchMessages(imap, 'INBOX');
console.log(`Successfully fetched ${messages.length} messages`);
// Process messages into the format expected by the frontend
const processedMessages = messages.map((msg: ImapMessage, index: number) => ({
id: index + 1,
@ -144,8 +183,11 @@ export async function GET(request: Request) {
return NextResponse.json({ messages: processedMessages });
} catch (error) {
console.error('Error fetching emails:', error);
return NextResponse.json({ error: 'Failed to fetch emails' }, { status: 500 });
console.error('Error in GET handler:', error);
return NextResponse.json({
error: 'Failed to fetch emails',
details: error instanceof Error ? error.message : 'Unknown error'
}, { status: 500 });
}
}